diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-22 13:56:44 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-22 13:56:44 +0000 |
| commit | 312a19c781da1c31ff1e3dc71c319663a8be9148 (patch) | |
| tree | 80d63213c0d11b1fcf3d67ef6e51289fe4b5096e /lib | |
| parent | 5eccd90b47b327917ad0e5858c79ca8fab27cd74 (diff) | |
feat(codegen): accept pointer types to any C type in EXTERN
A pseudocode pointer type now counts as a C type, in EXTERN signatures
and as a C-struct field, when it finally points to a non-void C type or a
C struct, as in TYPE PInt = ^C_INT. A pointer cycle such as TYPE P = ^P
and a pointer to a pseudocode type stay rejected.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/CodeGen/CallGen.cc | 3 | ||||
| -rw-r--r-- | lib/CodeGen/FunctionGen.cc | 3 | ||||
| -rw-r--r-- | lib/CodeGen/TypeSystem.cc | 24 |
3 files changed, 18 insertions, 12 deletions
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 { |
