*/
private JVariableRef createVariableRef(SourceInfo info, JVariable variable) {
if (variable instanceof JLocal) {
JLocal local = (JLocal) variable;
if (local.getEnclosingMethod() != currentMethod) {
throw new InternalCompilerException(
"LocalRef referencing local in a different method.");
}
return new JLocalRef(info, local);
} else if (variable instanceof JParameter) {
JParameter parameter = (JParameter) variable;
if (parameter.getEnclosingMethod() != currentMethod) {
throw new InternalCompilerException(
"ParameterRef referencing param in a different method.");
}
return new JParameterRef(info, parameter);
} else if (variable instanceof JField) {
JField field = (JField) variable;
JExpression instance = null;
if (!field.isStatic()) {
JClassType fieldEnclosingType = (JClassType) field.getEnclosingType();
instance = createThisRef(info, fieldEnclosingType);
if (!program.typeOracle.canTriviallyCast(
(JClassType) instance.getType(), fieldEnclosingType)) {
throw new InternalCompilerException(
"FieldRef referencing field in a different type.");
}
}
return new JFieldRef(info.makeChild(JavaASTGenerationVisitor.class,
"Reference", variable.getSourceInfo()), instance, field,
currentClass);
}
throw new InternalCompilerException("Unknown JVariable subclass.");
}