/* identifier.h -- The identifier/namespace/scope data structure. Copyright © 2011-2016 Samuel Lidén Borell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef LRL_IDENTIFIER_H #define LRL_IDENTIFIER_H #include "context.h" #include "tokenizer.h" typedef enum { LRL_IdFl_NotLoaded = 0x1, /* Not yet fully loaded from disk */ LRL_IdFl_Link = 0x2, /* This is a link to another namespace */ LRL_IdFl_HasHere = 0x4, /* The "here" keyword can be used to refer to this namespace */ LRL_IdFl_Statement = 0x8, /* Scope of an LRLASTStmt */ LRL_IdFl_Implementation = 0x10, /* Implementation namespace of a module */ LRL_IdFl_EnumValue = 0x20, /* Identifier of an enumeration value */ LRL_IdFl_StructMember = 0x40, /* Identifier of a struct member */ LRL_IdFl_Uninitialized = 0x80, /* Identifier of an uninitialized interop statement */ LRL_IdFl_FunctionBody = 0x100, /* Outermost body scope of a function */ LRL_IdFl_FromInterop = 0x200, /* Generated by an interop. Checked for by the C backend to avoid duplicated idents of the C standards library. */ LRL_IdFl_Broken = 0x400, /* The subtree of this identifier could not be loaded (e.g. due to an interop error). An error has been reported already, so suppress errors regarding subidentifiers. */ LRL_IdFl_TypedefAnon = 0x800, /* Anonymous scope of a type */ LRL_IdFl_TypedefParam = 0x1000, /* Type parameter */ LRL_IdFl_Typedef = 0x2000 /* Namespace of a typedef */ } LRLIdentFlags; /* should be private? */ /* should it be included directly in the AST tree nodes? */ typedef struct LRLIdent { LRLHashCode hash; struct LRLIdent *next; LRLIdentFlags flags; /* Definition */ const LRLToken *def_token; union LRLASTDefOrStmt *def_node; /* Name, if this identifier doesn't have a token */ char *name; /* Filesystem path (optional) */ char *path; /* Containing scope */ const struct LRLIdent *scope; /* Linkage name to use (default is to use parentnamespace_name) */ const LRLToken *linkname; /* Namespaces which are made visible with "uses" in this namespace */ /* TODO perhaps this can be removed, and implemented by creating an identifier+subidentifiers at the "uses" statement. In that case there has to be special handling so "uses a:b" doesn't hide the "a" namespace. */ size_t num_visible; const struct LRLIdent **visible; /* Hashmap with contents of this namespace */ struct { size_t num_buckets, key_bits, size; struct LRLIdent **buckets; } contents; } LRLIdent; #define LRL_IDENT_MISSING ((LRLIdent*)-1) #define LRL_IDENT_PLAIN(name) \ { 0, NULL, 0, NULL, NULL, (name), \ NULL, NULL, NULL, 0, NULL, { 0, 0, 0, NULL } } typedef struct LRLIdentRef { const LRLIdent *ident; const LRLToken *first_token; const LRLIdent *scope; struct LRLIdentRef *next; } LRLIdentRef; #define LRL_IDENTREF_NEW ((LRLIdentRef*)-1) typedef enum { LRL_Ident_Find, /* Look up an identifier. */ LRL_Ident_FindMember,/* Finds an identifier in a struct */ LRL_Ident_FindLocal, /* Find duplicates in {} block in a function. */ LRL_Ident_FindUses, /* Make an external identifier visible. */ LRL_Ident_Create, /* Create an identifier, fail if it exists. */ LRL_Ident_CreateMember, /* Like above, but disallow namespacing */ LRL_Ident_Extend /* Create or re-use an identifier. */ } LRLIdentOperation; #define lrl_ident_valid(ident) ((ident) && (ident) != LRL_IDENT_MISSING) LRLIdent *lrl_ident_create_root(void); LRLIdent *lrl_ident_insert_string(LRLCtx *ctx, LRLIdent *scope, char *name); LRLIdent *lrl_ident_insert_path(LRLCtx *ctx, LRLIdent *root, const char *path); LRLIdent *lrl_ident_create_priv_scope(const LRLIdent *scope); LRLIdent *lrl_ident_get(LRLCtx *ctx, LRLIdent *scope, const LRLToken *tokens, const LRLIdentOperation op, LRLIdent *newident); LRLIdent *lrl_ident_get_string(LRLIdent *scope, const char *name); LRLIdent *lrl_ident_deref_links(LRLIdent *ident); void lrl_ident_check_duplicates(LRLCtx *ctx, const LRLIdent *scope, const LRLToken *token); int lrl_ident_names_equal(const LRLIdent *a, const LRLIdent *b); int lrl_identref_queue(LRLCtx *ctx, LRLIdentRef *identref); void lrl_ident_defer(LRLCtx *ctx, LRLIdentRef *identref); int lrl_ident_lookup_deferred(LRLCtx *ctx); void lrl_ident_defer_uses(LRLCtx *ctx, LRLIdentRef *identref); int lrl_ident_lookup_uses_entries(LRLCtx *ctx); int lrl_ident_has_deferred(LRLCtx *ctx); #endif