-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfieldDeclaration.cpp
41 lines (39 loc) · 1.7 KB
/
fieldDeclaration.cpp
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
/**
* Implementation of \ref fieldDeclaration class
*/
#include "fieldDeclaration.h"
/**
* Constructor for the class
* @param dataType data type in the declaration i.e the data type for all the variables listed
* @param variables variables that are listed in the declaration
*/
fieldDeclaration::fieldDeclaration(string dataType, class Variables *variables) {
this->dataType = dataType;
this->var_list = variables->getVarsList();
}
Value *fieldDeclaration::generateCode(Constructs *compilerConstructs) {
llvm::Type *ty = nullptr;
/* Get the type reference */
if (dataType == "int") {
ty = Type::getInt32Ty(compilerConstructs->Context);
} else if (dataType == "boolean") {
ty = Type::getInt1Ty(compilerConstructs->Context);
}
for (auto var : var_list) {
/* Allocate one location of global variable for all */
if (var->isArray()) {
ArrayType *arrType = ArrayType::get(ty, var->getLength());
GlobalVariable *gv = new GlobalVariable(*(compilerConstructs->TheModule), arrType, false,
GlobalValue::ExternalLinkage, nullptr,
var->getName());
gv->setInitializer(ConstantAggregateZero::get(arrType));
} else {
GlobalVariable *gv = new GlobalVariable(*(compilerConstructs->TheModule), ty, false,
GlobalValue::ExternalLinkage, nullptr,
var->getName());
gv->setInitializer(Constant::getNullValue(ty));
}
}
Value *v = ConstantInt::get(compilerConstructs->Context, APInt(32, 1));
return v;
}