Home | History | Annotate | Download | only in preprocessor
      1 //
      2 //Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
      3 //Copyright (C) 2013 LunarG, Inc.
      4 //All rights reserved.
      5 //
      6 //Redistribution and use in source and binary forms, with or without
      7 //modification, are permitted provided that the following conditions
      8 //are met:
      9 //
     10 //    Redistributions of source code must retain the above copyright
     11 //    notice, this list of conditions and the following disclaimer.
     12 //
     13 //    Redistributions in binary form must reproduce the above
     14 //    copyright notice, this list of conditions and the following
     15 //    disclaimer in the documentation and/or other materials provided
     16 //    with the distribution.
     17 //
     18 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
     19 //    contributors may be used to endorse or promote products derived
     20 //    from this software without specific prior written permission.
     21 //
     22 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     23 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     24 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     25 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     26 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     27 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     28 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     29 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     30 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     32 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33 //POSSIBILITY OF SUCH DAMAGE.
     34 //
     35 /****************************************************************************\
     36 Copyright (c) 2002, NVIDIA Corporation.
     37 
     38 NVIDIA Corporation("NVIDIA") supplies this software to you in
     39 consideration of your agreement to the following terms, and your use,
     40 installation, modification or redistribution of this NVIDIA software
     41 constitutes acceptance of these terms.  If you do not agree with these
     42 terms, please do not use, install, modify or redistribute this NVIDIA
     43 software.
     44 
     45 In consideration of your agreement to abide by the following terms, and
     46 subject to these terms, NVIDIA grants you a personal, non-exclusive
     47 license, under NVIDIA's copyrights in this original NVIDIA software (the
     48 "NVIDIA Software"), to use, reproduce, modify and redistribute the
     49 NVIDIA Software, with or without modifications, in source and/or binary
     50 forms; provided that if you redistribute the NVIDIA Software, you must
     51 retain the copyright notice of NVIDIA, this notice and the following
     52 text and disclaimers in all such redistributions of the NVIDIA Software.
     53 Neither the name, trademarks, service marks nor logos of NVIDIA
     54 Corporation may be used to endorse or promote products derived from the
     55 NVIDIA Software without specific prior written permission from NVIDIA.
     56 Except as expressly stated in this notice, no other rights or licenses
     57 express or implied, are granted by NVIDIA herein, including but not
     58 limited to any patent rights that may be infringed by your derivative
     59 works or by other works in which the NVIDIA Software may be
     60 incorporated. No hardware is licensed hereunder.
     61 
     62 THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
     63 WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
     64 INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
     65 NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
     66 ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
     67 PRODUCTS.
     68 
     69 IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
     70 INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     71 TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     72 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
     73 OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
     74 NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
     75 TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
     76 NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     77 \****************************************************************************/
     78 
     79 //
     80 // atom.c
     81 //
     82 
     83 #define _CRT_SECURE_NO_WARNINGS
     84 
     85 #include <assert.h>
     86 #include <stdlib.h>
     87 #include <stdio.h>
     88 #include <string.h>
     89 
     90 #include "PpContext.h"
     91 #include "PpTokens.h"
     92 
     93 namespace {
     94 
     95 using namespace glslang;
     96 
     97 const struct {
     98     int val;
     99     const char* str;
    100 } tokens[] = {
    101     { PpAtomDefine,         "define" },
    102     { PpAtomDefined,        "defined" },
    103     { PpAtomUndef,          "undef" },
    104     { PpAtomIf,             "if" },
    105     { PpAtomElif,           "elif" },
    106     { PpAtomElse,           "else" },
    107     { PpAtomEndif,          "endif" },
    108     { PpAtomIfdef,          "ifdef" },
    109     { PpAtomIfndef,         "ifndef" },
    110     { PpAtomLine,           "line" },
    111     { PpAtomPragma,         "pragma" },
    112     { PpAtomError,          "error" },
    113 
    114     { PpAtomVersion,        "version" },
    115     { PpAtomCore,           "core" },
    116     { PpAtomCompatibility,  "compatibility" },
    117     { PpAtomEs,             "es" },
    118     { PpAtomExtension,      "extension" },
    119 
    120     { PpAtomLineMacro,       "__LINE__" },
    121     { PpAtomFileMacro,       "__FILE__" },
    122     { PpAtomVersionMacro,    "__VERSION__" },
    123 
    124     { PpAtomInclude,        "include" },
    125 
    126 };
    127 
    128 } // end anonymous namespace
    129 
    130 namespace glslang {
    131 
    132 //
    133 // Map a new or existing string to an atom, inventing a new atom if necessary.
    134 //
    135 int TPpContext::LookUpAddString(const char* s)
    136 {
    137     auto it = atomMap.find(s);
    138     if (it == atomMap.end()) {
    139         AddAtomFixed(s, nextAtom);
    140         return nextAtom++;
    141     } else
    142         return it->second;
    143 }
    144 
    145 //
    146 // Map an already created atom to its string.
    147 //
    148 const char* TPpContext::GetAtomString(int atom)
    149 {
    150     if ((size_t)atom >= stringMap.size())
    151         return "<bad token>";
    152 
    153     const TString* atomString = stringMap[atom];
    154 
    155     return atomString ? atomString->c_str() : "<bad token>";
    156 }
    157 
    158 //
    159 // Add forced mapping of string to atom.
    160 //
    161 void TPpContext::AddAtomFixed(const char* s, int atom)
    162 {
    163     auto it = atomMap.insert(std::pair<TString, int>(s, atom)).first;
    164     if (stringMap.size() < (size_t)atom + 1)
    165         stringMap.resize(atom + 100, 0);
    166     stringMap[atom] = &it->first;
    167 }
    168 
    169 //
    170 // Initialize the atom table.
    171 //
    172 void TPpContext::InitAtomTable()
    173 {
    174     // Add single character tokens to the atom table:
    175     const char* s = "~!%^&*()-+=|,.<>/?;:[]{}#\\";
    176     char t[2];
    177 
    178     t[1] = '\0';
    179     while (*s) {
    180         t[0] = *s;
    181         AddAtomFixed(t, s[0]);
    182         s++;
    183     }
    184 
    185     // Add multiple character scanner tokens :
    186     for (size_t ii = 0; ii < sizeof(tokens)/sizeof(tokens[0]); ii++)
    187         AddAtomFixed(tokens[ii].str, tokens[ii].val);
    188 
    189     nextAtom = PpAtomLast;
    190 }
    191 
    192 } // end namespace glslang
    193