1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// Copyright (c) 1995 David Engberg All rights reserved
// $Id: VariableDeclaration.C,v 1.6 1997/08/21 16:30:12 geppetto Exp $
#include "VariableDeclaration.h"
#include "Expression.h"
#include "CompileError.h"
#include "JavaMethodInfo.h"
#include "JavaClassFile.h"
#include "Compiler.h"
#include "BinaryExpression.h"
#include "CompileContext.h"
//
// Method name : CVariableDeclaration
// Description : Default constructor.
//
CVariableDeclaration::CVariableDeclaration()
: fInitializer(0),
fFinal(false)
{
}
//
// Method name : CVariableDeclaration
// Description : Constructs a variable declaration out of its name, type, and
// optional initializer.
//
CVariableDeclaration::CVariableDeclaration(const CJavaTypeSignature& type,
const unicode_string& name, CExpression* adoptInitializer,
bool final)
: fSignature(type, name),
fInitializer(adoptInitializer),
fFinal(final)
{
}
//
// Method name : CVariableDeclaration
// Description : Copy constructor.
//
CVariableDeclaration::CVariableDeclaration(const CVariableDeclaration& source)
: fSignature(source.fSignature),
fInitializer(0),
fFinal(source.fFinal)
{
if (source.fInitializer != 0) {
fInitializer = source.fInitializer->Clone();
}
}
//
// Method name : ~CVariableDeclaration
// Description : Destructor
//
CVariableDeclaration::~CVariableDeclaration()
{
delete fInitializer;
}
//
// Method name : GetType
// Description : Returns the type of this variable.
//
CJavaTypeSignature
CVariableDeclaration::GetType() const
{
return fSignature.GetType();
}
//
// Method name : SetType
// Description : Sets the type of this variable.
//
void
CVariableDeclaration::SetType(const CJavaTypeSignature& type)
{
fSignature = CJavaFieldSignature(type, fSignature.GetFieldName());
}
//
// Method name : GenerateCode
// Description : Appends the generated bytecode for this statement onto the
// code parameter, using the other parameters to help get the
// needed information. In addition, the 'stackUsed' parameter is set to
// the maximum expression stack depth incurred by this statement.
// If this operation is successful, 0 is returned, otherwise a compile
// error structure is created and returned to the caller to explain what
// went wrong.
//
CCompileError*
CVariableDeclaration::GenerateCode(CCodeSequence& code,
CCompileContext& context, const CJavaAccessFlags& modifiers,
unsigned short& stackUsed)
{
CCompileError* error = 0;
CJavaTypeSignature type;
stackUsed = 0;
if (fInitializer != 0) {
error = CExpression::EvaluateType(fInitializer, context, type);
if (error == 0) {
bool skipFinalConstant = false;
if (context.GetCompiler().InClassInitializers() &&
modifiers.fFinal != 0) {
CExpression* constantExpression = 0;
error = GetConstantValue(constantExpression, context);
if (constantExpression != 0) {
delete constantExpression;
skipFinalConstant = true;
}
}
if (!skipFinalConstant && error == 0) {
error =
fInitializer->GenerateCode(code, context, stackUsed, true);
}
}
}
return error;
}
//
// Method name : GetConstantValue
// Description : This method is used to try to extract a constant literal
// value associated with this variable declaration's initializer. If
// no initializer exists, or if the initializer can't be resolved to
// a constant value, then the intoPointer is set to 0, otherwise it is
// filled with the appropriate constant value.
// If a problem occurs during assesment, an error is created and returned
// to the caller, otherwise 0 is returned.
//
CCompileError*
CVariableDeclaration::GetConstantValue(CExpression*& intoPointer,
CCompileContext& context)
{
CCompileError* error = 0;
intoPointer = 0;
if (fInitializer != 0) {
CJavaTypeSignature type;
error =
CExpression::EvaluateType(fInitializer, context, type, false);
CBinaryExpression* initializer =
DYNAMIC_CAST(CBinaryExpression, fInitializer);
if (initializer != 0 && error == 0) {
const CExpression* initializerValue = initializer->GetRightArgument();
assert(initializerValue != 0);
error = initializerValue->CreateConstantExpression(intoPointer, context);
}
}
return error;
}
//
// Method name : SetFinal
// Description : Specifies whether this variable is final or not.
//
void
CVariableDeclaration::SetFinal(bool final)
{
fFinal = final;
}
//
// Method name : GetName
// Description : Spits out the name of this declared field.
//
unicode_string
CVariableDeclaration::GetName() const
{
return fSignature.GetFieldName();
}
|