diff --git a/gen/functions.cpp b/gen/functions.cpp index 55768c6368..3fd73dd8b4 100644 --- a/gen/functions.cpp +++ b/gen/functions.cpp @@ -303,6 +303,16 @@ void DtoResolveFunction(FuncDeclaration* fdecl) return; // ignore declaration completely } + if (AggregateDeclaration* ad = fdecl->isMember()) + { + ad->codegen(Type::sir); + if (ad->isStructDeclaration() && llvm::isa(DtoType(ad->type))) + { + ad->ir.irStruct->structFuncs.push_back(fdecl); + return; + } + } + //printf("resolve function: %s\n", fdecl->toPrettyChars()); if (fdecl->parent) @@ -434,6 +444,8 @@ static void set_param_attrs(TypeFunction* f, llvm::Function* func, FuncDeclarati void DtoDeclareFunction(FuncDeclaration* fdecl) { + DtoResolveFunction(fdecl); + if (fdecl->ir.declared) return; fdecl->ir.declared = true; @@ -478,6 +490,9 @@ void DtoDeclareFunction(FuncDeclaration* fdecl) if (!func) func = llvm::Function::Create(functype, DtoLinkage(fdecl), mangled_name, gIR->module); + if (Logger::enabled()) + Logger::cout() << "func = " << *func << std::endl; + // add func to IRFunc fdecl->ir.irFunc->func = func; @@ -594,6 +609,8 @@ void DtoDeclareFunction(FuncDeclaration* fdecl) void DtoDefineFunction(FuncDeclaration* fd) { + DtoDeclareFunction(fd); + if (fd->ir.defined) return; fd->ir.defined = true; diff --git a/gen/structs.cpp b/gen/structs.cpp index 6fe7907bca..ce2f491da6 100644 --- a/gen/structs.cpp +++ b/gen/structs.cpp @@ -14,6 +14,7 @@ #include "gen/logger.h" #include "gen/structs.h" #include "gen/dvalue.h" +#include "gen/functions.h" #include "ir/irstruct.h" @@ -583,6 +584,14 @@ void DtoResolveStruct(StructDeclaration* sd) gIR->module->addTypeName(sd->mangle(),ST); } + // emit functions + size_t n = irstruct->structFuncs.size(); + for (size_t i = 0; i < n; ++i) + { + DtoResolveFunction(irstruct->structFuncs[i]); + } + irstruct->structFuncs.clear(); + gIR->structs.pop_back(); DtoDeclareStruct(sd); diff --git a/ir/irstruct.h b/ir/irstruct.h index 1744a56b31..ff92353834 100644 --- a/ir/irstruct.h +++ b/ir/irstruct.h @@ -152,6 +152,7 @@ struct IrStruct : IrBase llvm::DICompositeType diCompositeType; std::vector staticVars; + std::vector structFuncs; }; //////////////////////////////////////////////////////////////////////////////