1   // Copyright 2012- Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2   
3   package jminusminus;
4   
5   /**
6    * The IDefn type is used to implement definitions of things (local variables, formal arguments,
7    * types) that are named in some context (aka scope).
8    */
9   interface IDefn {
10      /**
11       * The (local variable, formal parameter, or local or imported name) definition's type.
12       *
13       * @return the definition's type.
14       */
15      public Type type();
16  }
17  
18  /**
19   * A definition of a type name. In the first instance, an identifier, but later resolved to a
20   * local name or an imported name.
21   */
22  class TypeNameDefn implements IDefn {
23      /**
24       * The definition's type.
25       */
26      private Type type;
27  
28      /**
29       * Constructs a type name definition for a type.
30       *
31       * @param type the definition's type.
32       */
33      public TypeNameDefn(Type type) {
34          this.type = type;
35      }
36  
37      /**
38       * Returns the type for this definition.
39       *
40       * @return the definition's type.
41       */
42      public Type type() {
43          return type;
44      }
45  }
46  
47  /**
48   * The definition for a local variable (including formal parameters). All local variables are
49   * allocated on the stack at fixed offsets from the base of the stack frame and all have types.
50   * Some local variables have initializations.
51   */
52  class LocalVariableDefn implements IDefn {
53      // The local variable's type.
54      private Type type;
55  
56      // The local variable's offset from the base of the current the stack frame.
57      private int offset;
58  
59      // Has this local variable been initialized?
60      private boolean isInitialized;
61  
62      /**
63       * Constructs a local variable definition for a local variable.
64       *
65       * @param type   the variable's type.
66       * @param offset the variable's offset from the base of the current stack frame (allocated
67       *               for each method invocation).
68       */
69      public LocalVariableDefn(Type type, int offset) {
70          this.type = type;
71          this.offset = offset;
72      }
73  
74      /**
75       * Returns the type for this variable.
76       *
77       * @return the type.
78       */
79      public Type type() {
80          return type;
81      }
82  
83      /**
84       * Returns the offset of this variable on the stack frame.
85       *
86       * @return the offset.
87       */
88      public int offset() {
89          return offset;
90      }
91  
92      /**
93       * Initializes this local variable.
94       */
95      public void initialize() {
96          this.isInitialized = true;
97      }
98  
99      /**
100      * Returns true if this local variable has been initialized, and false otherwise.
101      *
102      * @returntrue if this local variable has been initialized, and false otherwise.
103      */
104     public boolean isInitialized() {
105         return isInitialized;
106     }
107 }
108