Home | History | Annotate | Download | only in preprocessor
      1 /****************************************************************************\
      2 Copyright (c) 2002, NVIDIA Corporation.
      3 
      4 NVIDIA Corporation("NVIDIA") supplies this software to you in
      5 consideration of your agreement to the following terms, and your use,
      6 installation, modification or redistribution of this NVIDIA software
      7 constitutes acceptance of these terms.  If you do not agree with these
      8 terms, please do not use, install, modify or redistribute this NVIDIA
      9 software.
     10 
     11 In consideration of your agreement to abide by the following terms, and
     12 subject to these terms, NVIDIA grants you a personal, non-exclusive
     13 license, under NVIDIA's copyrights in this original NVIDIA software (the
     14 "NVIDIA Software"), to use, reproduce, modify and redistribute the
     15 NVIDIA Software, with or without modifications, in source and/or binary
     16 forms; provided that if you redistribute the NVIDIA Software, you must
     17 retain the copyright notice of NVIDIA, this notice and the following
     18 text and disclaimers in all such redistributions of the NVIDIA Software.
     19 Neither the name, trademarks, service marks nor logos of NVIDIA
     20 Corporation may be used to endorse or promote products derived from the
     21 NVIDIA Software without specific prior written permission from NVIDIA.
     22 Except as expressly stated in this notice, no other rights or licenses
     23 express or implied, are granted by NVIDIA herein, including but not
     24 limited to any patent rights that may be infringed by your derivative
     25 works or by other works in which the NVIDIA Software may be
     26 incorporated. No hardware is licensed hereunder.
     27 
     28 THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
     29 WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
     30 INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
     31 NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
     32 ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
     33 PRODUCTS.
     34 
     35 IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
     36 INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     37 TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     38 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
     39 OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
     40 NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
     41 TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
     42 NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     43 \****************************************************************************/
     44 //
     45 // symbols.c
     46 //
     47 
     48 #include <stdlib.h>
     49 #include <stdio.h>
     50 #include <string.h>
     51 
     52 #include "compiler/preprocessor/slglobals.h"
     53 
     54 ///////////////////////////////////////////////////////////////////////////////////////////////
     55 /////////////////////////////////// Symbol Table Variables: ///////////////////////////////////
     56 ///////////////////////////////////////////////////////////////////////////////////////////////
     57 
     58 Scope *ScopeList = NULL;
     59 Scope *CurrentScope = NULL;
     60 Scope *GlobalScope = NULL;
     61 
     62 static void unlinkScope(void *_scope) {
     63     Scope *scope = _scope;
     64 
     65     if (scope->next)
     66         scope->next->prev = scope->prev;
     67     if (scope->prev)
     68         scope->prev->next = scope->next;
     69     else
     70         ScopeList = scope->next;
     71 }
     72 
     73 /*
     74  * NewScope()
     75  *
     76  */
     77 Scope *NewScopeInPool(MemoryPool *pool)
     78 {
     79     Scope *lScope;
     80 
     81     lScope = mem_Alloc(pool, sizeof(Scope));
     82     lScope->pool = pool;
     83     lScope->parent = NULL;
     84     lScope->funScope = NULL;
     85     lScope->symbols = NULL;
     86 
     87     lScope->level = 0;
     88 
     89     lScope->programs = NULL;
     90     if ((lScope->next = ScopeList))
     91         ScopeList->prev = lScope;
     92     lScope->prev = 0;
     93     ScopeList = lScope;
     94     mem_AddCleanup(pool, unlinkScope, lScope);
     95     return lScope;
     96 } // NewScope
     97 
     98 /*
     99  * PushScope()
    100  *
    101  */
    102 
    103 void PushScope(Scope *fScope)
    104 {
    105     Scope *lScope;
    106 
    107     if (CurrentScope) {
    108         fScope->level = CurrentScope->level + 1;
    109         if (fScope->level == 1) {
    110             if (!GlobalScope) {
    111                 /* HACK - CTD -- if GlobalScope==NULL and level==1, we're
    112                  * defining a function in the superglobal scope.  Things
    113                  * will break if we leave the level as 1, so we arbitrarily
    114                  * set it to 2 */
    115                 fScope->level = 2;
    116             }
    117         }
    118         if (fScope->level >= 2) {
    119             lScope = fScope;
    120             while (lScope->level > 2)
    121                 lScope = lScope->next;
    122             fScope->funScope = lScope;
    123         }
    124     } else {
    125         fScope->level = 0;
    126     }
    127     fScope->parent = CurrentScope;
    128     CurrentScope = fScope;
    129 } // PushScope
    130 
    131 /*
    132  * PopScope()
    133  *
    134  */
    135 
    136 Scope *PopScope(void)
    137 {
    138     Scope *lScope;
    139 
    140     lScope = CurrentScope;
    141     if (CurrentScope)
    142         CurrentScope = CurrentScope->parent;
    143     return lScope;
    144 } // PopScope
    145 
    146 /*
    147  * NewSymbol() - Allocate a new symbol node;
    148  *
    149  */
    150 
    151 Symbol *NewSymbol(SourceLoc *loc, Scope *fScope, int name, symbolkind kind)
    152 {
    153     Symbol *lSymb;
    154     char *pch;
    155     unsigned int ii;
    156 
    157     lSymb = (Symbol *) mem_Alloc(fScope->pool, sizeof(Symbol));
    158     lSymb->left = NULL;
    159     lSymb->right = NULL;
    160     lSymb->next = NULL;
    161     lSymb->name = name;
    162     lSymb->loc = *loc;
    163     lSymb->kind = kind;
    164 
    165     // Clear union area:
    166 
    167     pch = (char *) &lSymb->details;
    168     for (ii = 0; ii < sizeof(lSymb->details); ii++)
    169         *pch++ = 0;
    170     return lSymb;
    171 } // NewSymbol
    172 
    173 /*
    174  * lAddToTree() - Using a binary tree is not a good idea for basic atom values because they
    175  *         are generated in order.  We'll fix this later (by reversing the bit pattern).
    176  */
    177 
    178 static void lAddToTree(Symbol **fSymbols, Symbol *fSymb)
    179 {
    180     Symbol *lSymb;
    181     int lrev, frev;
    182 
    183     lSymb = *fSymbols;
    184     if (lSymb) {
    185         frev = GetReversedAtom(atable, fSymb->name);
    186         while (lSymb) {
    187             lrev = GetReversedAtom(atable, lSymb->name);
    188             if (lrev == frev) {
    189                 CPPErrorToInfoLog("GetAtomString(atable, fSymb->name)");
    190                 break;
    191             } else {
    192                 if (lrev > frev) {
    193                     if (lSymb->left) {
    194                         lSymb = lSymb->left;
    195                     } else {
    196                         lSymb->left = fSymb;
    197                         break;
    198                     }
    199                 } else {
    200                     if (lSymb->right) {
    201                         lSymb = lSymb->right;
    202                     } else {
    203                         lSymb->right = fSymb;
    204                         break;
    205                     }
    206                 }
    207             }
    208         }
    209     } else {
    210         *fSymbols = fSymb;
    211     }
    212 } // lAddToTree
    213 
    214 
    215 /*
    216  * AddSymbol() - Add a variable, type, or function name to a scope.
    217  *
    218  */
    219 
    220 Symbol *AddSymbol(SourceLoc *loc, Scope *fScope, int atom, symbolkind kind)
    221 {
    222     Symbol *lSymb;
    223 
    224     if (!fScope)
    225         fScope = CurrentScope;
    226     lSymb = NewSymbol(loc, fScope, atom, kind);
    227     lAddToTree(&fScope->symbols, lSymb);
    228     return lSymb;
    229 } // AddSymbol
    230 
    231 
    232 /*********************************************************************************************/
    233 /************************************ Symbol Semantic Functions ******************************/
    234 /*********************************************************************************************/
    235 
    236 /*
    237  * LookUpLocalSymbol()
    238  *
    239  */
    240 
    241 Symbol *LookUpLocalSymbol(Scope *fScope, int atom)
    242 {
    243     Symbol *lSymb;
    244     int rname, ratom;
    245 
    246     ratom = GetReversedAtom(atable, atom);
    247     if (!fScope)
    248         fScope = CurrentScope;
    249     lSymb = fScope->symbols;
    250     while (lSymb) {
    251         rname = GetReversedAtom(atable, lSymb->name);
    252         if (rname == ratom) {
    253             return lSymb;
    254         } else {
    255             if (rname > ratom) {
    256                 lSymb = lSymb->left;
    257             } else {
    258                 lSymb = lSymb->right;
    259             }
    260         }
    261     }
    262     return NULL;
    263 } // LookUpLocalSymbol
    264 
    265 /*
    266  * LookUpSymbol()
    267  *
    268  */
    269 
    270 Symbol *LookUpSymbol(Scope *fScope, int atom)
    271 {
    272     Symbol *lSymb;
    273 
    274     if (!fScope)
    275         fScope = CurrentScope;
    276     while (fScope) {
    277         lSymb = LookUpLocalSymbol(fScope, atom);
    278         if (lSymb)
    279             return lSymb;
    280         fScope = fScope->parent;
    281     }
    282     return NULL;
    283 } // LookUpSymbol
    284 
    285