-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast_declaration.cpp
46 lines (35 loc) · 1.33 KB
/
ast_declaration.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
42
43
44
45
46
#include "ast/ast_declaration.hpp"
// two constructors - with + without initialisation
Declaration::Declaration(Specifier _type, BaseDeclaration* _declarator)
: type(_type), init_declarator(_declarator) {}
Declaration::~Declaration() {
delete init_declarator;
}
int Declaration::getSize() const {
int array_size = init_declarator->getArraySize();
if (array_size == -1) {
// is a variable declaration (not an array)
return typeSizes.at(type);
}
// is an array with size > 0
return typeSizes.at(type) * array_size;
}
void Declaration::compile(std::ostream& os, Context& context, int destReg) const {
std::string var_name = init_declarator->getIdentifier();
if (init_declarator->getArraySize() != -1) {
// array declaration
int array_size = init_declarator->getArraySize();
// add to bindings with array name storing offset of a[0]
context.addArray(var_name, type, array_size);
} else if (init_declarator->isPointer()) {
// pointer declaration
// - type of the pointer = type of what its pointing to + sets isPointer to true
context.addVar(var_name, type, true);
}
else {
// variable declaration
context.addVar(var_name, type);
}
// sort out initialisation
init_declarator->compile(os, context, destReg);
};