Package jminusminus
Class JCompilationUnit
java.lang.Object
jminusminus.JAST
jminusminus.JCompilationUnit
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:
- 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.
- 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.
- 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.
-
Field Summary
Fields inherited from class jminusminus.JAST
compilationUnit, line
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionPerforms semantic analysis on this AST and returns the (possibly modified) AST.void
Performs code generation for this AST.boolean
Returns true if a semantic error has occurred up to now, and false otherwise.Returns the package in which this compilation unit is defined.void
Constructs a context for the compilation unit, initializing it with imported types.void
reportSemanticError
(int line, String message, Object... args) Reports a semantic error.void
toJSON
(JSONElement json) Stores information about this AST in JSON format.Methods inherited from class jminusminus.JAST
line, partialCodegen, preAnalyze, unescape
-
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
Returns the package in which this compilation unit is defined.- Returns:
- the package in which this compilation unit is defined.
-
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
Reports a semantic error.- Parameters:
line
- line in which the error occurred in the source file.message
- message identifying the error.args
- 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
Performs semantic analysis on this AST and returns the (possibly modified) AST. -
codegen
Performs code generation for this AST. -
toJSON
Stores information about this AST in JSON format.
-