From 56d68765016fc7aa68fe7b522de5e84c7a12123a Mon Sep 17 00:00:00 2001 From: Nagi5Yeq Date: Sat, 30 May 2020 00:25:03 +0800 Subject: global variable, constant support --- node/FunctionNode.cc | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'node/FunctionNode.cc') 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 arguments, - std::list> LocalDeclarations, + Context* context, std::string&& name, std::list&& arguments, + std::list>&& LocalDeclarations, std::shared_ptr type, std::shared_ptr 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 VarDecl = - std::dynamic_pointer_cast(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 -- cgit