Java Reference
In-Depth Information
Before the JVariableDeclaration is analyzed, it appears exactly as it was created by the
parser, as is illustrated in Figure 4.8.
FIGURE 4.8 The sub-tree for intw=v+5,x=w+7; before analysis.
Figure 4.8 also pictures the LocalContext created for the nested block in which the
declaration occurs, but before the declaration is analyzed.
The code for analyzing a JVariableDeclaration is as follows:
publicJStatementanalyze(Contextcontext){
for(JVariableDeclaratordecl:decls){
//Localvariablesaredeclaredhere(fieldsare
//declaredinpreAnalyze())
intoffset=((LocalContext)context).nextOffset();
LocalVariableDefndefn=newLocalVariableDefn(decl
.type().resolve(context),offset);
//First,checkforshadowing
IDefnpreviousDefn=context.lookup(decl.name());
if(previousDefn!=null
&&previousDefninstanceofLocalVariableDefn){
JAST.compilationUnit.reportSemanticError(decl.line(),
"Thename"+decl.name()
+"overshadowsanotherlocalvariable.");
}
//Thendeclareitinthelocalcontext
context.addEntry(decl.line(),decl.name(),defn);
//Allinitializationsmustbeturnedintoassignment
//statementsandanalyzed
if(decl.initializer()!=null){
defn.initialize();
JAssignOpassignOp=newJAssignOp(decl.line(),
newJVariable(decl.line(),decl.name()),decl
 
Search WWH ::




Custom Search