1   // Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2   
3   package jminusminus;
4   
5   /**
6    * The AST node for a formal parameter declaration. All analysis and code
7    * generation is done in a parent AST.
8    */
9   
10  class JFormalParameter extends JAST {
11  
12      /** Parameter name. */
13      private String name;
14  
15      /** Parameter type. */
16      private Type type;
17  
18      /**
19       * Construct an AST node for a formal parameter declaration given its line
20       * number, name, and type.
21       * 
22       * @param line
23       *            line in which the parameter occurs in the source file.
24       * @param name
25       *            parameter name.
26       * @param type
27       *            parameter type.
28       */
29  
30      public JFormalParameter(int line, String name, Type type) {
31          super(line);
32          this.name = name;
33          this.type = type;
34      }
35  
36      /**
37       * Return the parameter's name.
38       * 
39       * @return the parameter's name.
40       */
41  
42      public String name() {
43          return name;
44      }
45  
46      /**
47       * Return the parameter's type.
48       * 
49       * @return the parameter's type.
50       */
51  
52      public Type type() {
53          return type;
54      }
55  
56      /**
57       * Set the type to the specified type.
58       * 
59       * @param newType
60       *            the new type.
61       * @return return the new type.
62       */
63  
64      public Type setType(Type newType) {
65          return type = newType;
66      }
67  
68      /**
69       * No analysis done here.
70       * 
71       * @param context
72       *            context in which names are resolved.
73       * @return the analyzed (and possibly rewritten) AST subtree.
74       */
75  
76      public JAST analyze(Context context) {
77          // Nothing to do
78          return this;
79      }
80  
81      /**
82       * No code generated here.
83       * 
84       * @param output
85       *            the code emitter (basically an abstraction for producing the
86       *            .class file).
87       */
88  
89      public void codegen(CLEmitter output) {
90          // Nothing to do
91      }
92  
93      /**
94       * @inheritDoc
95       */
96  
97      public void writeToStdOut(PrettyPrinter p) {
98          p.printf("<JFormalParameter line=\"%d\" name=\"%s\" "
99                  + "type=\"%s\"/>\n", line(), name, (type == null) ? "" : type
100                 .toString());
101     }
102 
103 }
104