diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-21 06:14:00 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-21 06:14:00 +0000 |
| commit | db8c7f14d2f74c3db205782acf8222cb7d91a047 (patch) | |
| tree | 31b6e17c0cb818ba0cc700cd1e0ca896a6b9156f /lib/CodeGen/Designator.cc | |
| parent | 25ae363250e479701ed532c55c255e043c8240da (diff) | |
feat(codegen): instantiate class templates
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/Designator.cc')
| -rw-r--r-- | lib/CodeGen/Designator.cc | 56 |
1 files changed, 33 insertions, 23 deletions
diff --git a/lib/CodeGen/Designator.cc b/lib/CodeGen/Designator.cc index 12d3b60..ed43cbe 100644 --- a/lib/CodeGen/Designator.cc +++ b/lib/CodeGen/Designator.cc @@ -42,35 +42,45 @@ void CodeGen::runTypePrePass(const std::vector<std::unique_ptr<StmtAST>> &Statem if (!Ok) reportError("%s", Err.c_str()); } - if (Phase == 2) for (const auto &S : Statements) { - auto *Class = dynamic_cast<ClassTypeDeclAST*>(S.get()); - if (!Class) continue; - auto &Methods = ClassMethods[Class->getName()]; - IdentifierSet Names; - for (const auto &Field : Class->getFields()) { - Names.insert(Field.Name); - if (sameIdentifier(Field.Name, "this")) { - SaveAndRestore<int> Location(DiagnosticLine, Class->getLine()); - reportError("A class field cannot be named this"); - } + if (Phase == 2) for (const auto &S : Statements) + if (auto *Class = dynamic_cast<ClassTypeDeclAST*>(S.get())) registerClassMethods(Class); +} + +void CodeGen::registerClassMethods(ClassTypeDeclAST *Class) { + auto &Methods = ClassMethods[Class->getName()]; + IdentifierSet Names; + for (const auto &Field : Class->getFields()) { + Names.insert(Field.Name); + if (sameIdentifier(Field.Name, "this")) { + SaveAndRestore<int> Location(DiagnosticLine, Class->getLine()); + reportError("A class field cannot be named this"); } - for (const auto &Method : Class->getMethods()) { - SaveAndRestore<int> Location(DiagnosticLine, Method->getLine()); - const std::string &Name = Method->getProto()->getName(); - if (sameIdentifier(Name, "this")) { - reportError("A method cannot be named this"); - } else if (Names.count(Name) || - (!Methods[Name].empty() && (!Method->getProto()->isOverload() || - !Methods[Name].front()->getProto()->isOverload()))) { - reportError("Duplicate member %s in class %s", Name.c_str(), Class->getName().c_str()); - } else { - Methods[Name].push_back(Method.get()); - } + } + for (const auto &Method : Class->getMethods()) { + SaveAndRestore<int> Location(DiagnosticLine, Method->getLine()); + const std::string &Name = Method->getProto()->getName(); + if (sameIdentifier(Name, "this")) { + reportError("A method cannot be named this"); + } else if (Names.count(Name) || + (!Methods[Name].empty() && (!Method->getProto()->isOverload() || + !Methods[Name].front()->getProto()->isOverload()))) { + reportError("Duplicate member %s in class %s", Name.c_str(), Class->getName().c_str()); + } else { + Methods[Name].push_back(Method.get()); } } } +void CodeGen::emitClassPrototypes(ClassTypeDeclAST *Class) { + for (const auto &Method : Class->getMethods()) { + SaveAndRestore<int> Location(DiagnosticLine, Method->getLine()); + if (isRegisteredMethod(Class->getName(), Method->getProto())) + FuncGen->emitPrototype(Method->getProto()); + } +} + const std::vector<FunctionDefAST *> *CodeGen::findMethod(const std::string &Class, const std::string &Name) const { + if (Class.find('<') != std::string::npos && !resolveType(Class)) return nullptr; auto C = ClassMethods.find(Class); if (C == ClassMethods.end()) return nullptr; auto M = C->second.find(Name); |
