aboutsummaryrefslogtreecommitdiff
path: root/compiler/identifier.h
blob: 9f8f66481387fd74caab35418c16b5b0cc6cf384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

/*

  identifier.h -- The identifier/namespace/scope data structure.

  Copyright © 2011-2016 Samuel Lidén Borell <samuel@kodafritt.se>

  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