diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/lscript/lscript_compile/lscript_scope.h | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/lscript/lscript_compile/lscript_scope.h')
-rw-r--r-- | linden/indra/lscript/lscript_compile/lscript_scope.h | 407 |
1 files changed, 407 insertions, 0 deletions
diff --git a/linden/indra/lscript/lscript_compile/lscript_scope.h b/linden/indra/lscript/lscript_compile/lscript_scope.h new file mode 100644 index 0000000..ff20ad3 --- /dev/null +++ b/linden/indra/lscript/lscript_compile/lscript_scope.h | |||
@@ -0,0 +1,407 @@ | |||
1 | /** | ||
2 | * @file lscript_scope.h | ||
3 | * @brief builds nametable and checks scope | ||
4 | * | ||
5 | * Copyright (c) 2002-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #ifndef LL_LSCRIPT_SCOPE_H | ||
29 | #define LL_LSCRIPT_SCOPE_H | ||
30 | |||
31 | #include "string_table.h" | ||
32 | #include "llmap.h" | ||
33 | #include "lscript_byteformat.h" | ||
34 | |||
35 | typedef enum e_lscript_identifier_type | ||
36 | { | ||
37 | LIT_INVALID, | ||
38 | LIT_GLOBAL, | ||
39 | LIT_VARIABLE, | ||
40 | LIT_FUNCTION, | ||
41 | LIT_LABEL, | ||
42 | LIT_STATE, | ||
43 | LIT_HANDLER, | ||
44 | LIT_LIBRARY_FUNCTION, | ||
45 | LIT_EOF | ||
46 | } LSCRIPTIdentifierType; | ||
47 | |||
48 | const char LSCRIPTFunctionTypeStrings[LST_EOF] = | ||
49 | { | ||
50 | '0', | ||
51 | 'i', | ||
52 | 'f', | ||
53 | 's', | ||
54 | 'k', | ||
55 | 'v', | ||
56 | 'q', | ||
57 | 'l', | ||
58 | '0' | ||
59 | }; | ||
60 | |||
61 | const char * const LSCRIPTListDescription[LST_EOF] = | ||
62 | { | ||
63 | "PUSHARGB 0", | ||
64 | "PUSHARGB 1", | ||
65 | "PUSHARGB 2", | ||
66 | "PUSHARGB 3", | ||
67 | "PUSHARGB 4", | ||
68 | "PUSHARGB 5", | ||
69 | "PUSHARGB 6", | ||
70 | "PUSHARGB 7", | ||
71 | "PUSHARGB 0" | ||
72 | }; | ||
73 | |||
74 | const char * const LSCRIPTTypePush[LST_EOF] = | ||
75 | { | ||
76 | "INVALID", | ||
77 | "PUSHE", | ||
78 | "PUSHE", | ||
79 | "PUSHE", | ||
80 | "PUSHE", | ||
81 | "PUSHEV", | ||
82 | "PUSHEQ", | ||
83 | "PUSHE", | ||
84 | "undefined" | ||
85 | }; | ||
86 | |||
87 | const char * const LSCRIPTTypeReturn[LST_EOF] = | ||
88 | { | ||
89 | "INVALID", | ||
90 | "LOADP -12", | ||
91 | "LOADP -12", | ||
92 | "STORES -12\nPOP", | ||
93 | "STORES -12\nPOP", | ||
94 | "LOADVP -20", | ||
95 | "LOADQP -24", | ||
96 | "LOADLP -12", | ||
97 | "undefined" | ||
98 | }; | ||
99 | |||
100 | const char * const LSCRIPTTypePop[LST_EOF] = | ||
101 | { | ||
102 | "INVALID", | ||
103 | "POP", | ||
104 | "POP", | ||
105 | "POPS", | ||
106 | "POPS", | ||
107 | "POPV", | ||
108 | "POPQ", | ||
109 | "POPL", | ||
110 | "undefined" | ||
111 | }; | ||
112 | |||
113 | const char * const LSCRIPTTypeDuplicate[LST_EOF] = | ||
114 | { | ||
115 | "INVALID", | ||
116 | "DUP", | ||
117 | "DUP", | ||
118 | "DUPS", | ||
119 | "DUPS", | ||
120 | "DUPV", | ||
121 | "DUPQ", | ||
122 | "DUPL", | ||
123 | "undefined" | ||
124 | }; | ||
125 | |||
126 | const char * const LSCRIPTTypeLocalStore[LST_EOF] = | ||
127 | { | ||
128 | "INVALID", | ||
129 | "STORE ", | ||
130 | "STORE ", | ||
131 | "STORES ", | ||
132 | "STORES ", | ||
133 | "STOREV ", | ||
134 | "STOREQ ", | ||
135 | "STOREL ", | ||
136 | "undefined" | ||
137 | }; | ||
138 | |||
139 | const char * const LSCRIPTTypeLocalDeclaration[LST_EOF] = | ||
140 | { | ||
141 | "INVALID", | ||
142 | "STOREP ", | ||
143 | "STOREP ", | ||
144 | "STORESP ", | ||
145 | "STORESP ", | ||
146 | "STOREVP ", | ||
147 | "STOREQP ", | ||
148 | "STORELP ", | ||
149 | "undefined" | ||
150 | }; | ||
151 | |||
152 | const char * const LSCRIPTTypeGlobalStore[LST_EOF] = | ||
153 | { | ||
154 | "INVALID", | ||
155 | "STOREG ", | ||
156 | "STOREG ", | ||
157 | "STORESG ", | ||
158 | "STORESG ", | ||
159 | "STOREGV ", | ||
160 | "STOREGQ ", | ||
161 | "STORELG ", | ||
162 | "undefined" | ||
163 | }; | ||
164 | |||
165 | const char * const LSCRIPTTypeLocalPush[LST_EOF] = | ||
166 | { | ||
167 | "INVALID", | ||
168 | "PUSH ", | ||
169 | "PUSH ", | ||
170 | "PUSHS ", | ||
171 | "PUSHS ", | ||
172 | "PUSHV ", | ||
173 | "PUSHQ ", | ||
174 | "PUSHL ", | ||
175 | "undefined" | ||
176 | }; | ||
177 | |||
178 | const char * const LSCRIPTTypeLocalPush1[LST_EOF] = | ||
179 | { | ||
180 | "INVALID", | ||
181 | "PUSHARGI 1", | ||
182 | "PUSHARGF 1", | ||
183 | "undefined", | ||
184 | "undefined", | ||
185 | "undefined", | ||
186 | "undefined", | ||
187 | "undefined", | ||
188 | "undefined" | ||
189 | }; | ||
190 | |||
191 | const char * const LSCRIPTTypeGlobalPush[LST_EOF] = | ||
192 | { | ||
193 | "INVALID", | ||
194 | "PUSHG ", | ||
195 | "PUSHG ", | ||
196 | "PUSHGS ", | ||
197 | "PUSHGS ", | ||
198 | "PUSHGV ", | ||
199 | "PUSHGQ ", | ||
200 | "PUSHGL ", | ||
201 | "undefined" | ||
202 | }; | ||
203 | |||
204 | class LLScriptSimpleAssignable; | ||
205 | |||
206 | class LLScriptArgString | ||
207 | { | ||
208 | public: | ||
209 | LLScriptArgString() : mString(NULL) {} | ||
210 | ~LLScriptArgString() { delete [] mString; } | ||
211 | |||
212 | LSCRIPTType getType(S32 count) | ||
213 | { | ||
214 | if (!mString) | ||
215 | return LST_NULL; | ||
216 | S32 length = (S32)strlen(mString); | ||
217 | if (count >= length) | ||
218 | { | ||
219 | return LST_NULL; | ||
220 | } | ||
221 | switch(mString[count]) | ||
222 | { | ||
223 | case 'i': | ||
224 | return LST_INTEGER; | ||
225 | case 'f': | ||
226 | return LST_FLOATINGPOINT; | ||
227 | case 's': | ||
228 | return LST_STRING; | ||
229 | case 'k': | ||
230 | return LST_KEY; | ||
231 | case 'v': | ||
232 | return LST_VECTOR; | ||
233 | case 'q': | ||
234 | return LST_QUATERNION; | ||
235 | case 'l': | ||
236 | return LST_LIST; | ||
237 | default: | ||
238 | return LST_NULL; | ||
239 | } | ||
240 | } | ||
241 | |||
242 | void addType(LSCRIPTType type) | ||
243 | { | ||
244 | S32 count = 0; | ||
245 | if (mString) | ||
246 | { | ||
247 | count = (S32)strlen(mString); | ||
248 | char *temp = new char[count + 2]; | ||
249 | memcpy(temp, mString, count); | ||
250 | delete [] mString; | ||
251 | mString = temp; | ||
252 | mString[count + 1] = 0; | ||
253 | } | ||
254 | else | ||
255 | { | ||
256 | mString = new char[count + 2]; | ||
257 | mString[count + 1] = 0; | ||
258 | } | ||
259 | mString[count++] = LSCRIPTFunctionTypeStrings[type]; | ||
260 | } | ||
261 | |||
262 | S32 getNumber() | ||
263 | { | ||
264 | if (mString) | ||
265 | return (S32)strlen(mString); | ||
266 | else | ||
267 | return 0; | ||
268 | } | ||
269 | |||
270 | char *mString; | ||
271 | }; | ||
272 | |||
273 | class LLScriptScopeEntry | ||
274 | { | ||
275 | public: | ||
276 | LLScriptScopeEntry(char *identifier, LSCRIPTIdentifierType idtype, LSCRIPTType type, S32 count = 0) | ||
277 | : mIdentifier(identifier), mIDType(idtype), mType(type), mOffset(0), mSize(0), mAssignable(NULL), mCount(count), mLibraryNumber(0) | ||
278 | { | ||
279 | } | ||
280 | |||
281 | ~LLScriptScopeEntry() {} | ||
282 | |||
283 | char *mIdentifier; | ||
284 | LSCRIPTIdentifierType mIDType; | ||
285 | LSCRIPTType mType; | ||
286 | S32 mOffset; | ||
287 | S32 mSize; | ||
288 | LLScriptSimpleAssignable *mAssignable; | ||
289 | S32 mCount; // NOTE: Index for locals in CIL. | ||
290 | U16 mLibraryNumber; | ||
291 | LLScriptArgString mFunctionArgs; | ||
292 | LLScriptArgString mLocals; | ||
293 | }; | ||
294 | |||
295 | class LLScriptScope | ||
296 | { | ||
297 | public: | ||
298 | LLScriptScope(LLStringTable *stable) | ||
299 | : mParentScope(NULL), mSTable(stable), mFunctionCount(0), mStateCount(0) | ||
300 | { | ||
301 | } | ||
302 | |||
303 | ~LLScriptScope() | ||
304 | { | ||
305 | mEntryMap.deleteAllData(); | ||
306 | } | ||
307 | |||
308 | LLScriptScopeEntry *addEntry(char *identifier, LSCRIPTIdentifierType idtype, LSCRIPTType type) | ||
309 | { | ||
310 | char *name = mSTable->addString(identifier); | ||
311 | if (!mEntryMap.checkData(name)) | ||
312 | { | ||
313 | if (idtype == LIT_FUNCTION) | ||
314 | mEntryMap[name] = new LLScriptScopeEntry(name, idtype, type, mFunctionCount++); | ||
315 | else if (idtype == LIT_STATE) | ||
316 | mEntryMap[name] = new LLScriptScopeEntry(name, idtype, type, mStateCount++); | ||
317 | else | ||
318 | mEntryMap[name] = new LLScriptScopeEntry(name, idtype, type); | ||
319 | return mEntryMap[name]; | ||
320 | } | ||
321 | else | ||
322 | { | ||
323 | // identifier already exists at this scope | ||
324 | return NULL; | ||
325 | } | ||
326 | } | ||
327 | |||
328 | BOOL checkEntry(char *identifier) | ||
329 | { | ||
330 | char *name = mSTable->addString(identifier); | ||
331 | if (mEntryMap.checkData(name)) | ||
332 | { | ||
333 | return TRUE; | ||
334 | } | ||
335 | else | ||
336 | { | ||
337 | // identifier already exists at this scope | ||
338 | return FALSE; | ||
339 | } | ||
340 | } | ||
341 | |||
342 | LLScriptScopeEntry *findEntry(char *identifier) | ||
343 | { | ||
344 | char *name = mSTable->addString(identifier); | ||
345 | LLScriptScope *scope = this; | ||
346 | |||
347 | while (scope) | ||
348 | { | ||
349 | if (scope->mEntryMap.checkData(name)) | ||
350 | { | ||
351 | // cool, we found it at this scope | ||
352 | return scope->mEntryMap[name]; | ||
353 | } | ||
354 | scope = scope->mParentScope; | ||
355 | } | ||
356 | return NULL; | ||
357 | } | ||
358 | |||
359 | LLScriptScopeEntry *findEntryTyped(char *identifier, LSCRIPTIdentifierType idtype) | ||
360 | { | ||
361 | char *name = mSTable->addString(identifier); | ||
362 | LLScriptScope *scope = this; | ||
363 | |||
364 | while (scope) | ||
365 | { | ||
366 | if (scope->mEntryMap.checkData(name)) | ||
367 | { | ||
368 | // need to check type, and if type is function we need to check both types | ||
369 | if (idtype == LIT_FUNCTION) | ||
370 | { | ||
371 | if (scope->mEntryMap[name]->mIDType == LIT_FUNCTION) | ||
372 | { | ||
373 | return scope->mEntryMap[name]; | ||
374 | } | ||
375 | else if (scope->mEntryMap[name]->mIDType == LIT_LIBRARY_FUNCTION) | ||
376 | { | ||
377 | return scope->mEntryMap[name]; | ||
378 | } | ||
379 | } | ||
380 | else if (scope->mEntryMap[name]->mIDType == idtype) | ||
381 | { | ||
382 | // cool, we found it at this scope | ||
383 | return scope->mEntryMap[name]; | ||
384 | } | ||
385 | } | ||
386 | scope = scope->mParentScope; | ||
387 | } | ||
388 | return NULL; | ||
389 | } | ||
390 | |||
391 | void addParentScope(LLScriptScope *scope) | ||
392 | { | ||
393 | mParentScope = scope; | ||
394 | } | ||
395 | |||
396 | LLMap<char *, LLScriptScopeEntry *> mEntryMap; | ||
397 | LLScriptScope *mParentScope; | ||
398 | LLStringTable *mSTable; | ||
399 | S32 mFunctionCount; | ||
400 | S32 mStateCount; | ||
401 | }; | ||
402 | |||
403 | extern LLStringTable *gScopeStringTable; | ||
404 | |||
405 | |||
406 | |||
407 | #endif | ||