Files
whetstone_DSL/editor/tests/step278_test.cpp
Bill 090320f165 Steps 278-283: Phase 10d — shims, optimization, meta-programming & policy annotations (117/117 tests)
- Step 278: Shim/escape hatch — Intrinsic, Raw, CallingConv, Link, Shim, PointerArithmetic, Opaque (25 tests)
- Step 279: Platform/provenance — Target, Feature, Original, Mapping (19 tests)
- Step 280: Optimization completion — TailCall, Loop, Data, Align, Pack, BoundsCheck, Overflow (22 tests)
- Step 281: Meta-programming — Meta, Symbol, Evaluate, Template, Synthetic (20 tests)
- Step 282: Strategy/policy — Policy, Ambiguity, Candidate, Tradeoff, Choice, Decision (22 tests)
- Step 283: Integration tests — FFI workflow, sidecar roundtrip, RPC, taxonomy completeness (9 tests)
- 29 new annotation classes with JSON roundtrip, compact AST, sidecar persistence
- All 58 semantic annotation types recognized across 8 subjects

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 18:29:43 +00:00

146 lines
6.2 KiB
C++

// Step 278: Shim & Escape Hatch Annotations (12 tests)
#include <cassert>
#include <iostream>
#include <string>
#include "ast/ASTNode.h"
#include "ast/Module.h"
#include "ast/Function.h"
#include "ast/Annotation.h"
#include "ast/Serialization.h"
#include "ASTUtils.h"
#include "CompactAST.h"
static int passed = 0, failed = 0;
static void check(bool c, const std::string& n) {
if (c) { std::cout << " PASS: " << n << "\n"; ++passed; }
else { std::cout << " FAIL: " << n << "\n"; ++failed; }
}
static void test_intrinsic() {
IntrinsicAnnotation a;
check(a.conceptType == "IntrinsicAnnotation", "IntrinsicAnnotation conceptType");
a.instruction = "_mm256_add_ps"; a.arch = "x86_avx2";
check(a.instruction == "_mm256_add_ps" && a.arch == "x86_avx2", "fields set");
}
static void test_raw() {
RawAnnotation a; a.language = "c"; a.code = "asm volatile(\"nop\")";
check(a.conceptType == "RawAnnotation", "RawAnnotation conceptType");
check(a.language == "c" && a.code == "asm volatile(\"nop\")", "fields set");
}
static void test_callingconv_link_shim() {
CallingConvAnnotation cc; cc.convention = "stdcall";
check(cc.convention == "stdcall", "CallingConvAnnotation field");
LinkAnnotation la; la.symbolName = "printf"; la.library = "libc.so";
check(la.symbolName == "printf" && la.library == "libc.so", "LinkAnnotation fields");
ShimAnnotation sa; sa.strategy = "vtable";
check(sa.strategy == "vtable", "ShimAnnotation field");
}
static void test_marker_and_opaque() {
PointerArithmeticAnnotation pa;
check(pa.conceptType == "PointerArithmeticAnnotation", "PointerArithmeticAnnotation conceptType");
OpaqueAnnotation oa; oa.reason = "platform-specific ABI";
check(oa.reason == "platform-specific ABI", "OpaqueAnnotation field");
}
static void test_intrinsic_roundtrip() {
auto* a = new IntrinsicAnnotation(); a->id = "i1";
a->instruction = "_mm_mul_ps"; a->arch = "x86_sse";
json j = toJson(a);
check(j["properties"]["instruction"] == "_mm_mul_ps", "JSON instruction");
ASTNode* r = fromJson(j);
auto* ri = dynamic_cast<IntrinsicAnnotation*>(r);
check(ri && ri->instruction == "_mm_mul_ps" && ri->arch == "x86_sse", "roundtrip");
delete a; delete r;
}
static void test_link_roundtrip() {
auto* a = new LinkAnnotation(); a->id = "l1";
a->symbolName = "malloc"; a->library = "libc";
json j = toJson(a); ASTNode* r = fromJson(j);
auto* rl = dynamic_cast<LinkAnnotation*>(r);
check(rl && rl->symbolName == "malloc" && rl->library == "libc", "LinkAnnotation roundtrip");
delete a; delete r;
}
static void test_raw_roundtrip() {
auto* a = new RawAnnotation(); a->id = "r1";
a->language = "cpp"; a->code = "__attribute__((packed))";
json j = toJson(a); ASTNode* r = fromJson(j);
auto* rr = dynamic_cast<RawAnnotation*>(r);
check(rr && rr->language == "cpp" && rr->code == "__attribute__((packed))", "RawAnnotation roundtrip");
delete a; delete r;
}
static void test_create_node_factory() {
std::vector<std::string> types = {
"IntrinsicAnnotation", "RawAnnotation", "CallingConvAnnotation",
"LinkAnnotation", "ShimAnnotation", "PointerArithmeticAnnotation",
"OpaqueAnnotation"
};
for (const auto& t : types) {
ASTNode* n = createNode(t);
check(n && n->conceptType == t, "createNode(" + t + ")");
delete n;
}
}
static void test_compact_ast_intrinsic() {
auto* f = new Function(); f->id = "f1"; f->name = "simd_add";
auto* a = new IntrinsicAnnotation(); a->instruction = "_mm_add"; a->arch = "sse";
f->addChild("annotations", a);
json sem = extractSemanticSummary(f);
check(sem.contains("intrinsic") && sem["intrinsic"]["instruction"] == "_mm_add", "intrinsic in compact AST");
delete f;
}
static void test_compact_ast_shim_pointer() {
auto* f = new Function(); f->id = "f2"; f->name = "ffi_bridge";
auto* s = new ShimAnnotation(); s->strategy = "trampoline";
auto* p = new PointerArithmeticAnnotation();
f->addChild("annotations", s); f->addChild("annotations", p);
json sem = extractSemanticSummary(f);
check(sem["shim"] == "trampoline", "shim in compact AST");
check(sem["pointerArithmetic"] == true, "pointerArithmetic in compact AST");
delete f;
}
static void test_all_shim_annotations() {
auto* f = new Function(); f->id = "f3"; f->name = "fullShim";
f->addChild("annotations", [&]{ auto* a = new IntrinsicAnnotation(); a->instruction="x"; return a; }());
f->addChild("annotations", [&]{ auto* a = new RawAnnotation(); a->code="x"; return a; }());
f->addChild("annotations", [&]{ auto* a = new CallingConvAnnotation(); a->convention="cdecl"; return a; }());
f->addChild("annotations", [&]{ auto* a = new LinkAnnotation(); a->symbolName="x"; return a; }());
f->addChild("annotations", [&]{ auto* a = new ShimAnnotation(); a->strategy="cast"; return a; }());
f->addChild("annotations", new PointerArithmeticAnnotation());
f->addChild("annotations", [&]{ auto* a = new OpaqueAnnotation(); a->reason="x"; return a; }());
json sem = extractSemanticSummary(f);
check(sem.contains("intrinsic") && sem.contains("raw") &&
sem.contains("callingConv") && sem.contains("link") &&
sem.contains("shim") && sem.contains("pointerArithmetic") &&
sem.contains("opaque"), "all 7 shim annotations in summary");
delete f;
}
static void test_callingconv_roundtrip() {
auto* a = new CallingConvAnnotation(); a->id="cc1"; a->convention="fastcall";
json j = toJson(a); ASTNode* r = fromJson(j);
auto* rc = dynamic_cast<CallingConvAnnotation*>(r);
check(rc && rc->convention == "fastcall", "CallingConvAnnotation roundtrip");
delete a; delete r;
}
int main() {
std::cout << "=== Step 278: Shim & Escape Hatch Annotations ===\n";
test_intrinsic(); test_raw(); test_callingconv_link_shim();
test_marker_and_opaque(); test_intrinsic_roundtrip();
test_link_roundtrip(); test_raw_roundtrip();
test_create_node_factory(); test_compact_ast_intrinsic();
test_compact_ast_shim_pointer(); test_all_shim_annotations();
test_callingconv_roundtrip();
std::cout << "\nResults: " << passed << "/" << (passed+failed) << "\n";
return failed > 0 ? 1 : 0;
}