1   // Copyright 2012- Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2   
3   package jminusminus;
4   
5   /**
6    * The AST node for a variable declarator, which declares a name, its type and (possibly)
7    * provides an initialization.
8    */
9   class JVariableDeclarator extends JAST {
10      // Variable name.
11      private String name;
12  
13      // Variable type.
14      private Type type;
15  
16      // Variable initializer.
17      private JExpression initializer;
18  
19      /**
20       * Constructs an AST node for a variable declarator.
21       *
22       * @param line        line in which the variable occurs in the source file.
23       * @param name        variable name.
24       * @param type        variable type.
25       * @param initializer initializer.
26       */
27      public JVariableDeclarator(int line, String name, Type type, JExpression initializer) {
28          super(line);
29          this.name = name;
30          this.type = type;
31          this.initializer = initializer;
32      }
33  
34      /**
35       * Returns the variable name.
36       *
37       * @return the variable name.
38       */
39      public String name() {
40          return name;
41      }
42  
43      /**
44       * Returns the variable type.
45       *
46       * @return the variable type.
47       */
48      public Type type() {
49          return type;
50      }
51  
52      /**
53       * Sets the variable type.
54       *
55       * @param type the type
56       */
57      public void setType(Type type) {
58          this.type = type;
59      }
60  
61      /**
62       * Returns the variable initializer.
63       *
64       * @return the variable initializer.
65       */
66      public JExpression initializer() {
67          return initializer;
68      }
69  
70      /**
71       * Sets the variable initializer.
72       *
73       * @param initializer the initializer.
74       */
75      public void setInitializer(JExpression initializer) {
76          this.initializer = initializer;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      public JVariableDeclarator analyze(Context context) {
83          // Nothing here.
84          return this;
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public void codegen(CLEmitter output) {
91          // Nothing here.
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public void toJSON(JSONElement json) {
98          JSONElement e = new JSONElement();
99          json.addChild("JVariableDeclarator:" + line, e);
100         e.addAttribute("name", name());
101         e.addAttribute("type", type == null ? "" : type.toString());
102         if (initializer != null) {
103             JSONElement e1 = new JSONElement();
104             e.addChild("Initializer", e1);
105             initializer.toJSON(e1);
106         }
107     }
108 }
109