Files
ziran/bootstrap/compiler_declaration/compiler_declaration.h
T
waozi 7910e60542 Splice compile-time source into a module with #insert
`#insert NAME;` at file scope splices the text of a compile-time string
constant into the module where it stands: a literal, a #string body or
the result of `#run` on a pure procedure. The text is checked like the
rest of the file and shares its scope and #scope_file privacy, so a
compile-time procedure can write declarations generics cannot express.

Inserts are evaluated after discovery, under the #run rules and budget,
and their declarations are discovered before the file is parsed. Code
anywhere in the module can use them, including #if conditions and #run
calls above the #insert. The parser reads each insert as an in-memory
load frame. Every line of it keeps the #insert's line, so diagnostics
name the insertion site, and line numbers after the splice stay true.

A value that is not a compile-time string, #insert inside a procedure
body or an #if block, an insert in inserted text, and a misspelled
directive are rejected. The grammar lives in compiler_declaration.zi.
Saved IR holds only checked declarations, so no backend changes.

tests/insert_source_cases.zi builds a program whose Square, Cube and
helpers come from #insert. It runs from source and saved IR on C, Go,
the engine, native code and Wasm, with nine rejections.
2026-11-15 16:00:17 -03:00

347 lines
11 KiB
C

/* Generated by zi2c from compiler_declaration.zi. */
#ifndef ZIRAN_COMPILER_DECLARATION_H
#define ZIRAN_COMPILER_DECLARATION_H
#include <stdbool.h>
#include "zir_string.h"
#include "zir_slice.h"
#include "compiler_text.h"
#include "compiler_source.h"
#include "compiler_scan.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef int32_t DeclarationError;
typedef struct GoMethodDeclaration GoMethodDeclaration;
typedef struct ExportDirective ExportDirective;
typedef struct ParameterRewrite ParameterRewrite;
typedef int32_t ImportMode;
typedef struct ModuleImport ModuleImport;
typedef struct ParameterEntry ParameterEntry;
typedef struct ModuleParameterDirective ModuleParameterDirective;
typedef struct InsertDirective InsertDirective;
typedef struct LibraryDeclaration LibraryDeclaration;
typedef struct MethodSymbol MethodSymbol;
typedef struct UsingFilter UsingFilter;
typedef struct ForeignDeclaration ForeignDeclaration;
typedef struct ForeignTarget ForeignTarget;
typedef struct ResultDeclaration ResultDeclaration;
typedef struct ResultReturn ResultReturn;
typedef struct ResultBinding ResultBinding;
typedef struct SymmetricRewrite SymmetricRewrite;
typedef struct TextRewrite TextRewrite;
typedef struct NameReplacement NameReplacement;
typedef struct PrintRewrite PrintRewrite;
typedef struct StatementOutput StatementOutput;
#ifdef ZIRAN_COMPILER_DECLARATION_H_PRIVATE
typedef struct QuotedText QuotedText;
#endif
enum {
DeclarationError_None = 0,
DeclarationError_ExportQuote = 1,
DeclarationError_ExportName = 2,
DeclarationError_Parameters = 3,
DeclarationError_DefaultValue = 4,
DeclarationError_UsingModifier = 5,
DeclarationError_UsingType = 6,
DeclarationError_UsingName = 7,
DeclarationError_ImportQuote = 8,
DeclarationError_ImportCharacter = 9,
DeclarationError_ImportMode = 10,
DeclarationError_ImportDirSyntax = 11,
DeclarationError_ImportDirPath = 12,
DeclarationError_ImportDirName = 13,
DeclarationError_ImportFileSyntax = 14,
DeclarationError_ImportFilePath = 15,
DeclarationError_ImportFileName = 16,
DeclarationError_ImportAlias = 17,
DeclarationError_ImportAliasLimit = 18,
DeclarationError_ImportTarget = 19,
DeclarationError_ImportPackage = 20,
DeclarationError_ImportUsing = 21,
DeclarationError_LibraryQuote = 22,
DeclarationError_LibraryEnd = 23,
DeclarationError_LibraryCharacter = 24,
DeclarationError_UsingKind = 25,
DeclarationError_UsingList = 26,
DeclarationError_UsingEntry = 27,
DeclarationError_UsingMap = 28,
DeclarationError_ForeignResults = 29,
DeclarationError_ForeignField = 30,
DeclarationError_ForeignDefer = 31,
DeclarationError_ForeignVariadic = 32,
DeclarationError_ForeignReads = 33,
DeclarationError_ForeignType = 34,
DeclarationError_ForeignBody = 35,
DeclarationError_ForeignLibrary = 36,
DeclarationError_ForeignSymbol = 37,
DeclarationError_ForeignEnd = 38,
DeclarationError_ForeignHostRename = 39,
DeclarationError_ForeignGoResults = 40,
DeclarationError_ForeignGoField = 41,
DeclarationError_ForeignGoDefer = 42,
DeclarationError_ForeignGoVariadic = 43,
DeclarationError_ResultCount = 44,
DeclarationError_ResultName = 45,
DeclarationError_ResultType = 46,
DeclarationError_ResultParameters = 47,
DeclarationError_ReturnCount = 48,
DeclarationError_SymmetricParameters = 49,
DeclarationError_SymmetricModifier = 50,
DeclarationError_ProcedureNameArguments = 51,
DeclarationError_BuilderFormat = 52,
DeclarationError_BuilderPercent = 53,
DeclarationError_BuilderArguments = 54,
DeclarationError_BuilderPieces = 55,
DeclarationError_BuilderArgumentCount = 56,
DeclarationError_GoMethodQuote = 57,
DeclarationError_GoMethodDuplicate = 58,
DeclarationError_GoMethodResults = 59,
DeclarationError_GoMethodForeign = 60,
DeclarationError_ProcedureTypeCount = 61,
DeclarationError_ProcedureTypeName = 62,
DeclarationError_ProcedureTypeType = 63,
DeclarationError_ProcedureTypeDefault = 64,
DeclarationError_ImportParameterList = 65,
DeclarationError_ImportParameterMode = 66,
DeclarationError_ParameterListSyntax = 67,
DeclarationError_ParameterName = 68,
DeclarationError_ParameterValue = 69,
DeclarationError_ParameterDuplicate = 70,
DeclarationError_ParameterType = 71,
DeclarationError_ParameterDefault = 72,
DeclarationError_ParameterFit = 73,
DeclarationError_InsertSyntax = 74,
};
struct GoMethodDeclaration {
DeclarationError error;
String name;
bool results;
};
struct ExportDirective {
DeclarationError error;
int32_t kind;
String symbol;
String body;
};
struct ParameterRewrite {
DeclarationError error;
int64_t count;
int32_t parameters;
bool has_defaults;
uint64_t using_parameters;
};
enum {
ImportMode_Module = 0,
ImportMode_File = 1,
ImportMode_Directory = 2,
};
struct ModuleImport {
DeclarationError error;
bool present;
bool named;
bool using_import;
ImportMode mode;
String name;
String target;
String path;
String detail;
bool parameterized;
String parameters;
};
struct ParameterEntry {
DeclarationError error;
bool present;
String name;
String type;
String value;
int64_t next;
};
struct ModuleParameterDirective {
DeclarationError error;
bool present;
String list;
int64_t line;
};
struct InsertDirective {
DeclarationError error;
bool present;
String name;
};
struct LibraryDeclaration {
DeclarationError error;
bool present;
String name;
String target;
};
struct MethodSymbol {
bool valid;
String receiver;
String method;
};
struct UsingFilter {
DeclarationError error;
bool present;
int64_t count;
int64_t next;
};
struct ForeignDeclaration {
DeclarationError error;
bool present;
String name;
String library;
String symbol;
bool is_type;
bool go_results;
bool go_field;
bool go_defer;
bool go_variadic;
bool reads;
};
struct ForeignTarget {
DeclarationError error;
int64_t count;
};
struct ResultDeclaration {
DeclarationError error;
int32_t count;
String types[16];
int32_t parameter_count;
String parameters[64];
};
struct ResultReturn {
DeclarationError error;
bool present;
int32_t values;
int64_t count;
};
struct ResultBinding {
bool present;
int32_t count;
bool inferred;
String targets[16];
String value;
};
struct SymmetricRewrite {
DeclarationError error;
bool present;
bool needed;
int64_t header_count;
int64_t count;
};
struct TextRewrite {
DeclarationError error;
bool changed;
int64_t count;
};
struct NameReplacement {
String source;
String target;
};
struct PrintRewrite {
DeclarationError error;
int32_t statements;
int64_t lengths[64];
};
struct StatementOutput {
Slice bytes;
};
#ifdef ZIRAN_COMPILER_DECLARATION_H_PRIVATE
struct QuotedText {
bool valid;
String value;
int64_t end;
};
#endif
String compiler_declaration_ErrorText(DeclarationError error);
GoMethodDeclaration compiler_declaration_ParseGoMethod(String source, int64_t name_limit);
UsingFilter compiler_declaration_UsingModifierClause(String source, Slice output, int64_t name_limit);
ExportDirective compiler_declaration_ProgramExport(String source, int64_t symbol_limit);
ParameterRewrite compiler_declaration_RewriteParameters(String source, Slice output, bool strip_defaults, bool strip_using, int64_t name_limit);
ParameterRewrite compiler_declaration_RewriteProcedureTypeParameters(String source, Slice output, int64_t name_limit);
ModuleImport compiler_declaration_ImportDeclaration(String source, int64_t name_limit, int64_t path_limit);
String compiler_declaration_ParameterLiteralType(String value);
bool compiler_declaration_ParameterTypeName(String type);
bool compiler_declaration_ParameterFits(String type, String value);
int64_t compiler_declaration_ParameterCanonical(String value, Slice output);
ParameterEntry compiler_declaration_NextParameter(String list, int64_t at, bool declaration, int64_t name_limit);
DeclarationError compiler_declaration_ParameterListError(String list, bool declaration, int64_t name_limit);
InsertDirective compiler_declaration_InsertDeclaration(String source, int64_t name_limit);
ModuleParameterDirective compiler_declaration_ModuleParametersPrefix(String source, int64_t name_limit);
LibraryDeclaration compiler_declaration_SystemLibrary(String source, int64_t name_limit, int64_t path_limit);
MethodSymbol compiler_declaration_ForeignMethod(String source, int64_t receiver_limit, int64_t method_limit);
ForeignDeclaration compiler_declaration_ParseForeign(String source, int64_t name_limit);
ForeignTarget compiler_declaration_ResolveForeignTarget(ForeignDeclaration declaration, String library, Slice output, int64_t name_limit);
bool compiler_declaration_ForeignVarargs(String parameters);
ResultDeclaration compiler_declaration_ParseResults(String source, String template_parameters);
int64_t compiler_declaration_ResultsRecord(ResultDeclaration results, Slice output);
int64_t compiler_declaration_ResultsParameters(ResultDeclaration results, Slice output);
int64_t compiler_declaration_ResultsBody(ResultDeclaration results, Slice output);
ResultReturn compiler_declaration_RewriteResultReturn(String source, String record, int32_t results, Slice output);
ResultBinding compiler_declaration_MultipleBinding(String source, int32_t maximum, int64_t name_limit);
bool compiler_declaration_DefaultScopeIndependent(String source, int64_t token_limit);
TextRewrite compiler_declaration_RewriteProcedureName(String source, String name, Slice output);
TextRewrite compiler_declaration_RewriteLocalNames(String source, Slice names, Slice output);
SymmetricRewrite compiler_declaration_RewriteSymmetric(String source, Slice header_output, Slice wrapper_output, int64_t name_limit);
PrintRewrite compiler_declaration_BuilderPrintPieces(String source, Slice output, int32_t maximum);
/* Small procedures other modules inline; compiler_declaration.c defines them. */
#if !defined(__cplusplus) && !defined(ZIRAN_COMPILER_DECLARATION_H_SOURCE) && !defined(ZIRAN_NO_HEADER_INLINE)
#include <stdbool.h>
#include "zir_string.h"
static inline DeclarationError compiler_declaration_ParameterListError_ziran_inline(String list, bool declaration, int64_t name_limit);
#define compiler_declaration_ParameterListError(...) compiler_declaration_ParameterListError_ziran_inline(__VA_ARGS__)
static inline DeclarationError
compiler_declaration_ParameterListError_ziran_inline(String list, bool declaration, int64_t name_limit)
{
int64_t at = 0LL;
while (at >= 0LL) {
ParameterEntry entry = compiler_declaration_NextParameter(list, at, declaration, name_limit);
if (((DeclarationError)(entry.error)) != DeclarationError_None) {
return (DeclarationError)(entry.error);
}
int64_t earlier = 0LL;
while (earlier >= 0LL && earlier < at) {
ParameterEntry previous = compiler_declaration_NextParameter(list, earlier, declaration, name_limit);
if (StringEqual(previous.name, entry.name)) {
return DeclarationError_ParameterDuplicate;
}
earlier = previous.next;
}
at = entry.next;
}
return DeclarationError_None;
}
#endif
#ifdef __cplusplus
}
#endif
#endif /* ZIRAN_COMPILER_DECLARATION_H */