1 //
2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
4 // Copyright (C) 2015-2018 Google, Inc.
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
19 //
20 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
36 //
37 /****************************************************************************\
38 Copyright (c) 2002, NVIDIA Corporation.
39
40 NVIDIA Corporation("NVIDIA") supplies this software to you in
41 consideration of your agreement to the following terms, and your use,
42 installation, modification or redistribution of this NVIDIA software
43 constitutes acceptance of these terms. If you do not agree with these
44 terms, please do not use, install, modify or redistribute this NVIDIA
45 software.
46
47 In consideration of your agreement to abide by the following terms, and
48 subject to these terms, NVIDIA grants you a personal, non-exclusive
49 license, under NVIDIA's copyrights in this original NVIDIA software (the
50 "NVIDIA Software"), to use, reproduce, modify and redistribute the
51 NVIDIA Software, with or without modifications, in source and/or binary
52 forms; provided that if you redistribute the NVIDIA Software, you must
53 retain the copyright notice of NVIDIA, this notice and the following
54 text and disclaimers in all such redistributions of the NVIDIA Software.
55 Neither the name, trademarks, service marks nor logos of NVIDIA
56 Corporation may be used to endorse or promote products derived from the
57 NVIDIA Software without specific prior written permission from NVIDIA.
58 Except as expressly stated in this notice, no other rights or licenses
59 express or implied, are granted by NVIDIA herein, including but not
60 limited to any patent rights that may be infringed by your derivative
61 works or by other works in which the NVIDIA Software may be
62 incorporated. No hardware is licensed hereunder.
63
64 THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
65 WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
66 INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
67 NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
68 ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
69 PRODUCTS.
70
71 IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
72 INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
73 TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
74 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
75 OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
76 NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
77 TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
78 NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79 \****************************************************************************/
80
81 //
82 // For recording and playing back the stream of tokens in a macro definition.
83 //
84
85 #ifndef _CRT_SECURE_NO_WARNINGS
86 #define _CRT_SECURE_NO_WARNINGS
87 #endif
88 #if (defined(_MSC_VER) && _MSC_VER < 1900 /*vs2015*/)
89 #define snprintf sprintf_s
90 #endif
91
92 #include <cassert>
93 #include <cstdlib>
94 #include <cstring>
95 #include <cctype>
96
97 #include "PpContext.h"
98 #include "PpTokens.h"
99
100 namespace glslang {
101
102 // Add a token (including backing string) to the end of a macro
103 // token stream, for later playback.
putToken(int atom,TPpToken * ppToken)104 void TPpContext::TokenStream::putToken(int atom, TPpToken* ppToken)
105 {
106 TokenStream::Token streamToken(atom, *ppToken);
107 stream.push_back(streamToken);
108 }
109
110 // Read the next token from a macro token stream.
getToken(TParseContextBase & parseContext,TPpToken * ppToken)111 int TPpContext::TokenStream::getToken(TParseContextBase& parseContext, TPpToken *ppToken)
112 {
113 if (atEnd())
114 return EndOfInput;
115
116 int atom = stream[currentPos++].get(*ppToken);
117 ppToken->loc = parseContext.getCurrentLoc();
118
119 #ifndef GLSLANG_WEB
120 // Check for ##, unless the current # is the last character
121 if (atom == '#') {
122 if (peekToken('#')) {
123 parseContext.requireProfile(ppToken->loc, ~EEsProfile, "token pasting (##)");
124 parseContext.profileRequires(ppToken->loc, ~EEsProfile, 130, 0, "token pasting (##)");
125 currentPos++;
126 atom = PpAtomPaste;
127 }
128 }
129 #endif
130
131 return atom;
132 }
133
134 // We are pasting if
135 // 1. we are preceding a pasting operator within this stream
136 // or
137 // 2. the entire macro is preceding a pasting operator (lastTokenPastes)
138 // and we are also on the last token
peekTokenizedPasting(bool lastTokenPastes)139 bool TPpContext::TokenStream::peekTokenizedPasting(bool lastTokenPastes)
140 {
141 // 1. preceding ##?
142
143 size_t savePos = currentPos;
144 // skip white space
145 while (peekToken(' '))
146 ++currentPos;
147 if (peekToken(PpAtomPaste)) {
148 currentPos = savePos;
149 return true;
150 }
151
152 // 2. last token and we've been told after this there will be a ##
153
154 if (! lastTokenPastes)
155 return false;
156 // Getting here means the last token will be pasted, after this
157
158 // Are we at the last non-whitespace token?
159 savePos = currentPos;
160 bool moreTokens = false;
161 do {
162 if (atEnd())
163 break;
164 if (!peekToken(' ')) {
165 moreTokens = true;
166 break;
167 }
168 ++currentPos;
169 } while (true);
170 currentPos = savePos;
171
172 return !moreTokens;
173 }
174
175 // See if the next non-white-space tokens are two consecutive #
peekUntokenizedPasting()176 bool TPpContext::TokenStream::peekUntokenizedPasting()
177 {
178 // don't return early, have to restore this
179 size_t savePos = currentPos;
180
181 // skip white-space
182 while (peekToken(' '))
183 ++currentPos;
184
185 // check for ##
186 bool pasting = false;
187 if (peekToken('#')) {
188 ++currentPos;
189 if (peekToken('#'))
190 pasting = true;
191 }
192
193 currentPos = savePos;
194
195 return pasting;
196 }
197
pushTokenStreamInput(TokenStream & ts,bool prepasting)198 void TPpContext::pushTokenStreamInput(TokenStream& ts, bool prepasting)
199 {
200 pushInput(new tTokenInput(this, &ts, prepasting));
201 ts.reset();
202 }
203
scan(TPpToken * ppToken)204 int TPpContext::tUngotTokenInput::scan(TPpToken* ppToken)
205 {
206 if (done)
207 return EndOfInput;
208
209 int ret = token;
210 *ppToken = lval;
211 done = true;
212
213 return ret;
214 }
215
UngetToken(int token,TPpToken * ppToken)216 void TPpContext::UngetToken(int token, TPpToken* ppToken)
217 {
218 pushInput(new tUngotTokenInput(this, token, ppToken));
219 }
220
221 } // end namespace glslang
222