// File: /workspace/github_cpp_makefile_star2_O2/marxn[P]pupp/src/parser/function.cpp
// Function: FunctionNode::Provision()
bool FunctionNode::Provision()
{
        list<FuncArgDef*>::iterator i;
        for(i = this->ArgList->begin(); i!=this->ArgList->end(); i++)
        {
                string argname = (*i)->GetName();
                DataType argtype = (*i)->GetType();
                VariableDef * localvar = new VariableDef(argname);
                localvar->SetVarType(argtype);
                this->AddVariable(localvar);
                
                unsigned long varindex = localvar->GetVarIndex();
                (*i)->SetArgIndex(varindex);
        }

        Node * parent = this->GetParentNode();
        if(parent==NULL)
        {
                cerr<<"pupp provision error: A function cannot be defined without a context."<<endl;
                return false;
        }

        list<ClosureVarDesc*>::iterator j;

        if(this->CopyList!=NULL)
        {
                int pos = 0;
                
                for(j = this->CopyList->begin(); j!= this->CopyList->end(); j++)
                {
                        //Because its parent is NOT a Node but a lambda exp, the layer has to be set to 0 to skip the exp.
                        //This is extremely unusual.
                        unsigned long layer = 0; 
                        
                        string varname = (*j)->GetVarName();
                        VariableDef * vardef = parent->FindVariable(varname, &layer);
                        if(vardef==NULL)
                        {
                                cerr<<"pupp provision error: Variable "<<varname<<" has not been defined"<<endl;
                                return false;
                        }
                        
                        VariableDef * newvardef = new VariableDef(varname);
                        newvardef->SetVarType(vardef->GetVarType());
                        newvardef->SetNeedInstance(false);

                        this->AddVariable(newvardef);
                        
                        (*j)->SetSrcLayer(layer);
                        (*j)->SetSrcDef(vardef);
                        (*j)->SetDstDef(newvardef);
                        
                        if(pos++==0)
                        {
                                this->ClosureVarStartIndex = newvardef->GetVarIndex();
                        }
                        
                }
        }
        
        return ContainerNode::Provision();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq 0x98(%rdi), %rax
leaq 0x38(%rsp), %r14
leaq 0x78(%rsp), %r12
movq %rax, %r15
movq (%r15), %r15
cmpq %rax, %r15
je 0xa6aa
movq 0x10(%r15), %rsi
movq %r14, %rdi
callq 0x6380
movq 0x10(%r15), %rax
movl 0x20(%rax), %ebp
pushq $0x40
popq %rdi
callq 0x6260
movq %rax, %r13
movq %r12, %rdi
movq %r14, %rsi
callq 0x6380
movq %r13, %rdi
movq %r12, %rsi
callq 0x1501a
movq %r12, %rdi
callq 0x6130
movq %r13, %rdi
movl %ebp, %esi
callq 0x150ee
movq %rbx, %rdi
movq %r13, %rsi
callq 0xa9c0
movq %r13, %rdi
callq 0x15116
movq 0x10(%r15), %rcx
movq %rax, 0x30(%rcx)
movq %r14, %rdi
callq 0x6130
movq 0x98(%rbx), %rax
jmp 0xa630
movq %rbx, %rdi
callq 0xa9b0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0xa827
movq 0xa0(%rbx), %r14
testq %r14, %r14
je 0xa81d
xorl %eax, %eax
leaq 0x38(%rsp), %r15
leaq 0x18(%rsp), %r12
movq (%r14), %r14
cmpq 0xa0(%rbx), %r14
je 0xa81d
movl %eax, 0x4(%rsp)
andq $0x0, 0x8(%rsp)
movq 0x10(%r14), %rsi
movq %r15, %rdi
callq 0x6380
movq %r12, %rdi
movq %r15, %rsi
callq 0x6380
movq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rbp
movq %rbp, %rsi
leaq 0x8(%rsp), %rdx
callq 0xaaac
movq %rax, %r13
movq %rbp, %rdi
callq 0x6130
testq %r13, %r13
je 0xa7b1
movq %r15, %r12
pushq $0x40
popq %rdi
callq 0x6260
movq %rax, %rbp
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x6380
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1501a
movq %r15, %rdi
callq 0x6130
movq %r13, %rdi
callq 0x150f2
movq %rbp, %rdi
movl %eax, %esi
callq 0x150ee
movq %rbp, %rdi
xorl %esi, %esi
callq 0x15120
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa9c0
movq 0x10(%r14), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq %r13, 0x28(%rax)
movq 0x10(%r14), %rax
movq %rbp, 0x30(%rax)
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0xa7e8
incl %eax
jmp 0xa7fa
movq 0x38838(%rip), %rdi     # 0x42ff0
leaq 0x24a34(%rip), %rsi     # 0x2f1f3
callq 0x6240
movq %rax, %rdi
movq %r15, %rsi
callq 0x6210
movq %rax, %rdi
leaq 0x24a3a(%rip), %rsi     # 0x2f213
callq 0x6240
movq %rax, %rdi
callq 0x6140
jmp 0xa801
movq %rbp, %rdi
callq 0x15116
movq %rax, 0xa8(%rbx)
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
movq %r12, %r15
movq %r15, %rdi
callq 0x6130
testq %r13, %r13
leaq 0x18(%rsp), %r12
movl 0x4(%rsp), %eax
jne 0xa6dc
jmp 0xa842
movq %rbx, %rdi
callq 0x8892
jmp 0xa844
movq 0x387c2(%rip), %rdi     # 0x42ff0
leaq 0x24dab(%rip), %rsi     # 0x2f5e0
callq 0x6240
movq %rax, %rdi
callq 0x6140
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x6130
jmp 0xa868
movq %rax, %rbx
pushq $0x40
popq %rsi
movq %rbp, %rdi
jmp 0xa89d
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x6130
jmp 0xa8a7
jmp 0xa8a4
jmp 0xa8a4
jmp 0xa8a4
movq %rax, %rbx
jmp 0xa897
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x6130
pushq $0x40
popq %rsi
movq %r13, %rdi
callq 0x6270
jmp 0xa8a7
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x6130
movq %rbx, %rdi
callq 0x6460
nop