summaryrefslogtreecommitdiff
path: root/node/FunctionNode.cc
diff options
context:
space:
mode:
authorNagi5Yeq <wu@x-nagi.com>2020-05-30 00:25:03 +0800
committerNagi5Yeq <wu@x-nagi.com>2020-05-30 00:25:03 +0800
commit56d68765016fc7aa68fe7b522de5e84c7a12123a (patch)
treeba9828637913aa40bc1b1509c5127be9bded3a6e /node/FunctionNode.cc
parentf15a071f6d599d2a21083dfb041b18db8d482949 (diff)
global variable, constant support
Diffstat (limited to 'node/FunctionNode.cc')
-rw-r--r--node/FunctionNode.cc23
1 files changed, 9 insertions, 14 deletions
diff --git a/node/FunctionNode.cc b/node/FunctionNode.cc
index 349b02d..a812ee1 100644
--- a/node/FunctionNode.cc
+++ b/node/FunctionNode.cc
@@ -10,23 +10,18 @@
namespace pcc {
FunctionNode::FunctionNode(
- Context* context, std::string name, std::list<Declaration> arguments,
- std::list<std::shared_ptr<BaseNode>> LocalDeclarations,
+ Context* context, std::string&& name, std::list<Declaration>&& arguments,
+ std::list<std::shared_ptr<DeclNode>>&& LocalDeclarations,
std::shared_ptr<Type> type, std::shared_ptr<StatementListNode> body)
: BaseNode(context)
- , name_(name)
- , arguments_(arguments)
- , LocalDeclarations_(LocalDeclarations)
+ , name_(std::move(name))
+ , arguments_(std::move(arguments))
+ , LocalDeclarations_(std::move(LocalDeclarations))
, type_(type)
, locals_(context->GetGlobals())
, body_(body) {
- for (auto& decl : LocalDeclarations_) {
- std::shared_ptr<VarDeclNode> VarDecl =
- std::dynamic_pointer_cast<VarDeclNode>(decl);
- if (VarDecl != nullptr) {
- // set the inherited attribute
- VarDecl->SetScope(&locals_);
- }
+ for (auto&& decl : LocalDeclarations_) {
+ decl->SetScope(&locals_);
}
}
@@ -51,13 +46,13 @@ Value FunctionNode::CodeGen() {
builder->SetInsertPoint(block);
// alloc arguments
llvm::Function::arg_iterator ArgIt = function->arg_begin();
- for (auto& arg : arguments_) {
+ for (auto&& arg : arguments_) {
Value ArgValue = builder->CreateAlloca(std::get<1>(arg)->GetLLVMType());
builder->CreateStore(ArgIt++, ArgValue);
locals_.Add(std::get<0>(arg), {std::get<1>(arg), ArgValue});
}
// alloc local variables
- for (auto& decl : LocalDeclarations_) {
+ for (auto&& decl : LocalDeclarations_) {
decl->CodeGen();
}
// create a variable with the same name as the function for return value