Package jminusminus

Class JCompilationUnit

java.lang.Object
jminusminus.JAST
jminusminus.JCompilationUnit

class JCompilationUnit extends JAST
The abstract syntax tree (AST) node representing a compilation unit, and so the root of the AST.

The AST is produced by the Parser. Once the AST has been built, three successive methods are invoked:

  1. Method preAnalyze() is invoked for making a first pass at type analysis, recursively reaching down to the member headers for declaring types and member interfaces in the environment (contexts). preAnalyze() also creates a partial class file (in memory) for recording member header information.
  2. Method analyze() is invoked for type-checking field initializations and method bodies, and determining the types of all expressions. A certain amount of tree surgery is also done here. And stack frame offsets are computed for method parameters and local variables.
  3. Method codegen() is invoked for generating code for the compilation unit to a class file. For each type declaration, it instantiates a CLEmitter object (an abstraction of the class file) and then invokes methods on that CLEmitter for generating instructions. At the end of each type declaration, a method is invoked on the CLEmitter which writes the class out to the file system either as .class file or as a .s (SPIM) file. Of course, codegen() makes recursive calls down the tree, to the codegen methods at each node, for generating the appropriate instructions.
  • Constructor Details

    • JCompilationUnit

      public JCompilationUnit(String fileName, int line, TypeName packageName, ArrayList<TypeName> imports, ArrayList<JAST> typeDeclarations)
      Constructs an AST node for a compilation unit.
      Parameters:
      fileName - the name of the source file.
      line - line in which the compilation unit occurs in the source file.
      packageName - package name.
      imports - a list of imports.
      typeDeclarations - type declarations.
  • Method Details

    • packageName

      public String packageName()
      Returns the package in which this compilation unit is defined.
      Returns:
      the package in which this compilation unit is defined.
    • clFiles

      public ArrayList<CLFile> clFiles()
      Returns the list of CLFile objects corresponding to the type declarations in this compilation unit.
      Returns:
      the list of CLFile objects corresponding to the type declarations in this compilation unit.
    • errorHasOccurred

      public boolean errorHasOccurred()
      Returns true if a semantic error has occurred up to now, and false otherwise.
      Returns:
      true if a semantic error has occurred up to now, and false otherwise..
    • reportSemanticError

      public void reportSemanticError(int line, String message, Object... arguments)
      Reports a semantic error.
      Parameters:
      line - line in which the error occurred in the source file.
      message - message identifying the error.
      arguments - related values.
    • preAnalyze

      public void preAnalyze()
      Constructs a context for the compilation unit, initializing it with imported types. Then pre-analyzes the unit's type declarations, adding their types to the context.
    • analyze

      public JAST analyze(Context context)
      Performs semantic analysis on this AST and returns the (possibly modified) AST.
      Specified by:
      analyze in class JAST
      Parameters:
      context - the environment (scope) in which code is analyzed.
      Returns:
      the (possibly modified) AST.
    • codegen

      public void codegen(CLEmitter output)
      Performs code generation for this AST.
      Specified by:
      codegen in class JAST
      Parameters:
      output - the code emitter.
    • toJSON

      public void toJSON(JSONElement json)
      Stores information about this AST in JSON format.
      Overrides:
      toJSON in class JAST
      Parameters:
      json - the JSON emitter.