JThis.java |
1 // Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas 2 3 package jminusminus; 4 5 import static jminusminus.CLConstants.*; 6 7 /** 8 * The AST for a "this" expression. It serves as a target of some field 9 * selection or message. 10 */ 11 12 class JThis extends JExpression { 13 14 /** 15 * Construct an AST node for a "this" expression given its line number. 16 * 17 * @param line 18 * line in which the expression occurs in the source file. 19 */ 20 21 public JThis(int line) { 22 super(line); 23 } 24 25 /** 26 * Analysis involves simply determining the type in which we are, since that 27 * determines the type of this target. 28 * 29 * @param context 30 * context in which names are resolved. 31 * @return the analyzed (and possibly rewritten) AST subtree. 32 */ 33 34 public JExpression analyze(Context context) { 35 type = ((JClassDeclaration) context.classContext.definition()) 36 .thisType(); 37 return this; 38 } 39 40 /** 41 * Simply generate code to load "this" onto the stack. 42 * 43 * @param output 44 * the code emitter (basically an abstraction for producing the 45 * .class file). 46 */ 47 48 public void codegen(CLEmitter output) { 49 output.addNoArgInstruction(ALOAD_0); 50 } 51 52 /** 53 * inheritDoc 54 */ 55 56 public void writeToStdOut(PrettyPrinter p) { 57 p.println("<JThis/>"); 58 } 59 60 } 61