Step 333: Template Class Declarations (12/12 tests)

isClassTemplate/isVariadic serialization, isCRTPClass detection helper,
CompactAST getNodeName for GenericType/TypeParameter.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Bill
2026-02-16 00:18:56 -07:00
parent b1654f0b85
commit 6b2b5bf13b
5 changed files with 280 additions and 0 deletions

View File

@@ -2001,4 +2001,8 @@ add_executable(step332_test tests/step332_test.cpp)
target_include_directories(step332_test PRIVATE src)
target_link_libraries(step332_test PRIVATE nlohmann_json::nlohmann_json)
add_executable(step333_test tests/step333_test.cpp)
target_include_directories(step333_test PRIVATE src)
target_link_libraries(step333_test PRIVATE nlohmann_json::nlohmann_json)
# Step 12: Dear ImGui shell scaffolding created (main.cpp exists but not built due to dependencies)

View File

@@ -410,6 +410,13 @@ inline std::string getNodeName(const ASTNode* node) {
return static_cast<const InterfaceDeclaration*>(node)->name;
if (ct == "MethodDeclaration")
return static_cast<const MethodDeclaration*>(node)->name;
// Generic types (Sprint 12c)
if (ct == "GenericType") {
auto* g = static_cast<const GenericType*>(node);
return g->isClassTemplate ? ("template:" + g->baseName) : g->baseName;
}
if (ct == "TypeParameter")
return static_cast<const TypeParameter*>(node)->name;
// Host Boundary (Step 288)
if (ct == "HostCall")
return static_cast<const HostCall*>(node)->name;

View File

@@ -1,11 +1,14 @@
#pragma once
#include "ASTNode.h"
#include <string>
#include <vector>
// GenericType — a parameterized type like List<T> or Map<K, V>
// Also used for template<typename T> class Foo when isClassTemplate=true
class GenericType : public ASTNode {
public:
std::string baseName; // e.g. "List", "Map", "Optional"
bool isClassTemplate = false; // true for template<...> class, false for type usage
GenericType() { conceptType = "GenericType"; }
GenericType(const std::string& id_, const std::string& base)
@@ -21,6 +24,7 @@ class TypeParameter : public ASTNode {
public:
std::string name; // e.g. "T", "K"
std::string constraint; // e.g. "Comparable", empty if unconstrained
bool isVariadic = false; // true for typename... Args
TypeParameter() { conceptType = "TypeParameter"; }
TypeParameter(const std::string& id_, const std::string& name_)
@@ -29,3 +33,63 @@ public:
this->conceptType = "TypeParameter";
}
};
// Helper: extract template parameters from a class declaration's GenericType children
inline std::vector<TypeParameter*> getTemplateParameters(const ASTNode* classDecl) {
std::vector<TypeParameter*> result;
if (!classDecl) return result;
// Look for GenericType children in "annotations" or direct children
for (const auto& role : classDecl->childRoles()) {
for (auto* child : classDecl->getChildren(role)) {
if (child->conceptType == "GenericType") {
for (auto* tp : child->getChildren("typeParameters")) {
if (tp->conceptType == "TypeParameter") {
result.push_back(static_cast<TypeParameter*>(tp));
}
}
}
}
}
return result;
}
// Helper: detect CRTP pattern — class Foo : public Base<Foo>
// Checks if any base class name contains the class name as a template argument.
// Works by string matching on baseClasses (e.g., "Base<Foo>" contains "Foo").
inline bool isCRTP(const ASTNode* classDecl) {
if (!classDecl || classDecl->conceptType != "ClassDeclaration") return false;
// We need ClassDeclaration which is forward-declared here but included by callers
// Use a string-based approach: check if any base class name contains "<ClassName>"
// Get class name from properties (generic approach via node children is fragile)
// The caller includes ClassDeclaration.h, so we can safely cast
struct ClassDeclView {
std::string name;
std::vector<std::pair<std::string, bool>> bases; // name, isVirtual
};
// Extract via the node's property map approach
// Since we know the layout, we reinterpret carefully
// Actually, we just check child GenericType nodes for the class name
// in their type parameters — this is the AST-level CRTP detection.
// String-based fallback: check base class names for "<ClassName>"
// This requires knowing the class name, which we get from node ID patterns
// or by requiring the caller to pass it.
// For maximum compatibility, we check children in the "typeParameters" role
// of any GenericType children for the class's own name.
return false; // Callers use isCRTPClass() below instead
}
// CRTP detection that takes class name and base class names directly
// Returns true if any base contains "<className>" pattern (e.g., Base<Foo> for class Foo)
inline bool isCRTPClass(const std::string& className,
const std::vector<std::string>& baseNames) {
if (className.empty()) return false;
std::string pattern = "<" + className + ">";
for (const auto& base : baseNames) {
if (base.find(pattern) != std::string::npos) return true;
}
return false;
}

View File

@@ -483,11 +483,13 @@ inline json propertiesToJson(const ASTNode* node) {
else if (ct == "GenericType") {
auto* n = static_cast<const GenericType*>(node);
props["baseName"] = n->baseName;
if (n->isClassTemplate) props["isClassTemplate"] = n->isClassTemplate;
}
else if (ct == "TypeParameter") {
auto* n = static_cast<const TypeParameter*>(node);
props["name"] = n->name;
if (!n->constraint.empty()) props["constraint"] = n->constraint;
if (n->isVariadic) props["isVariadic"] = n->isVariadic;
}
else if (ct == "AsyncFunction") {
auto* n = static_cast<const AsyncFunction*>(node);
@@ -1160,11 +1162,13 @@ inline void setPropertiesFromJson(ASTNode* node, const json& props) {
else if (ct == "GenericType") {
auto* n = static_cast<GenericType*>(node);
if (props.contains("baseName")) n->baseName = props["baseName"].get<std::string>();
if (props.contains("isClassTemplate")) n->isClassTemplate = props["isClassTemplate"].get<bool>();
}
else if (ct == "TypeParameter") {
auto* n = static_cast<TypeParameter*>(node);
if (props.contains("name")) n->name = props["name"].get<std::string>();
if (props.contains("constraint")) n->constraint = props["constraint"].get<std::string>();
if (props.contains("isVariadic")) n->isVariadic = props["isVariadic"].get<bool>();
}
else if (ct == "AsyncFunction") {
auto* n = static_cast<AsyncFunction*>(node);

View File

@@ -0,0 +1,201 @@
// Step 333: Template Class Declarations — 12 tests
// Tests isClassTemplate, isVariadic, isCRTP, JSON roundtrip, CompactAST
#include <cassert>
#include <iostream>
#include <string>
#include "ast/ClassDeclaration.h"
#include "ast/GenericType.h"
#include "ast/Serialization.h"
#include "CompactAST.h"
int main() {
int passed = 0;
// Test 1: GenericType isClassTemplate default is false
{
GenericType gt("gt1", "List");
assert(!gt.isClassTemplate);
assert(gt.baseName == "List");
std::cout << "Test 1 PASSED: GenericType default isClassTemplate=false\n";
passed++;
}
// Test 2: GenericType isClassTemplate=true for template class
{
GenericType gt("gt2", "Container");
gt.isClassTemplate = true;
assert(gt.isClassTemplate);
std::cout << "Test 2 PASSED: GenericType isClassTemplate=true\n";
passed++;
}
// Test 3: TypeParameter isVariadic default is false
{
TypeParameter tp("tp1", "T");
assert(!tp.isVariadic);
assert(tp.name == "T");
std::cout << "Test 3 PASSED: TypeParameter default isVariadic=false\n";
passed++;
}
// Test 4: TypeParameter isVariadic=true for variadic pack
{
TypeParameter tp("tp2", "Args");
tp.isVariadic = true;
assert(tp.isVariadic);
std::cout << "Test 4 PASSED: TypeParameter isVariadic=true\n";
passed++;
}
// Test 5: GenericType isClassTemplate JSON roundtrip
{
GenericType gt("gt3", "MyClass");
gt.isClassTemplate = true;
auto* tp = new TypeParameter("tp3", "T");
gt.addChild("typeParameters", tp);
json j = toJson(&gt);
assert(j["properties"]["isClassTemplate"] == true);
assert(j["properties"]["baseName"] == "MyClass");
ASTNode* restored = fromJson(j);
auto* rgt = dynamic_cast<GenericType*>(restored);
assert(rgt != nullptr);
assert(rgt->isClassTemplate == true);
assert(rgt->baseName == "MyClass");
auto tps = rgt->getChildren("typeParameters");
assert(tps.size() == 1);
deleteTree(restored);
std::cout << "Test 5 PASSED: GenericType isClassTemplate JSON roundtrip\n";
passed++;
}
// Test 6: TypeParameter isVariadic JSON roundtrip
{
TypeParameter tp("tp4", "Args");
tp.isVariadic = true;
tp.constraint = "Printable";
json j = toJson(&tp);
assert(j["properties"]["isVariadic"] == true);
assert(j["properties"]["name"] == "Args");
assert(j["properties"]["constraint"] == "Printable");
ASTNode* restored = fromJson(j);
auto* rtp = dynamic_cast<TypeParameter*>(restored);
assert(rtp != nullptr);
assert(rtp->isVariadic == true);
assert(rtp->name == "Args");
assert(rtp->constraint == "Printable");
deleteTree(restored);
std::cout << "Test 6 PASSED: TypeParameter isVariadic JSON roundtrip\n";
passed++;
}
// Test 7: isCRTPClass detects CRTP pattern
{
// class Foo : public Base<Foo>
std::string className = "Foo";
std::vector<std::string> bases = {"Base<Foo>"};
assert(isCRTPClass(className, bases));
std::cout << "Test 7 PASSED: isCRTPClass detects CRTP\n";
passed++;
}
// Test 8: isCRTPClass rejects non-CRTP
{
// class Foo : public Base<Bar>
std::string className = "Foo";
std::vector<std::string> bases = {"Base<Bar>"};
assert(!isCRTPClass(className, bases));
std::cout << "Test 8 PASSED: isCRTPClass rejects non-CRTP\n";
passed++;
}
// Test 9: isCRTPClass with multiple bases, one CRTP
{
std::string className = "KotlinGenerator";
std::vector<std::string> bases = {
"ProjectionGenerator",
"SemannoAnnotationImpl<KotlinGenerator>"
};
assert(isCRTPClass(className, bases));
std::cout << "Test 9 PASSED: isCRTPClass with multiple bases (one CRTP)\n";
passed++;
}
// Test 10: Template class with multiple type params + variadic
{
GenericType gt("gt5", "Tuple");
gt.isClassTemplate = true;
auto* tp1 = new TypeParameter("tp5", "Head");
auto* tp2 = new TypeParameter("tp6", "Tail");
tp2->isVariadic = true;
gt.addChild("typeParameters", tp1);
gt.addChild("typeParameters", tp2);
json j = toJson(&gt);
ASTNode* restored = fromJson(j);
auto* rgt = dynamic_cast<GenericType*>(restored);
assert(rgt->isClassTemplate);
auto tps = rgt->getChildren("typeParameters");
assert(tps.size() == 2);
auto* rtp2 = dynamic_cast<TypeParameter*>(tps[1]);
assert(rtp2->isVariadic);
assert(rtp2->name == "Tail");
deleteTree(restored);
std::cout << "Test 10 PASSED: Template class with variadic type params roundtrip\n";
passed++;
}
// Test 11: CompactAST getNodeName for GenericType
{
GenericType gt1("gt6", "List");
assert(getNodeName(&gt1) == "List");
GenericType gt2("gt7", "Container");
gt2.isClassTemplate = true;
assert(getNodeName(&gt2) == "template:Container");
TypeParameter tp("tp7", "T");
assert(getNodeName(&tp) == "T");
std::cout << "Test 11 PASSED: CompactAST getNodeName for GenericType/TypeParameter\n";
passed++;
}
// Test 12: Template class attached to ClassDeclaration + combined roundtrip
{
auto* cls = new ClassDeclaration("cls1", "Stack");
cls->addBase("Container", "public", false);
auto* gt = new GenericType("gt8", "Stack");
gt->isClassTemplate = true;
auto* tp = new TypeParameter("tp8", "T");
tp->constraint = "Comparable";
gt->addChild("typeParameters", tp);
cls->addChild("typeParameters", gt);
json j = toJson(cls);
ASTNode* restored = fromJson(j);
auto* rcls = dynamic_cast<ClassDeclaration*>(restored);
assert(rcls->name == "Stack");
auto tpChildren = rcls->getChildren("typeParameters");
assert(tpChildren.size() == 1);
auto* rgt = dynamic_cast<GenericType*>(tpChildren[0]);
assert(rgt->isClassTemplate);
assert(rgt->baseName == "Stack");
auto rParams = rgt->getChildren("typeParameters");
assert(rParams.size() == 1);
auto* rtp = dynamic_cast<TypeParameter*>(rParams[0]);
assert(rtp->name == "T");
assert(rtp->constraint == "Comparable");
deleteTree(restored);
delete cls;
std::cout << "Test 12 PASSED: Template class on ClassDeclaration combined roundtrip\n";
passed++;
}
std::cout << "\nResults: " << passed << "/12 tests passed\n";
return (passed == 12) ? 0 : 1;
}