diff --git a/ast/statements/VarInit.h b/ast/statements/VarInit.h index 2bff0008..d3c62400 100644 --- a/ast/statements/VarInit.h +++ b/ast/statements/VarInit.h @@ -27,6 +27,11 @@ struct VarInitAttributes { */ bool is_comptime; + /** + * compiler declarations are present inside the compiler, no need to import + */ + bool is_compiler_decl; + /** * has moved is used to indicate that an object at this location has moved * destructor is not called on moved objects, once moved, any attempt to access @@ -88,7 +93,7 @@ class VarInitStatement : public AnnotableNode { ASTNode* parent_node, SourceLocation location, AccessSpecifier specifier = AccessSpecifier::Internal - ) : attrs(specifier, false, false, false, is_const, is_reference, false), located_id(identifier), type(type), value(value), parent_node(parent_node), location(location) { + ) : attrs(specifier, false, false, false, false, is_const, is_reference, false), located_id(identifier), type(type), value(value), parent_node(parent_node), location(location) { } @@ -136,6 +141,15 @@ class VarInitStatement : public AnnotableNode { attrs.is_comptime = value; } + inline bool is_compiler_decl() { + return attrs.is_compiler_decl; + } + + inline void set_compiler_decl(bool value) { + attrs.is_comptime = value; + attrs.is_compiler_decl = value; + } + /** * get the access specifier */ diff --git a/ast/structures/FunctionDeclaration.h b/ast/structures/FunctionDeclaration.h index d8294806..98f9f134 100644 --- a/ast/structures/FunctionDeclaration.h +++ b/ast/structures/FunctionDeclaration.h @@ -36,6 +36,11 @@ struct FuncDeclAttributes { * is this function comptime */ bool is_comptime; + /** + * compiler functions are present inside the compiler + * like compiler::println + */ + bool is_compiler_decl; /** * when involved in multi function node (due to same name, different parameters) */ @@ -249,7 +254,7 @@ class FunctionDeclaration : public AnnotableNode, public FunctionType { bool signature_resolved = false ) : FunctionType(std::move(params), returnType, isVariadic, false, parent_node, location, signature_resolved), identifier(identifier), body(std::move(body)), location(location), - attrs(specifier, false, 0, false, false, false, false, false, false, false, false, false, false) { + attrs(specifier, false, false, 0, false, false, false, false, false, false, false, false, false, false) { } /** @@ -265,7 +270,7 @@ class FunctionDeclaration : public AnnotableNode, public FunctionType { bool signature_resolved = false ) : FunctionType(returnType, isVariadic, false, parent_node, location, signature_resolved), identifier(identifier), location(location), - attrs(specifier, false, 0, false, false, false, false, false, false, false, false, false, false) { + attrs(specifier, false, false, 0, false, false, false, false, false, false, false, false, false, false) { } /** @@ -287,6 +292,15 @@ class FunctionDeclaration : public AnnotableNode, public FunctionType { attrs.is_comptime = value; } + inline bool is_compiler_decl() { + return attrs.is_compiler_decl; + } + + inline void set_compiler_decl(bool value) { + attrs.is_comptime = value; + attrs.is_compiler_decl = value; + } + inline void set_specifier_fast(AccessSpecifier specifier) { attrs.specifier = specifier; } diff --git a/ast/structures/Namespace.cpp b/ast/structures/Namespace.cpp index b7cae688..a7c6fb5f 100644 --- a/ast/structures/Namespace.cpp +++ b/ast/structures/Namespace.cpp @@ -3,15 +3,6 @@ #include "Namespace.h" #include "compiler/SymbolResolver.h" -Namespace::Namespace( - LocatedIdentifier identifier, - ASTNode* parent_node, - SourceLocation location, - AccessSpecifier specifier -) : identifier(identifier), parent_node(parent_node), location(location), attrs(specifier, false) { - -} - void Namespace::declare_node(SymbolResolver& linker, ASTNode* node, const chem::string_view& node_id) { auto found = extended.find(node_id); if(found == extended.end()) { diff --git a/ast/structures/Namespace.h b/ast/structures/Namespace.h index bb39a3f6..5e4833ac 100644 --- a/ast/structures/Namespace.h +++ b/ast/structures/Namespace.h @@ -19,6 +19,11 @@ struct NamespaceDeclAttributes { */ bool is_comptime; + /** + * compiler declarations are present inside the compiler, no need to import + */ + bool is_compiler_decl; + /** * is namespace deprecated */ @@ -51,7 +56,9 @@ class Namespace : public AnnotableNode { ASTNode* parent_node, SourceLocation location, AccessSpecifier specifier = AccessSpecifier::Internal - ); + ) : identifier(identifier), parent_node(parent_node), location(location), attrs(specifier, false, false) { + + } /** * get the name of node @@ -83,6 +90,15 @@ class Namespace : public AnnotableNode { attrs.is_comptime = value; } + inline bool is_compiler_decl() { + return attrs.is_compiler_decl; + } + + inline void set_compiler_decl(bool value) { + attrs.is_comptime = value; + attrs.is_compiler_decl = value; + } + inline bool deprecated() { return attrs.deprecated; } diff --git a/ast/structures/StructDefinition.cpp b/ast/structures/StructDefinition.cpp index ad0cc8a0..18b46a89 100644 --- a/ast/structures/StructDefinition.cpp +++ b/ast/structures/StructDefinition.cpp @@ -347,17 +347,6 @@ BaseTypeKind StructMember::type_kind() const { return type->kind(); } -StructDefinition::StructDefinition( - LocatedIdentifier identifier, - ASTNode* parent_node, - SourceLocation location, - AccessSpecifier specifier -) : ExtendableMembersContainerNode(std::move(identifier)), parent_node(parent_node), - location(location), attrs(specifier, false, false, false, false, false, false, false), - linked_type(identifier.identifier, this, location) { - -} - //BaseType *StructDefinition::copy(ASTAllocator& allocator) const { // return new (allocator.allocate()) LinkedType(name_view(), (ASTNode *) this, location); //} diff --git a/ast/structures/StructDefinition.h b/ast/structures/StructDefinition.h index 71d4e24c..ca54901e 100644 --- a/ast/structures/StructDefinition.h +++ b/ast/structures/StructDefinition.h @@ -28,6 +28,11 @@ struct StructDeclAttributes { */ bool is_comptime; + /** + * is a compiler declaration (present inside the compiler, no need to import) + */ + bool is_compiler_decl; + /** * is direct initialization * constructor or de constructor allow functions to be called automatically @@ -90,7 +95,11 @@ class StructDefinition : public ExtendableMembersContainerNode { ASTNode* parent_node, SourceLocation location, AccessSpecifier specifier = AccessSpecifier::Internal - ); + ) : ExtendableMembersContainerNode(std::move(identifier)), parent_node(parent_node), + location(location), attrs(specifier, false, false, false, false, false, false, false, false), + linked_type(identifier.identifier, this, location) { + + } /** * get the name of node @@ -119,6 +128,15 @@ class StructDefinition : public ExtendableMembersContainerNode { attrs.is_comptime = value; } + inline bool is_compiler_decl() { + return attrs.is_compiler_decl; + } + + inline void set_compiler_decl(bool value) { + attrs.is_comptime = value; + attrs.is_compiler_decl = value; + } + inline bool is_direct_init() { return attrs.is_direct_init; } diff --git a/ast/utils/GlobalFunctions.cpp b/ast/utils/GlobalFunctions.cpp index a9f84e56..26c46531 100644 --- a/ast/utils/GlobalFunctions.cpp +++ b/ast/utils/GlobalFunctions.cpp @@ -269,7 +269,7 @@ namespace InterpretVector { typeParam("T", nullptr, nullptr, this, 0, ZERO_LOC), selfType("vector", this, ZERO_LOC), selfReference(&selfType, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); generic_params.emplace_back(&typeParam); insert_func(&constructorFn); insert_func(&sizeFn); @@ -406,7 +406,7 @@ class InterpretSize : public FunctionDeclaration { AccessSpecifier::Public, true ), returnType(ZERO_LOC), anyType(ZERO_LOC), valueParam("value", &anyType, 0, nullptr, false, this, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); params.emplace_back(&valueParam); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { @@ -543,7 +543,7 @@ class InterpretWrap : public FunctionDeclaration { ), anyType(ZERO_LOC), valueParam("value", &anyType, 0, nullptr, false, this, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); // having a generic type parameter T requires that user gives type during function call to wrap // when we can successfully avoid giving type for generic parameters in functions, we should do this // generic_params.emplace_back(new (allocator.allocate()) GenericTypeParameter("T", nullptr, this)); @@ -572,7 +572,7 @@ class InterpretUnwrap : public FunctionDeclaration { AccessSpecifier::Public, true ), anyType(ZERO_LOC), valueParam("value", &anyType, 0, nullptr, false, this, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); // having a generic type parameter T requires that user gives type during function call to wrap // when we can successfully avoid giving type for generic parameters in functions, we should do this // generic_params.emplace_back(new (allocator.allocate()) GenericTypeParameter("T", nullptr, this)); @@ -600,7 +600,7 @@ class InterpretRetStructPtr : public FunctionDeclaration { AccessSpecifier::Public, true ), voidType(ZERO_LOC), ptrType(&voidType, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { return new (allocator.allocate()) RetStructParamValue(ZERO_LOC); @@ -621,7 +621,7 @@ class InterpretCompilerVersion : public FunctionDeclaration { AccessSpecifier::Public, true ), stringType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { return new (allocator.allocate()) StringValue(VERSION_STRING, ZERO_LOC); @@ -642,7 +642,7 @@ class InterpretIsTcc : public FunctionDeclaration { AccessSpecifier::Public, true ), boolType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { #ifdef TCC_BUILD @@ -667,7 +667,7 @@ class InterpretIsClang : public FunctionDeclaration { AccessSpecifier::Public, true ), boolType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { #ifdef COMPILER_BUILD @@ -692,7 +692,7 @@ class InterpretGetRawLocation : public FunctionDeclaration { AccessSpecifier::Public, true ), uIntType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { return new (allocator.allocate()) UBigIntValue(call->location.encoded, call->location); @@ -714,7 +714,7 @@ class InterpretGetLineNo : public FunctionDeclaration { AccessSpecifier::Public, true ), uIntType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { const auto loc = call_scope->global->loc_man.getLocation(call->location); @@ -737,7 +737,7 @@ class InterpretGetCharacterNo : public FunctionDeclaration { AccessSpecifier::Public, true ), uIntType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { const auto loc = call_scope->global->loc_man.getLocation(call->location); @@ -769,7 +769,7 @@ class InterpretGetCallerLineNo : public FunctionDeclaration { AccessSpecifier::Public, true ), uIntType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { const auto global = call_scope->global;; @@ -798,7 +798,7 @@ class InterpretGetCallerCharacterNo : public FunctionDeclaration { AccessSpecifier::Public, true ), uIntType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *self_call, Value *parent_val, bool evaluate_refs) final { const auto global = call_scope->global;; @@ -829,7 +829,7 @@ class InterpretDefined : public FunctionDeclaration { AccessSpecifier::Public, true ), boolType(ZERO_LOC), stringType(ZERO_LOC), valueParam("value", &stringType, 0, nullptr, false, this, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); params.emplace_back(&valueParam); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { @@ -859,7 +859,7 @@ class InterpretError : public FunctionDeclaration { AccessSpecifier::Public, true ), voidType(ZERO_LOC), stringType(ZERO_LOC), valueParam("value", &stringType, 0, nullptr, false, this, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); params.emplace_back(&valueParam); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { @@ -889,7 +889,7 @@ class InterpretSatisfies : public FunctionDeclaration { ), returnType(ZERO_LOC), anyType(ZERO_LOC), valueParam("value", &anyType, 0, nullptr, false, this, ZERO_LOC), valueParam2("value2", &anyType, 1, nullptr, false, this, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); params.emplace_back(&valueParam); params.emplace_back(&valueParam2); } @@ -934,7 +934,7 @@ class InterpretIsPtrNull : public FunctionDeclaration { ), boolType(ZERO_LOC), nullVal(ZERO_LOC), anyType(ZERO_LOC), ptrType(&anyType, ZERO_LOC), valueParam("value", &ptrType, 0, nullptr, false, this, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); params.emplace_back(&valueParam); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { @@ -963,7 +963,7 @@ class InterpretIsPtrNotNull : public FunctionDeclaration { ), boolType(ZERO_LOC), nullVal(ZERO_LOC), anyType(ZERO_LOC), ptrType(&anyType, ZERO_LOC), valueParam("value", &ptrType, 0, nullptr, false, this, ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); params.emplace_back(&valueParam); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { @@ -989,7 +989,7 @@ class InterpretMemCopy : public FunctionDeclaration { true ), boolType(ZERO_LOC), stringType(ZERO_LOC), destValueParam("dest_value", &stringType, 0, nullptr, false, this, ZERO_LOC), sourceValueParam("source_value", &stringType, 1, nullptr, false, this, ZERO_LOC){ - set_comptime(true); + set_compiler_decl(true); params.emplace_back(&destValueParam); params.emplace_back(&sourceValueParam); } @@ -1017,7 +1017,7 @@ class InterpretGetTarget : public FunctionDeclaration { AccessSpecifier::Public, true ), stringType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { auto& triple = call_scope->global->target_triple; @@ -1040,7 +1040,7 @@ class InterpretGetCurrentFilePath : public FunctionDeclaration { AccessSpecifier::Public, true ), stringType(ZERO_LOC) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { auto& loc_man = call_scope->global->loc_man; @@ -1071,7 +1071,7 @@ class InterpretGetChildFunction : public FunctionDeclaration { true ), param("value", &anyType, 0, nullptr, false, this, 0), methodParam("method", &strType, 1, nullptr, false, this, 0), anyType(0), strType(0) { - set_comptime(true); + set_compiler_decl(true); } Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { @@ -1148,7 +1148,7 @@ class InterpretTypeToString : public FunctionDeclaration { // AccessSpecifier::Public, // true // ), stringType(ZERO_LOC) { -// set_comptime(true); +// set_compiler_decl(true); // } // Value *call(InterpretScope *call_scope, ASTAllocator& allocator, FunctionCall *call, Value *parent_val, bool evaluate_refs) final { // auto& loc_man = call_scope->global->loc_man; @@ -1168,7 +1168,7 @@ class InterpretTypeToString : public FunctionDeclaration { // parent_node, // AccessSpecifier::Public // ) { -// set_comptime(true); +// set_compiler_decl(true); // params.emplace_back(std::make_unique("ptr", std::make_unique(std::make_unique()), 0, std::nullopt, this)); // params.emplace_back(std::make_unique("value", std::make_unique(), 1, std::nullopt, this)); // } @@ -1211,7 +1211,7 @@ class CompilerNamespace : public Namespace { isTccFn(this), isClangFn(this), sizeFn(this), vectorNode(this), satisfiesFn(this), get_target_fn(this), get_current_file_path(this), get_child_fn(this), error_fn(this) { - set_comptime(true); + set_compiler_decl(true); nodes = { &printFn, &printlnFn, &to_stringFn, &type_to_stringFn, &wrapFn, &unwrapFn, &retStructPtr, &verFn, &isTccFn, &isClangFn, &sizeFn, &vectorNode, &satisfiesFn, &get_raw_location, &get_line_no, &get_char_no, &get_caller_line_no, &get_caller_char_no, @@ -1229,7 +1229,7 @@ class MemNamespace : public Namespace { explicit MemNamespace( ASTNode* parent_node ) : Namespace(ZERO_LOC_ID("mem"), parent_node, ZERO_LOC, AccessSpecifier::Public), memCopyFn(this) { - set_comptime(true); + set_compiler_decl(true); nodes = { &memCopyFn }; } @@ -1246,7 +1246,7 @@ class PtrNamespace : public Namespace { ) : Namespace(ZERO_LOC_ID("ptr"), parent_node, ZERO_LOC, AccessSpecifier::Public), isNullFn(this), isNotNullFn(this) { - set_comptime(true); + set_compiler_decl(true); nodes = { &isNullFn, &isNotNullFn }; } @@ -1264,7 +1264,7 @@ class StdNamespace : public Namespace { ) : Namespace(ZERO_LOC_ID("std"), nullptr, ZERO_LOC, AccessSpecifier::Public), memNamespace(this), ptrNamespace(this) { - set_comptime(true); + set_compiler_decl(true); nodes = { &memNamespace, &ptrNamespace }; } @@ -1276,7 +1276,7 @@ class DefDecl : public StructDefinition { DefDecl() : StructDefinition( ZERO_LOC_ID("Def"), nullptr, ZERO_LOC, AccessSpecifier::Public ) { - set_comptime(true); + set_compiler_decl(true); } }; @@ -1303,7 +1303,7 @@ struct DefThing { VarInitStatement defStmt; DefThing() : defValue(&decl), defStmt(true, false, ZERO_LOC_ID("def"), defValue.refType, &defValue, nullptr, ZERO_LOC, AccessSpecifier::Public) { - defStmt.set_comptime(true); + defStmt.set_compiler_decl(true); } void declare_value(ASTAllocator& allocator, const chem::string_view& name, BaseType* type, Value* value) {