]> git.sur5r.net Git - cc65/blob - src/ca65/symbol.c
Allow conditional directives within .STRUCT7:UNION and .ENUM
[cc65] / src / ca65 / symbol.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 symbol.c                                  */
4 /*                                                                           */
5 /*                   Parse a symbol name and search for it                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstraße 52                                              */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* ca65 */
39 #include "error.h"
40 #include "nexttok.h"
41 #include "scanner.h"
42 #include "symbol.h"
43 #include "symtab.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Data                                    */
49 /*****************************************************************************/
50
51
52
53 /*****************************************************************************/
54 /*                                   Code                                    */
55 /*****************************************************************************/
56
57
58
59 SymEntry* ParseScopedSymName (int AllocNew)
60 /* Parse a (possibly scoped) symbol name, search for it in the symbol table
61  * and return the symbol table entry.
62  */
63 {
64     /* Get the starting table */
65     SymTable* Scope;
66     if (Tok == TOK_NAMESPACE) {
67         Scope = RootScope;
68         NextTok ();
69     } else {
70         Scope = CurrentScope;
71         /* ### Need to walk up the tree */
72     }
73
74     /* Resolve scopes */
75     while (1) {
76
77         /* An identifier must follow. Remember and skip it. */
78         char Name[sizeof (SVal)];
79         if (Tok != TOK_IDENT) {
80             Error ("Identifier expected");
81             return 0;
82         }
83         strcpy (Name, SVal);
84         NextTok ();
85
86         /* If the next token is a namespace token, handle the name as the
87          * name of a scope, otherwise it's the name of a symbol in that
88          * scope.
89          */
90
91         if (Tok == TOK_NAMESPACE) {
92
93             /* Search for the child scope */
94             Scope = SymFindScope (Scope, Name, AllocNew);
95
96             /* Skip the namespace token */
97             NextTok ();
98
99             /* If we didn't find the scope, bail out */
100             if (Scope == 0) {
101                 return 0;
102             }
103
104         } else {
105
106             /* Search for the symbol and return it */
107             return SymFind (Scope, Name, AllocNew);
108
109         }
110     }
111 }
112
113
114
115 SymTable* ParseScopedSymTable (int AllocNew)
116 /* Parse a (possibly scoped) symbol table (scope) name, search for it in the
117  * symbol space and return the symbol table struct.
118  */
119 {
120     /* Get the starting table */
121     SymTable* Scope;
122     if (Tok == TOK_NAMESPACE) {
123         Scope = RootScope;
124         NextTok ();
125     } else {
126         Scope = CurrentScope;
127         if (Tok != TOK_IDENT) {
128             Error ("Identifier expected");
129             return Scope;
130         }
131
132         /* If no new scope should be allocated, the scope may specify any
133          * scope in any of the parent scopes, so search for it.
134          */
135         if (!AllocNew) {
136             Scope = SymFindAnyScope (Scope, SVal);
137             NextTok ();
138             if (Tok != TOK_NAMESPACE) {
139                 return Scope;
140             }
141             NextTok ();
142         }
143     }
144
145     /* Resolve scopes. */
146     while (Tok == TOK_IDENT) {
147
148         /* Search for the child scope if we have a valid parent */
149         if (Scope) {
150             Scope = SymFindScope (Scope, SVal, AllocNew);
151         }
152
153         /* Skip the name token */
154         NextTok ();
155
156         /* If a namespace token follows, read on, otherwise bail out */
157         if (Tok == TOK_NAMESPACE) {
158             NextTok ();
159             if (Tok != TOK_IDENT) {
160                 Error ("Identifier expected");
161             }
162         } else {
163             break;
164         }
165     }
166
167     /* Return the scope we found or created */
168     return Scope;
169 }
170
171
172