diff options
| -rw-r--r-- | include/cps/TypeSystem.h | 11 | ||||
| -rw-r--r-- | lib/CodeGen/CallGen.cc | 3 | ||||
| -rw-r--r-- | lib/CodeGen/FunctionGen.cc | 3 | ||||
| -rw-r--r-- | lib/CodeGen/TypeSystem.cc | 24 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/expected.txt | 2 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/lib.c | 8 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/notes.md | 2 | ||||
| -rw-r--r-- | tests/i/extern-c-struct/program.cps | 21 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/case.json | 4 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/notes.md | 2 | ||||
| -rw-r--r-- | tests/iii/extern-struct-rejected/program.cps | 2 |
11 files changed, 63 insertions, 19 deletions
diff --git a/include/cps/TypeSystem.h b/include/cps/TypeSystem.h index 1c1e422..f8427f3 100644 --- a/include/cps/TypeSystem.h +++ b/include/cps/TypeSystem.h @@ -154,12 +154,15 @@ public: std::string &Err); bool sameType(const std::string &Left, const std::string &Right) const; const TypeInfo *promoteCScalar(const TypeInfo *Info) const; - // A TYPE record (not a CLASS) whose fields are C scalars, C pointers, - // C structs or pointer types to C structs. Otherwise Why, if given, - // names the first offending field and its type. + // Follows pseudocode pointer types to the type they finally point to: + // Info itself when it is not one, nullptr for a cycle (TYPE P = ^P). + const TypeInfo *pointerBase(const TypeInfo *Info) const; + // A TYPE record (not a CLASS) whose fields are C types, C structs or + // pointer types to either. Otherwise Why, if given, names the first + // offending field and its type. bool isCStruct(const TypeInfo *Info, std::string *Why = nullptr) const; // Crosses the EXTERN "C" boundary: a non-void C type, a C struct or a - // pointer type to one. + // pointer type to either. bool isExternType(const TypeInfo *Info) const; const TypeInfo *commonCScalar(const TypeInfo *Left, const TypeInfo *Right) const; diff --git a/lib/CodeGen/CallGen.cc b/lib/CodeGen/CallGen.cc index a5567b7..576520d 100644 --- a/lib/CodeGen/CallGen.cc +++ b/lib/CodeGen/CallGen.cc @@ -134,8 +134,7 @@ bool CodeGen::marshalCallArgs(const FuncSig &Signature, Value *CodeGen::emitVariadicArgument(ExprAST *Expr, const std::string &Callee, size_t Position, const TypeInfo *&Promoted) { const TypeInfo *Info = isWholeArrayVar(Expr) ? nullptr : getExprTypeInfo(Expr); - const TypeInfo *Record = Info && Info->Kind == TypeKind::Pointer - ? Types->resolve(Info->Pointer.PointeeTypeName) : Info; + const TypeInfo *Record = Types->pointerBase(Info); std::string Why; if (Record && Record->isRecord() && !Record->IsClass && !Types->isCStruct(Record, &Why)) { reportError("Variadic argument %zu to %s uses record %s, which is not a C struct: %s", diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc index 6895945..48c9978 100644 --- a/lib/CodeGen/FunctionGen.cc +++ b/lib/CodeGen/FunctionGen.cc @@ -294,8 +294,7 @@ bool FunctionGen::emitExternPrototype(PrototypeAST *Proto) { // Records cross the C boundary only as C structs, by value or through a // pointer type. What names the parameter or return type in diagnostics. auto CheckRecord = [&](const TypeInfo *Info, const std::string &What) { - const TypeInfo *Record = Info->Kind == TypeKind::Pointer - ? Types.resolve(Info->Pointer.PointeeTypeName) : Info; + const TypeInfo *Record = Types.pointerBase(Info); if (!Record || !Record->isRecord() || Record->IsClass) return true; std::string Why; if (!Types.isCStruct(Record, &Why)) { diff --git a/lib/CodeGen/TypeSystem.cc b/lib/CodeGen/TypeSystem.cc index c13d81e..ee177fb 100644 --- a/lib/CodeGen/TypeSystem.cc +++ b/lib/CodeGen/TypeSystem.cc @@ -273,10 +273,8 @@ bool checkCStruct(const TypeSystem &Types, const TypeInfo *Info, std::string *Wh if (std::find(Seen.begin(), Seen.end(), Info) != Seen.end()) return true; Seen.push_back(Info); for (const auto &[FieldName, Field] : Info->Record.Fields) { - const TypeInfo *FieldInfo = Types.resolve(Field.TypeName); - if (FieldInfo && FieldInfo->isC()) continue; - const TypeInfo *Inner = FieldInfo && FieldInfo->Kind == TypeKind::Pointer - ? Types.resolve(FieldInfo->Pointer.PointeeTypeName) : FieldInfo; + const TypeInfo *Inner = Types.pointerBase(Types.resolve(Field.TypeName)); + if (Inner && Inner->isC() && !Inner->isVoid()) continue; if (Inner && Inner->isRecord() && !Inner->IsClass) { if (checkCStruct(Types, Inner, Why, Seen)) continue; return false; @@ -289,16 +287,26 @@ bool checkCStruct(const TypeSystem &Types, const TypeInfo *Info, std::string *Wh } // namespace +const TypeInfo *TypeSystem::pointerBase(const TypeInfo *Info) const { + std::vector<const TypeInfo *> Chain; + while (Info && Info->Kind == TypeKind::Pointer) { + if (std::find(Chain.begin(), Chain.end(), Info) != Chain.end()) return nullptr; + Chain.push_back(Info); + Info = resolve(Info->Pointer.PointeeTypeName); + } + return Info; +} + bool TypeSystem::isCStruct(const TypeInfo *Info, std::string *Why) const { std::vector<const TypeInfo *> Seen; return checkCStruct(*this, Info, Why, Seen); } bool TypeSystem::isExternType(const TypeInfo *Info) const { - if (!Info) return false; - if (Info->isC()) return !Info->isVoid(); - if (Info->Kind == TypeKind::Pointer) return isCStruct(resolve(Info->Pointer.PointeeTypeName)); - return isCStruct(Info); + const TypeInfo *Base = pointerBase(Info); + if (!Base) return false; + if (Base->isC()) return !Base->isVoid(); + return isCStruct(Base); } const EnumConstant *TypeSystem::lookupEnumConstant(const std::string &Name) const { diff --git a/tests/i/extern-c-struct/expected.txt b/tests/i/extern-c-struct/expected.txt index dee8467..816b4e3 100644 --- a/tests/i/extern-c-struct/expected.txt +++ b/tests/i/extern-c-struct/expected.txt @@ -2,3 +2,5 @@ S 4.500000 5 G 0.500000 7 9 14 +3 +G 9 diff --git a/tests/i/extern-c-struct/lib.c b/tests/i/extern-c-struct/lib.c index 65080f1..45bb30e 100644 --- a/tests/i/extern-c-struct/lib.c +++ b/tests/i/extern-c-struct/lib.c @@ -12,6 +12,14 @@ void scale_sample(struct Sample *s, int k) { s->value *= k; } struct Sample *shared_sample(void) { return &shared; } int shared_count(void) { return shared.count; } +struct Counter { + int *target; +}; + +void bump(int *p) { ++*p; } +void bump_counter(struct Counter *c) { ++*c->target; } +void get_shared(struct Sample **out) { *out = &shared; } + /* Each argument after n is a struct Sample *, read back with va_arg. */ int sum_counts(int n, ...) { va_list ap; diff --git a/tests/i/extern-c-struct/notes.md b/tests/i/extern-c-struct/notes.md index 519e835..494c510 100644 --- a/tests/i/extern-c-struct/notes.md +++ b/tests/i/extern-c-struct/notes.md @@ -4,4 +4,4 @@ Human review: pending. Expected output was independently traced by AI, not captu Basis: compiler extension (EXTERN "C", INCLUDE_OBJ of a user object); a record whose fields are all C types has the layout of the matching C struct. -Trace: the harness compiles `lib.c` with the host C compiler. Sample matches `struct Sample {char; double; int}`, including the padding after the char. S is 'S', 1.5, 5. scale_sample(^S, 3) multiplies the value through the pointer to 4.5 and leaves tag and count alone, so the first line is `S 4.500000 5`. shared_sample returns the address of the static struct in lib.c, initialised to 'G', 0.5, 7, giving `G 0.500000 7`. P^.Count <- 9 writes through that pointer into the C object, so shared_count() returns 9. sum_counts receives P and Q as variadic arguments; pointers are passed unchanged and read back with va_arg(ap, struct Sample *). P points to the shared struct, whose count is now 9, and Q points to S, whose count is 5, so the last line is 9 + 5 = 14. +Trace: the harness compiles `lib.c` with the host C compiler. Sample matches `struct Sample {char; double; int}`, including the padding after the char. S is 'S', 1.5, 5. scale_sample(^S, 3) multiplies the value through the pointer to 4.5 and leaves tag and count alone, so the first line is `S 4.500000 5`. shared_sample returns the address of the static struct in lib.c, initialised to 'G', 0.5, 7, giving `G 0.500000 7`. P^.Count <- 9 writes through that pointer into the C object, so shared_count() returns 9. sum_counts receives P and Q as variadic arguments; pointers are passed unchanged and read back with va_arg(ap, struct Sample *). P points to the shared struct, whose count is now 9, and Q points to S, whose count is 5, so the last line is 9 + 5 = 14. N starts at 1. bump(^N) receives a PInt, an int * in C, and increments N to 2. Counter matches `struct Counter {int *}`; bump_counter(^C) increments through C.Target, which points to N, giving 3. get_shared(^R) receives a struct Sample ** and stores the address of the shared struct into R, so R^ shows tag G and the count 9 written earlier: `G 9`. diff --git a/tests/i/extern-c-struct/program.cps b/tests/i/extern-c-struct/program.cps index bcb7af4..62370d3 100644 --- a/tests/i/extern-c-struct/program.cps +++ b/tests/i/extern-c-struct/program.cps @@ -1,4 +1,4 @@ -// Records whose fields are C types pass to C functions through pointer types. +// Records whose fields are C types, and pointer types to C types, pass to C functions. INCLUDE_OBJ "lib.o" TYPE Sample DECLARE Tag : C_CHAR @@ -6,13 +6,25 @@ TYPE Sample DECLARE Count : C_INT ENDTYPE TYPE PSample = ^Sample +TYPE PPSample = ^PSample +TYPE PInt = ^C_INT +TYPE Counter + DECLARE Target : PInt +ENDTYPE +TYPE PCounter = ^Counter EXTERN "C" EXTERN_PROCEDURE scale_sample(PSample, C_INT) EXTERN "C" EXTERN_FUNCTION shared_sample() RETURNS PSample EXTERN "C" EXTERN_FUNCTION shared_count() RETURNS C_INT EXTERN "C" EXTERN_FUNCTION sum_counts(C_INT, ...) RETURNS C_INT +EXTERN "C" EXTERN_PROCEDURE bump(PInt) +EXTERN "C" EXTERN_PROCEDURE bump_counter(PCounter) +EXTERN "C" EXTERN_PROCEDURE get_shared(PPSample) DECLARE S : Sample DECLARE P : PSample DECLARE Q : PSample +DECLARE R : PSample +DECLARE N : C_INT +DECLARE C : Counter S.Tag <- (C_CHAR)'S' S.Value <- (C_DOUBLE)1.5 S.Count <- (C_INT)5 @@ -24,3 +36,10 @@ P^.Count <- (C_INT)9 OUTPUT shared_count() Q <- ^S OUTPUT sum_counts((C_INT)2, P, Q) +N <- (C_INT)1 +CALL bump(^N) +C.Target <- ^N +CALL bump_counter(^C) +OUTPUT N +CALL get_shared(^R) +OUTPUT R^.Tag, " ", R^.Count diff --git a/tests/iii/extern-struct-rejected/case.json b/tests/iii/extern-struct-rejected/case.json index cd7a3dd..905d5ad 100644 --- a/tests/iii/extern-struct-rejected/case.json +++ b/tests/iii/extern-struct-rejected/case.json @@ -15,6 +15,10 @@ { "reason": "Variadic\\ argument\\ 2\\ to\\ printf\\ uses\\ record\\ Person,\\ which\\ is\\ not\\ a\\ C\\ struct:\\ field\\ Name\\ of\\ Person\\ has\\ type\\ STRING", "line": 13 + }, + { + "reason": "EXTERN\\ parameter\\ 1\\ of\\ srand\\ must\\ be\\ a\\ non\\-void\\ C\\ type\\ \\(got\\ PInteger\\)", + "line": 15 } ] } diff --git a/tests/iii/extern-struct-rejected/notes.md b/tests/iii/extern-struct-rejected/notes.md index 650b420..40e3ab8 100644 --- a/tests/iii/extern-struct-rejected/notes.md +++ b/tests/iii/extern-struct-rejected/notes.md @@ -1,5 +1,5 @@ # extern-struct-rejected -Compiler extension: an EXTERN signature accepts a record, through a pointer type or by value, only as a C struct (every field a C type, a C struct or a pointer type to one). Person has a STRING field, so a pointer to it, Person as a by-value parameter, as a by-value result and as a by-value variadic argument are all rejected and the field is named. +Compiler extension: an EXTERN signature accepts a record, through a pointer type or by value, only as a C struct (every field a C type, a C struct or a pointer type to either). Person has a STRING field, so a pointer to it, Person as a by-value parameter, as a by-value result and as a by-value variadic argument are all rejected and the field is named. A pointer type counts as a C type only when its pointee does, so a pointer to INTEGER is rejected too. Require exit 1 and each cause on its EXTERN or call line. diff --git a/tests/iii/extern-struct-rejected/program.cps b/tests/iii/extern-struct-rejected/program.cps index ced6fee..80f965f 100644 --- a/tests/iii/extern-struct-rejected/program.cps +++ b/tests/iii/extern-struct-rejected/program.cps @@ -11,3 +11,5 @@ EXTERN "C" EXTERN_FUNCTION atoi(C_STRING) RETURNS Person EXTERN "C" EXTERN_FUNCTION printf(C_STRING, ...) RETURNS C_INT DECLARE Someone : Person OUTPUT printf((C_STRING)"%d", Someone) +TYPE PInteger = ^INTEGER +EXTERN "C" EXTERN_PROCEDURE srand(PInteger) |
