2
0

PpScanner.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. //
  2. // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
  3. // Copyright (C) 2013 LunarG, Inc.
  4. // Copyright (C) 2017 ARM Limited.
  5. // Copyright (C) 2015-2018 Google, Inc.
  6. //
  7. // All rights reserved.
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions
  11. // are met:
  12. //
  13. // Redistributions of source code must retain the above copyright
  14. // notice, this list of conditions and the following disclaimer.
  15. //
  16. // Redistributions in binary form must reproduce the above
  17. // copyright notice, this list of conditions and the following
  18. // disclaimer in the documentation and/or other materials provided
  19. // with the distribution.
  20. //
  21. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  22. // contributors may be used to endorse or promote products derived
  23. // from this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. // POSSIBILITY OF SUCH DAMAGE.
  37. //
  38. /****************************************************************************\
  39. Copyright (c) 2002, NVIDIA Corporation.
  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. In consideration of your agreement to abide by the following terms, and
  47. subject to these terms, NVIDIA grants you a personal, non-exclusive
  48. license, under NVIDIA's copyrights in this original NVIDIA software (the
  49. "NVIDIA Software"), to use, reproduce, modify and redistribute the
  50. NVIDIA Software, with or without modifications, in source and/or binary
  51. forms; provided that if you redistribute the NVIDIA Software, you must
  52. retain the copyright notice of NVIDIA, this notice and the following
  53. text and disclaimers in all such redistributions of the NVIDIA Software.
  54. Neither the name, trademarks, service marks nor logos of NVIDIA
  55. Corporation may be used to endorse or promote products derived from the
  56. NVIDIA Software without specific prior written permission from NVIDIA.
  57. Except as expressly stated in this notice, no other rights or licenses
  58. express or implied, are granted by NVIDIA herein, including but not
  59. limited to any patent rights that may be infringed by your derivative
  60. works or by other works in which the NVIDIA Software may be
  61. incorporated. No hardware is licensed hereunder.
  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. IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
  69. INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  70. TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  71. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
  72. OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
  73. NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
  74. TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  75. NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  76. \****************************************************************************/
  77. #ifndef _CRT_SECURE_NO_WARNINGS
  78. #define _CRT_SECURE_NO_WARNINGS
  79. #endif
  80. #include <cstdlib>
  81. #include <cstring>
  82. #include "PpContext.h"
  83. #include "PpTokens.h"
  84. #include "../Scan.h"
  85. namespace glslang {
  86. ///////////////////////////////////////////////////////////////////////////////////////////////
  87. /////////////////////////////////// Floating point constants: /////////////////////////////////
  88. ///////////////////////////////////////////////////////////////////////////////////////////////
  89. //
  90. // Scan a single- or double-precision floating point constant.
  91. // Assumes that the scanner has seen at least one digit,
  92. // followed by either a decimal '.' or the letter 'e', or a
  93. // precision ending (e.g., F or LF).
  94. //
  95. // This is technically not correct, as the preprocessor should just
  96. // accept the numeric literal along with whatever suffix it has, but
  97. // currently, it stops on seeing a bad suffix, treating that as the
  98. // next token. This effects things like token pasting, where it is
  99. // relevant how many tokens something was broken into.
  100. //
  101. // See peekContinuedPasting().
  102. int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
  103. {
  104. const auto saveName = [&](int ch) {
  105. if (len <= MaxTokenLength)
  106. ppToken->name[len++] = static_cast<char>(ch);
  107. };
  108. // find the range of non-zero digits before the decimal point
  109. int startNonZero = 0;
  110. while (startNonZero < len && ppToken->name[startNonZero] == '0')
  111. ++startNonZero;
  112. int endNonZero = len;
  113. while (endNonZero > startNonZero && ppToken->name[endNonZero-1] == '0')
  114. --endNonZero;
  115. int numWholeNumberDigits = endNonZero - startNonZero;
  116. // accumulate the range's value
  117. bool fastPath = numWholeNumberDigits <= 15; // when the number gets too complex, set to false
  118. unsigned long long wholeNumber = 0;
  119. if (fastPath) {
  120. for (int i = startNonZero; i < endNonZero; ++i)
  121. wholeNumber = wholeNumber * 10 + (ppToken->name[i] - '0');
  122. }
  123. int decimalShift = len - endNonZero;
  124. // Decimal point:
  125. bool hasDecimalOrExponent = false;
  126. if (ch == '.') {
  127. hasDecimalOrExponent = true;
  128. saveName(ch);
  129. ch = getChar();
  130. int firstDecimal = len;
  131. #ifdef ENABLE_HLSL
  132. // 1.#INF or -1.#INF
  133. if (ch == '#' && (ifdepth > 0 || parseContext.intermediate.getSource() == EShSourceHlsl)) {
  134. if ((len < 2) ||
  135. (len == 2 && ppToken->name[0] != '1') ||
  136. (len == 3 && ppToken->name[1] != '1' && !(ppToken->name[0] == '-' || ppToken->name[0] == '+')) ||
  137. (len > 3))
  138. parseContext.ppError(ppToken->loc, "unexpected use of", "#", "");
  139. else {
  140. // we have 1.# or -1.# or +1.#, check for 'INF'
  141. if ((ch = getChar()) != 'I' ||
  142. (ch = getChar()) != 'N' ||
  143. (ch = getChar()) != 'F')
  144. parseContext.ppError(ppToken->loc, "expected 'INF'", "#", "");
  145. else {
  146. // we have [+-].#INF, and we are targeting IEEE 754, so wrap it up:
  147. saveName('I');
  148. saveName('N');
  149. saveName('F');
  150. ppToken->name[len] = '\0';
  151. if (ppToken->name[0] == '-')
  152. ppToken->i64val = 0xfff0000000000000; // -Infinity
  153. else
  154. ppToken->i64val = 0x7ff0000000000000; // +Infinity
  155. return PpAtomConstFloat;
  156. }
  157. }
  158. }
  159. #endif
  160. // Consume leading-zero digits after the decimal point
  161. while (ch == '0') {
  162. saveName(ch);
  163. ch = getChar();
  164. }
  165. int startNonZeroDecimal = len;
  166. int endNonZeroDecimal = len;
  167. // Consume remaining digits, up to the exponent
  168. while (ch >= '0' && ch <= '9') {
  169. saveName(ch);
  170. if (ch != '0')
  171. endNonZeroDecimal = len;
  172. ch = getChar();
  173. }
  174. // Compute accumulation up to the last non-zero digit
  175. if (endNonZeroDecimal > startNonZeroDecimal) {
  176. numWholeNumberDigits += endNonZeroDecimal - endNonZero - 1; // don't include the "."
  177. if (numWholeNumberDigits > 15)
  178. fastPath = false;
  179. if (fastPath) {
  180. for (int i = endNonZero; i < endNonZeroDecimal; ++i) {
  181. if (ppToken->name[i] != '.')
  182. wholeNumber = wholeNumber * 10 + (ppToken->name[i] - '0');
  183. }
  184. }
  185. decimalShift = firstDecimal - endNonZeroDecimal;
  186. }
  187. }
  188. // Exponent:
  189. bool negativeExponent = false;
  190. double exponentValue = 0.0;
  191. int exponent = 0;
  192. {
  193. if (ch == 'e' || ch == 'E') {
  194. hasDecimalOrExponent = true;
  195. saveName(ch);
  196. ch = getChar();
  197. if (ch == '+' || ch == '-') {
  198. negativeExponent = ch == '-';
  199. saveName(ch);
  200. ch = getChar();
  201. }
  202. if (ch >= '0' && ch <= '9') {
  203. while (ch >= '0' && ch <= '9') {
  204. exponent = exponent * 10 + (ch - '0');
  205. saveName(ch);
  206. ch = getChar();
  207. }
  208. } else {
  209. parseContext.ppError(ppToken->loc, "bad character in float exponent", "", "");
  210. }
  211. }
  212. // Compensate for location of decimal
  213. if (negativeExponent)
  214. exponent -= decimalShift;
  215. else {
  216. exponent += decimalShift;
  217. if (exponent < 0) {
  218. negativeExponent = true;
  219. exponent = -exponent;
  220. }
  221. }
  222. if (exponent > 22)
  223. fastPath = false;
  224. if (fastPath) {
  225. // Compute the floating-point value of the exponent
  226. exponentValue = 1.0;
  227. if (exponent > 0) {
  228. double expFactor = 10;
  229. while (exponent > 0) {
  230. if (exponent & 0x1)
  231. exponentValue *= expFactor;
  232. expFactor *= expFactor;
  233. exponent >>= 1;
  234. }
  235. }
  236. }
  237. }
  238. // Suffix:
  239. bool isDouble = false;
  240. bool isFloat16 = false;
  241. #ifndef GLSLANG_WEB
  242. if (ch == 'l' || ch == 'L') {
  243. if (ifdepth == 0 && parseContext.intermediate.getSource() == EShSourceGlsl)
  244. parseContext.doubleCheck(ppToken->loc, "double floating-point suffix");
  245. if (ifdepth == 0 && !hasDecimalOrExponent)
  246. parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", "");
  247. if (parseContext.intermediate.getSource() == EShSourceGlsl) {
  248. int ch2 = getChar();
  249. if (ch2 != 'f' && ch2 != 'F') {
  250. ungetChar();
  251. ungetChar();
  252. } else {
  253. saveName(ch);
  254. saveName(ch2);
  255. isDouble = true;
  256. }
  257. } else if (parseContext.intermediate.getSource() == EShSourceHlsl) {
  258. saveName(ch);
  259. isDouble = true;
  260. }
  261. } else if (ch == 'h' || ch == 'H') {
  262. if (ifdepth == 0 && parseContext.intermediate.getSource() == EShSourceGlsl)
  263. parseContext.float16Check(ppToken->loc, "half floating-point suffix");
  264. if (ifdepth == 0 && !hasDecimalOrExponent)
  265. parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", "");
  266. if (parseContext.intermediate.getSource() == EShSourceGlsl) {
  267. int ch2 = getChar();
  268. if (ch2 != 'f' && ch2 != 'F') {
  269. ungetChar();
  270. ungetChar();
  271. } else {
  272. saveName(ch);
  273. saveName(ch2);
  274. isFloat16 = true;
  275. }
  276. } else if (parseContext.intermediate.getSource() == EShSourceHlsl) {
  277. saveName(ch);
  278. isFloat16 = true;
  279. }
  280. } else
  281. #endif
  282. if (ch == 'f' || ch == 'F') {
  283. #ifndef GLSLANG_WEB
  284. if (ifdepth == 0)
  285. parseContext.profileRequires(ppToken->loc, EEsProfile, 300, nullptr, "floating-point suffix");
  286. if (ifdepth == 0 && !parseContext.relaxedErrors())
  287. parseContext.profileRequires(ppToken->loc, ~EEsProfile, 120, nullptr, "floating-point suffix");
  288. #endif
  289. if (ifdepth == 0 && !hasDecimalOrExponent)
  290. parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", "");
  291. saveName(ch);
  292. } else
  293. ungetChar();
  294. // Patch up the name and length for overflow
  295. if (len > MaxTokenLength) {
  296. len = MaxTokenLength;
  297. parseContext.ppError(ppToken->loc, "float literal too long", "", "");
  298. }
  299. ppToken->name[len] = '\0';
  300. // Compute the numerical value
  301. if (fastPath) {
  302. // compute the floating-point value of the exponent
  303. if (exponentValue == 0.0)
  304. ppToken->dval = (double)wholeNumber;
  305. else if (negativeExponent)
  306. ppToken->dval = (double)wholeNumber / exponentValue;
  307. else
  308. ppToken->dval = (double)wholeNumber * exponentValue;
  309. } else {
  310. // slow path
  311. ppToken->dval = 0.0;
  312. // remove suffix
  313. TString numstr(ppToken->name);
  314. if (numstr.back() == 'f' || numstr.back() == 'F')
  315. numstr.pop_back();
  316. if (numstr.back() == 'h' || numstr.back() == 'H')
  317. numstr.pop_back();
  318. if (numstr.back() == 'l' || numstr.back() == 'L')
  319. numstr.pop_back();
  320. // use platform library
  321. strtodStream.clear();
  322. strtodStream.str(numstr.c_str());
  323. strtodStream >> ppToken->dval;
  324. if (strtodStream.fail()) {
  325. // Assume failure combined with a large exponent was overflow, in
  326. // an attempt to set INF.
  327. if (!negativeExponent && exponent + numWholeNumberDigits > 300)
  328. ppToken->i64val = 0x7ff0000000000000; // +Infinity
  329. // Assume failure combined with a small exponent was overflow.
  330. if (negativeExponent && exponent + numWholeNumberDigits > 300)
  331. ppToken->dval = 0.0;
  332. // Unknown reason for failure. Theory is that either
  333. // - the 0.0 is still there, or
  334. // - something reasonable was written that is better than 0.0
  335. }
  336. }
  337. // Return the right token type
  338. if (isDouble)
  339. return PpAtomConstDouble;
  340. else if (isFloat16)
  341. return PpAtomConstFloat16;
  342. else
  343. return PpAtomConstFloat;
  344. }
  345. // Recognize a character literal.
  346. //
  347. // The first ' has already been accepted, read the rest, through the closing '.
  348. //
  349. // Always returns PpAtomConstInt.
  350. //
  351. int TPpContext::characterLiteral(TPpToken* ppToken)
  352. {
  353. ppToken->name[0] = 0;
  354. ppToken->ival = 0;
  355. if (parseContext.intermediate.getSource() != EShSourceHlsl) {
  356. // illegal, except in macro definition, for which case we report the character
  357. return '\'';
  358. }
  359. int ch = getChar();
  360. switch (ch) {
  361. case '\'':
  362. // As empty sequence: ''
  363. parseContext.ppError(ppToken->loc, "unexpected", "\'", "");
  364. return PpAtomConstInt;
  365. case '\\':
  366. // As escape sequence: '\XXX'
  367. switch (ch = getChar()) {
  368. case 'a':
  369. ppToken->ival = 7;
  370. break;
  371. case 'b':
  372. ppToken->ival = 8;
  373. break;
  374. case 't':
  375. ppToken->ival = 9;
  376. break;
  377. case 'n':
  378. ppToken->ival = 10;
  379. break;
  380. case 'v':
  381. ppToken->ival = 11;
  382. break;
  383. case 'f':
  384. ppToken->ival = 12;
  385. break;
  386. case 'r':
  387. ppToken->ival = 13;
  388. break;
  389. case 'x':
  390. case '0':
  391. parseContext.ppError(ppToken->loc, "octal and hex sequences not supported", "\\", "");
  392. break;
  393. default:
  394. // This catches '\'', '\"', '\?', etc.
  395. // Also, things like '\C' mean the same thing as 'C'
  396. // (after the above cases are filtered out).
  397. ppToken->ival = ch;
  398. break;
  399. }
  400. break;
  401. default:
  402. ppToken->ival = ch;
  403. break;
  404. }
  405. ppToken->name[0] = (char)ppToken->ival;
  406. ppToken->name[1] = '\0';
  407. ch = getChar();
  408. if (ch != '\'') {
  409. parseContext.ppError(ppToken->loc, "expected", "\'", "");
  410. // Look ahead for a closing '
  411. do {
  412. ch = getChar();
  413. } while (ch != '\'' && ch != EndOfInput && ch != '\n');
  414. }
  415. return PpAtomConstInt;
  416. }
  417. //
  418. // Scanner used to tokenize source stream.
  419. //
  420. // N.B. Invalid numeric suffixes are not consumed.//
  421. // This is technically not correct, as the preprocessor should just
  422. // accept the numeric literal along with whatever suffix it has, but
  423. // currently, it stops on seeing a bad suffix, treating that as the
  424. // next token. This effects things like token pasting, where it is
  425. // relevant how many tokens something was broken into.
  426. // See peekContinuedPasting().
  427. //
  428. int TPpContext::tStringInput::scan(TPpToken* ppToken)
  429. {
  430. int AlreadyComplained = 0;
  431. int len = 0;
  432. int ch = 0;
  433. int ii = 0;
  434. unsigned long long ival = 0;
  435. const auto floatingPointChar = [&](int ch) { return ch == '.' || ch == 'e' || ch == 'E' ||
  436. ch == 'f' || ch == 'F' ||
  437. ch == 'h' || ch == 'H'; };
  438. static const char* const Int64_Extensions[] = {
  439. E_GL_ARB_gpu_shader_int64,
  440. E_GL_EXT_shader_explicit_arithmetic_types,
  441. E_GL_EXT_shader_explicit_arithmetic_types_int64 };
  442. static const int Num_Int64_Extensions = sizeof(Int64_Extensions) / sizeof(Int64_Extensions[0]);
  443. static const char* const Int16_Extensions[] = {
  444. E_GL_AMD_gpu_shader_int16,
  445. E_GL_EXT_shader_explicit_arithmetic_types,
  446. E_GL_EXT_shader_explicit_arithmetic_types_int16 };
  447. static const int Num_Int16_Extensions = sizeof(Int16_Extensions) / sizeof(Int16_Extensions[0]);
  448. ppToken->ival = 0;
  449. ppToken->i64val = 0;
  450. ppToken->space = false;
  451. ch = getch();
  452. for (;;) {
  453. while (ch == ' ' || ch == '\t') {
  454. ppToken->space = true;
  455. ch = getch();
  456. }
  457. ppToken->loc = pp->parseContext.getCurrentLoc();
  458. len = 0;
  459. switch (ch) {
  460. default:
  461. // Single character token, including EndOfInput, '#' and '\' (escaped newlines are handled at a lower level, so this is just a '\' token)
  462. if (ch > PpAtomMaxSingle)
  463. ch = PpAtomBadToken;
  464. return ch;
  465. case 'A': case 'B': case 'C': case 'D': case 'E':
  466. case 'F': case 'G': case 'H': case 'I': case 'J':
  467. case 'K': case 'L': case 'M': case 'N': case 'O':
  468. case 'P': case 'Q': case 'R': case 'S': case 'T':
  469. case 'U': case 'V': case 'W': case 'X': case 'Y':
  470. case 'Z': case '_':
  471. case 'a': case 'b': case 'c': case 'd': case 'e':
  472. case 'f': case 'g': case 'h': case 'i': case 'j':
  473. case 'k': case 'l': case 'm': case 'n': case 'o':
  474. case 'p': case 'q': case 'r': case 's': case 't':
  475. case 'u': case 'v': case 'w': case 'x': case 'y':
  476. case 'z':
  477. do {
  478. if (len < MaxTokenLength) {
  479. ppToken->name[len++] = (char)ch;
  480. ch = getch();
  481. } else {
  482. if (! AlreadyComplained) {
  483. pp->parseContext.ppError(ppToken->loc, "name too long", "", "");
  484. AlreadyComplained = 1;
  485. }
  486. ch = getch();
  487. }
  488. } while ((ch >= 'a' && ch <= 'z') ||
  489. (ch >= 'A' && ch <= 'Z') ||
  490. (ch >= '0' && ch <= '9') ||
  491. ch == '_');
  492. // line continuation with no token before or after makes len == 0, and need to start over skipping white space, etc.
  493. if (len == 0)
  494. continue;
  495. ppToken->name[len] = '\0';
  496. ungetch();
  497. return PpAtomIdentifier;
  498. case '0':
  499. ppToken->name[len++] = (char)ch;
  500. ch = getch();
  501. if (ch == 'x' || ch == 'X') {
  502. // must be hexadecimal
  503. bool isUnsigned = false;
  504. bool isInt64 = false;
  505. bool isInt16 = false;
  506. ppToken->name[len++] = (char)ch;
  507. ch = getch();
  508. if ((ch >= '0' && ch <= '9') ||
  509. (ch >= 'A' && ch <= 'F') ||
  510. (ch >= 'a' && ch <= 'f')) {
  511. ival = 0;
  512. do {
  513. if (len < MaxTokenLength && ival <= 0x0fffffffffffffffull) {
  514. ppToken->name[len++] = (char)ch;
  515. if (ch >= '0' && ch <= '9') {
  516. ii = ch - '0';
  517. } else if (ch >= 'A' && ch <= 'F') {
  518. ii = ch - 'A' + 10;
  519. } else if (ch >= 'a' && ch <= 'f') {
  520. ii = ch - 'a' + 10;
  521. } else
  522. pp->parseContext.ppError(ppToken->loc, "bad digit in hexadecimal literal", "", "");
  523. ival = (ival << 4) | ii;
  524. } else {
  525. if (! AlreadyComplained) {
  526. if(len < MaxTokenLength)
  527. pp->parseContext.ppError(ppToken->loc, "hexadecimal literal too big", "", "");
  528. else
  529. pp->parseContext.ppError(ppToken->loc, "hexadecimal literal too long", "", "");
  530. AlreadyComplained = 1;
  531. }
  532. ival = 0xffffffffffffffffull;
  533. }
  534. ch = getch();
  535. } while ((ch >= '0' && ch <= '9') ||
  536. (ch >= 'A' && ch <= 'F') ||
  537. (ch >= 'a' && ch <= 'f'));
  538. } else {
  539. pp->parseContext.ppError(ppToken->loc, "bad digit in hexadecimal literal", "", "");
  540. }
  541. if (ch == 'u' || ch == 'U') {
  542. if (len < MaxTokenLength)
  543. ppToken->name[len++] = (char)ch;
  544. isUnsigned = true;
  545. #ifndef GLSLANG_WEB
  546. int nextCh = getch();
  547. if (nextCh == 'l' || nextCh == 'L') {
  548. if (len < MaxTokenLength)
  549. ppToken->name[len++] = (char)nextCh;
  550. isInt64 = true;
  551. } else
  552. ungetch();
  553. nextCh = getch();
  554. if ((nextCh == 's' || nextCh == 'S') &&
  555. pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  556. if (len < MaxTokenLength)
  557. ppToken->name[len++] = (char)nextCh;
  558. isInt16 = true;
  559. } else
  560. ungetch();
  561. } else if (ch == 'l' || ch == 'L') {
  562. if (len < MaxTokenLength)
  563. ppToken->name[len++] = (char)ch;
  564. isInt64 = true;
  565. } else if ((ch == 's' || ch == 'S') &&
  566. pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  567. if (len < MaxTokenLength)
  568. ppToken->name[len++] = (char)ch;
  569. isInt16 = true;
  570. #endif
  571. } else
  572. ungetch();
  573. ppToken->name[len] = '\0';
  574. if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  575. if (pp->ifdepth == 0) {
  576. pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
  577. "64-bit hexadecimal literal");
  578. pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
  579. Num_Int64_Extensions, Int64_Extensions, "64-bit hexadecimal literal");
  580. }
  581. ppToken->i64val = ival;
  582. return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64;
  583. } else if (isInt16) {
  584. if (pp->ifdepth == 0) {
  585. if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  586. pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
  587. "16-bit hexadecimal literal");
  588. pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
  589. Num_Int16_Extensions, Int16_Extensions, "16-bit hexadecimal literal");
  590. }
  591. }
  592. ppToken->ival = (int)ival;
  593. return isUnsigned ? PpAtomConstUint16 : PpAtomConstInt16;
  594. } else {
  595. if (ival > 0xffffffffu && !AlreadyComplained)
  596. pp->parseContext.ppError(ppToken->loc, "hexadecimal literal too big", "", "");
  597. ppToken->ival = (int)ival;
  598. return isUnsigned ? PpAtomConstUint : PpAtomConstInt;
  599. }
  600. } else {
  601. // could be octal integer or floating point, speculative pursue octal until it must be floating point
  602. bool isUnsigned = false;
  603. bool isInt64 = false;
  604. bool isInt16 = false;
  605. bool octalOverflow = false;
  606. bool nonOctal = false;
  607. ival = 0;
  608. // see how much octal-like stuff we can read
  609. while (ch >= '0' && ch <= '7') {
  610. if (len < MaxTokenLength)
  611. ppToken->name[len++] = (char)ch;
  612. else if (! AlreadyComplained) {
  613. pp->parseContext.ppError(ppToken->loc, "numeric literal too long", "", "");
  614. AlreadyComplained = 1;
  615. }
  616. if (ival <= 0x1fffffffffffffffull) {
  617. ii = ch - '0';
  618. ival = (ival << 3) | ii;
  619. } else
  620. octalOverflow = true;
  621. ch = getch();
  622. }
  623. // could be part of a float...
  624. if (ch == '8' || ch == '9') {
  625. nonOctal = true;
  626. do {
  627. if (len < MaxTokenLength)
  628. ppToken->name[len++] = (char)ch;
  629. else if (! AlreadyComplained) {
  630. pp->parseContext.ppError(ppToken->loc, "numeric literal too long", "", "");
  631. AlreadyComplained = 1;
  632. }
  633. ch = getch();
  634. } while (ch >= '0' && ch <= '9');
  635. }
  636. if (floatingPointChar(ch))
  637. return pp->lFloatConst(len, ch, ppToken);
  638. // wasn't a float, so must be octal...
  639. if (nonOctal)
  640. pp->parseContext.ppError(ppToken->loc, "octal literal digit too large", "", "");
  641. if (ch == 'u' || ch == 'U') {
  642. if (len < MaxTokenLength)
  643. ppToken->name[len++] = (char)ch;
  644. isUnsigned = true;
  645. #ifndef GLSLANG_WEB
  646. int nextCh = getch();
  647. if (nextCh == 'l' || nextCh == 'L') {
  648. if (len < MaxTokenLength)
  649. ppToken->name[len++] = (char)nextCh;
  650. isInt64 = true;
  651. } else
  652. ungetch();
  653. nextCh = getch();
  654. if ((nextCh == 's' || nextCh == 'S') &&
  655. pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  656. if (len < MaxTokenLength)
  657. ppToken->name[len++] = (char)nextCh;
  658. isInt16 = true;
  659. } else
  660. ungetch();
  661. } else if (ch == 'l' || ch == 'L') {
  662. if (len < MaxTokenLength)
  663. ppToken->name[len++] = (char)ch;
  664. isInt64 = true;
  665. } else if ((ch == 's' || ch == 'S') &&
  666. pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  667. if (len < MaxTokenLength)
  668. ppToken->name[len++] = (char)ch;
  669. isInt16 = true;
  670. #endif
  671. } else
  672. ungetch();
  673. ppToken->name[len] = '\0';
  674. if (!isInt64 && ival > 0xffffffffu)
  675. octalOverflow = true;
  676. if (octalOverflow)
  677. pp->parseContext.ppError(ppToken->loc, "octal literal too big", "", "");
  678. if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  679. if (pp->ifdepth == 0) {
  680. pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
  681. "64-bit octal literal");
  682. pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
  683. Num_Int64_Extensions, Int64_Extensions, "64-bit octal literal");
  684. }
  685. ppToken->i64val = ival;
  686. return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64;
  687. } else if (isInt16) {
  688. if (pp->ifdepth == 0) {
  689. if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  690. pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
  691. "16-bit octal literal");
  692. pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
  693. Num_Int16_Extensions, Int16_Extensions, "16-bit octal literal");
  694. }
  695. }
  696. ppToken->ival = (int)ival;
  697. return isUnsigned ? PpAtomConstUint16 : PpAtomConstInt16;
  698. } else {
  699. ppToken->ival = (int)ival;
  700. return isUnsigned ? PpAtomConstUint : PpAtomConstInt;
  701. }
  702. }
  703. break;
  704. case '1': case '2': case '3': case '4':
  705. case '5': case '6': case '7': case '8': case '9':
  706. // can't be hexadecimal or octal, is either decimal or floating point
  707. do {
  708. if (len < MaxTokenLength)
  709. ppToken->name[len++] = (char)ch;
  710. else if (! AlreadyComplained) {
  711. pp->parseContext.ppError(ppToken->loc, "numeric literal too long", "", "");
  712. AlreadyComplained = 1;
  713. }
  714. ch = getch();
  715. } while (ch >= '0' && ch <= '9');
  716. if (floatingPointChar(ch))
  717. return pp->lFloatConst(len, ch, ppToken);
  718. else {
  719. // Finish handling signed and unsigned integers
  720. int numericLen = len;
  721. bool isUnsigned = false;
  722. bool isInt64 = false;
  723. bool isInt16 = false;
  724. if (ch == 'u' || ch == 'U') {
  725. if (len < MaxTokenLength)
  726. ppToken->name[len++] = (char)ch;
  727. isUnsigned = true;
  728. #ifndef GLSLANG_WEB
  729. int nextCh = getch();
  730. if (nextCh == 'l' || nextCh == 'L') {
  731. if (len < MaxTokenLength)
  732. ppToken->name[len++] = (char)nextCh;
  733. isInt64 = true;
  734. } else
  735. ungetch();
  736. nextCh = getch();
  737. if ((nextCh == 's' || nextCh == 'S') &&
  738. pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  739. if (len < MaxTokenLength)
  740. ppToken->name[len++] = (char)nextCh;
  741. isInt16 = true;
  742. } else
  743. ungetch();
  744. } else if (ch == 'l' || ch == 'L') {
  745. if (len < MaxTokenLength)
  746. ppToken->name[len++] = (char)ch;
  747. isInt64 = true;
  748. } else if ((ch == 's' || ch == 'S') &&
  749. pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  750. if (len < MaxTokenLength)
  751. ppToken->name[len++] = (char)ch;
  752. isInt16 = true;
  753. #endif
  754. } else
  755. ungetch();
  756. ppToken->name[len] = '\0';
  757. ival = 0;
  758. const unsigned oneTenthMaxInt = 0xFFFFFFFFu / 10;
  759. const unsigned remainderMaxInt = 0xFFFFFFFFu - 10 * oneTenthMaxInt;
  760. const unsigned long long oneTenthMaxInt64 = 0xFFFFFFFFFFFFFFFFull / 10;
  761. const unsigned long long remainderMaxInt64 = 0xFFFFFFFFFFFFFFFFull - 10 * oneTenthMaxInt64;
  762. const unsigned short oneTenthMaxInt16 = 0xFFFFu / 10;
  763. const unsigned short remainderMaxInt16 = 0xFFFFu - 10 * oneTenthMaxInt16;
  764. for (int i = 0; i < numericLen; i++) {
  765. ch = ppToken->name[i] - '0';
  766. bool overflow = false;
  767. if (isInt64)
  768. overflow = (ival > oneTenthMaxInt64 || (ival == oneTenthMaxInt64 && (unsigned long long)ch > remainderMaxInt64));
  769. else if (isInt16)
  770. overflow = (ival > oneTenthMaxInt16 || (ival == oneTenthMaxInt16 && (unsigned short)ch > remainderMaxInt16));
  771. else
  772. overflow = (ival > oneTenthMaxInt || (ival == oneTenthMaxInt && (unsigned)ch > remainderMaxInt));
  773. if (overflow) {
  774. pp->parseContext.ppError(ppToken->loc, "numeric literal too big", "", "");
  775. ival = 0xFFFFFFFFFFFFFFFFull;
  776. break;
  777. } else
  778. ival = ival * 10 + ch;
  779. }
  780. if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  781. if (pp->ifdepth == 0) {
  782. pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
  783. "64-bit literal");
  784. pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
  785. Num_Int64_Extensions, Int64_Extensions, "64-bit literal");
  786. }
  787. ppToken->i64val = ival;
  788. return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64;
  789. } else if (isInt16) {
  790. if (pp->ifdepth == 0 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
  791. pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
  792. "16-bit literal");
  793. pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
  794. Num_Int16_Extensions, Int16_Extensions, "16-bit literal");
  795. }
  796. ppToken->ival = (int)ival;
  797. return isUnsigned ? PpAtomConstUint16 : PpAtomConstInt16;
  798. } else {
  799. ppToken->ival = (int)ival;
  800. return isUnsigned ? PpAtomConstUint : PpAtomConstInt;
  801. }
  802. }
  803. break;
  804. case '-':
  805. ch = getch();
  806. if (ch == '-') {
  807. return PpAtomDecrement;
  808. } else if (ch == '=') {
  809. return PPAtomSubAssign;
  810. } else {
  811. ungetch();
  812. return '-';
  813. }
  814. case '+':
  815. ch = getch();
  816. if (ch == '+') {
  817. return PpAtomIncrement;
  818. } else if (ch == '=') {
  819. return PPAtomAddAssign;
  820. } else {
  821. ungetch();
  822. return '+';
  823. }
  824. case '*':
  825. ch = getch();
  826. if (ch == '=') {
  827. return PPAtomMulAssign;
  828. } else {
  829. ungetch();
  830. return '*';
  831. }
  832. case '%':
  833. ch = getch();
  834. if (ch == '=') {
  835. return PPAtomModAssign;
  836. } else {
  837. ungetch();
  838. return '%';
  839. }
  840. case '^':
  841. ch = getch();
  842. if (ch == '^') {
  843. return PpAtomXor;
  844. } else {
  845. if (ch == '=')
  846. return PpAtomXorAssign;
  847. else{
  848. ungetch();
  849. return '^';
  850. }
  851. }
  852. case '=':
  853. ch = getch();
  854. if (ch == '=') {
  855. return PpAtomEQ;
  856. } else {
  857. ungetch();
  858. return '=';
  859. }
  860. case '!':
  861. ch = getch();
  862. if (ch == '=') {
  863. return PpAtomNE;
  864. } else {
  865. ungetch();
  866. return '!';
  867. }
  868. case '|':
  869. ch = getch();
  870. if (ch == '|') {
  871. return PpAtomOr;
  872. } else if (ch == '=') {
  873. return PpAtomOrAssign;
  874. } else {
  875. ungetch();
  876. return '|';
  877. }
  878. case '&':
  879. ch = getch();
  880. if (ch == '&') {
  881. return PpAtomAnd;
  882. } else if (ch == '=') {
  883. return PpAtomAndAssign;
  884. } else {
  885. ungetch();
  886. return '&';
  887. }
  888. case '<':
  889. ch = getch();
  890. if (ch == '<') {
  891. ch = getch();
  892. if (ch == '=')
  893. return PpAtomLeftAssign;
  894. else {
  895. ungetch();
  896. return PpAtomLeft;
  897. }
  898. } else if (ch == '=') {
  899. return PpAtomLE;
  900. } else {
  901. ungetch();
  902. return '<';
  903. }
  904. case '>':
  905. ch = getch();
  906. if (ch == '>') {
  907. ch = getch();
  908. if (ch == '=')
  909. return PpAtomRightAssign;
  910. else {
  911. ungetch();
  912. return PpAtomRight;
  913. }
  914. } else if (ch == '=') {
  915. return PpAtomGE;
  916. } else {
  917. ungetch();
  918. return '>';
  919. }
  920. case '.':
  921. ch = getch();
  922. if (ch >= '0' && ch <= '9') {
  923. ungetch();
  924. return pp->lFloatConst(0, '.', ppToken);
  925. } else {
  926. ungetch();
  927. return '.';
  928. }
  929. case '/':
  930. ch = getch();
  931. if (ch == '/') {
  932. pp->inComment = true;
  933. do {
  934. ch = getch();
  935. } while (ch != '\n' && ch != EndOfInput);
  936. ppToken->space = true;
  937. pp->inComment = false;
  938. return ch;
  939. } else if (ch == '*') {
  940. ch = getch();
  941. do {
  942. while (ch != '*') {
  943. if (ch == EndOfInput) {
  944. pp->parseContext.ppError(ppToken->loc, "End of input in comment", "comment", "");
  945. return ch;
  946. }
  947. ch = getch();
  948. }
  949. ch = getch();
  950. if (ch == EndOfInput) {
  951. pp->parseContext.ppError(ppToken->loc, "End of input in comment", "comment", "");
  952. return ch;
  953. }
  954. } while (ch != '/');
  955. ppToken->space = true;
  956. // loop again to get the next token...
  957. break;
  958. } else if (ch == '=') {
  959. return PPAtomDivAssign;
  960. } else {
  961. ungetch();
  962. return '/';
  963. }
  964. break;
  965. case '\'':
  966. return pp->characterLiteral(ppToken);
  967. case '"':
  968. // #include uses scanHeaderName() to ignore these escape sequences.
  969. ch = getch();
  970. while (ch != '"' && ch != '\n' && ch != EndOfInput) {
  971. if (len < MaxTokenLength) {
  972. if (ch == '\\' && !pp->disableEscapeSequences) {
  973. int nextCh = getch();
  974. switch (nextCh) {
  975. case '\'': ch = 0x27; break;
  976. case '"': ch = 0x22; break;
  977. case '?': ch = 0x3f; break;
  978. case '\\': ch = 0x5c; break;
  979. case 'a': ch = 0x07; break;
  980. case 'b': ch = 0x08; break;
  981. case 'f': ch = 0x0c; break;
  982. case 'n': ch = 0x0a; break;
  983. case 'r': ch = 0x0d; break;
  984. case 't': ch = 0x09; break;
  985. case 'v': ch = 0x0b; break;
  986. case 'x':
  987. // Hex value, arbitrary number of characters. Terminated by the first
  988. // non-hex digit
  989. {
  990. int numDigits = 0;
  991. ch = 0;
  992. while (true) {
  993. nextCh = getch();
  994. if (nextCh >= '0' && nextCh <= '9')
  995. nextCh -= '0';
  996. else if (nextCh >= 'A' && nextCh <= 'F')
  997. nextCh -= 'A' - 10;
  998. else if (nextCh >= 'a' && nextCh <= 'f')
  999. nextCh -= 'a' - 10;
  1000. else {
  1001. ungetch();
  1002. break;
  1003. }
  1004. numDigits++;
  1005. ch = ch * 0x10 + nextCh;
  1006. }
  1007. if (numDigits == 0) {
  1008. pp->parseContext.ppError(ppToken->loc, "Expected hex value in escape sequence", "string", "");
  1009. }
  1010. break;
  1011. }
  1012. case '0':
  1013. case '1':
  1014. case '2':
  1015. case '3':
  1016. case '4':
  1017. case '5':
  1018. case '6':
  1019. case '7':
  1020. // Octal value, up to three octal digits
  1021. {
  1022. int numDigits = 1;
  1023. ch = nextCh - '0';
  1024. while (numDigits < 3) {
  1025. nextCh = getch();
  1026. if (nextCh >= '0' && nextCh <= '7')
  1027. nextCh -= '0';
  1028. else {
  1029. ungetch();
  1030. break;
  1031. }
  1032. numDigits++;
  1033. ch = ch * 8 + nextCh;
  1034. }
  1035. break;
  1036. }
  1037. default:
  1038. pp->parseContext.ppError(ppToken->loc, "Invalid escape sequence", "string", "");
  1039. break;
  1040. }
  1041. }
  1042. ppToken->name[len] = (char)ch;
  1043. len++;
  1044. ch = getch();
  1045. } else
  1046. break;
  1047. };
  1048. ppToken->name[len] = '\0';
  1049. if (ch != '"') {
  1050. ungetch();
  1051. pp->parseContext.ppError(ppToken->loc, "End of line in string", "string", "");
  1052. }
  1053. return PpAtomConstString;
  1054. case ':':
  1055. ch = getch();
  1056. if (ch == ':')
  1057. return PpAtomColonColon;
  1058. ungetch();
  1059. return ':';
  1060. }
  1061. ch = getch();
  1062. }
  1063. }
  1064. //
  1065. // The main functional entry point into the preprocessor, which will
  1066. // scan the source strings to figure out and return the next processing token.
  1067. //
  1068. // Return the token, or EndOfInput when no more tokens.
  1069. //
  1070. int TPpContext::tokenize(TPpToken& ppToken)
  1071. {
  1072. for(;;) {
  1073. int token = scanToken(&ppToken);
  1074. // Handle token-pasting logic
  1075. token = tokenPaste(token, ppToken);
  1076. if (token == EndOfInput) {
  1077. missingEndifCheck();
  1078. return EndOfInput;
  1079. }
  1080. if (token == '#') {
  1081. if (previous_token == '\n') {
  1082. token = readCPPline(&ppToken);
  1083. if (token == EndOfInput) {
  1084. missingEndifCheck();
  1085. return EndOfInput;
  1086. }
  1087. continue;
  1088. } else {
  1089. parseContext.ppError(ppToken.loc, "preprocessor directive cannot be preceded by another token", "#", "");
  1090. return EndOfInput;
  1091. }
  1092. }
  1093. previous_token = token;
  1094. if (token == '\n')
  1095. continue;
  1096. // expand macros
  1097. if (token == PpAtomIdentifier) {
  1098. switch (MacroExpand(&ppToken, false, true)) {
  1099. case MacroExpandNotStarted:
  1100. break;
  1101. case MacroExpandError:
  1102. return EndOfInput;
  1103. case MacroExpandStarted:
  1104. case MacroExpandUndef:
  1105. continue;
  1106. }
  1107. }
  1108. switch (token) {
  1109. case PpAtomIdentifier:
  1110. case PpAtomConstInt:
  1111. case PpAtomConstUint:
  1112. case PpAtomConstFloat:
  1113. case PpAtomConstInt64:
  1114. case PpAtomConstUint64:
  1115. case PpAtomConstInt16:
  1116. case PpAtomConstUint16:
  1117. case PpAtomConstDouble:
  1118. case PpAtomConstFloat16:
  1119. if (ppToken.name[0] == '\0')
  1120. continue;
  1121. break;
  1122. case PpAtomConstString:
  1123. // HLSL allows string literals.
  1124. // GLSL allows string literals with GL_EXT_debug_printf.
  1125. if (ifdepth == 0 && parseContext.intermediate.getSource() != EShSourceHlsl) {
  1126. parseContext.requireExtensions(ppToken.loc, 1, &E_GL_EXT_debug_printf, "string literal");
  1127. if (!parseContext.extensionTurnedOn(E_GL_EXT_debug_printf))
  1128. continue;
  1129. }
  1130. break;
  1131. case '\'':
  1132. parseContext.ppError(ppToken.loc, "character literals not supported", "\'", "");
  1133. continue;
  1134. default:
  1135. snprintf(ppToken.name, sizeof(ppToken.name), "%s", atomStrings.getString(token));
  1136. break;
  1137. }
  1138. return token;
  1139. }
  1140. }
  1141. //
  1142. // Do all token-pasting related combining of two pasted tokens when getting a
  1143. // stream of tokens from a replacement list. Degenerates to no processing if a
  1144. // replacement list is not the source of the token stream.
  1145. //
  1146. int TPpContext::tokenPaste(int token, TPpToken& ppToken)
  1147. {
  1148. // starting with ## is illegal, skip to next token
  1149. if (token == PpAtomPaste) {
  1150. parseContext.ppError(ppToken.loc, "unexpected location", "##", "");
  1151. return scanToken(&ppToken);
  1152. }
  1153. int resultToken = token; // "foo" pasted with "35" is an identifier, not a number
  1154. // ## can be chained, process all in the chain at once
  1155. while (peekPasting()) {
  1156. TPpToken pastedPpToken;
  1157. // next token has to be ##
  1158. token = scanToken(&pastedPpToken);
  1159. assert(token == PpAtomPaste);
  1160. // This covers end of macro expansion
  1161. if (endOfReplacementList()) {
  1162. parseContext.ppError(ppToken.loc, "unexpected location; end of replacement list", "##", "");
  1163. break;
  1164. }
  1165. // Get the token(s) after the ##.
  1166. // Because of "space" semantics, and prior tokenization, what
  1167. // appeared a single token, e.g. "3A", might have been tokenized
  1168. // into two tokens "3" and "A", but the "A" will have 'space' set to
  1169. // false. Accumulate all of these to recreate the original lexical
  1170. // appearing token.
  1171. do {
  1172. token = scanToken(&pastedPpToken);
  1173. // This covers end of argument expansion
  1174. if (token == tMarkerInput::marker) {
  1175. parseContext.ppError(ppToken.loc, "unexpected location; end of argument", "##", "");
  1176. return resultToken;
  1177. }
  1178. // get the token text
  1179. switch (resultToken) {
  1180. case PpAtomIdentifier:
  1181. // already have the correct text in token.names
  1182. break;
  1183. case '=':
  1184. case '!':
  1185. case '-':
  1186. case '~':
  1187. case '+':
  1188. case '*':
  1189. case '/':
  1190. case '%':
  1191. case '<':
  1192. case '>':
  1193. case '|':
  1194. case '^':
  1195. case '&':
  1196. case PpAtomRight:
  1197. case PpAtomLeft:
  1198. case PpAtomAnd:
  1199. case PpAtomOr:
  1200. case PpAtomXor:
  1201. snprintf(ppToken.name, sizeof(ppToken.name), "%s", atomStrings.getString(resultToken));
  1202. snprintf(pastedPpToken.name, sizeof(pastedPpToken.name), "%s", atomStrings.getString(token));
  1203. break;
  1204. default:
  1205. parseContext.ppError(ppToken.loc, "not supported for these tokens", "##", "");
  1206. return resultToken;
  1207. }
  1208. // combine the tokens
  1209. if (strlen(ppToken.name) + strlen(pastedPpToken.name) > MaxTokenLength) {
  1210. parseContext.ppError(ppToken.loc, "combined tokens are too long", "##", "");
  1211. return resultToken;
  1212. }
  1213. snprintf(&ppToken.name[0] + strlen(ppToken.name), sizeof(ppToken.name) - strlen(ppToken.name),
  1214. "%s", pastedPpToken.name);
  1215. // correct the kind of token we are making, if needed (identifiers stay identifiers)
  1216. if (resultToken != PpAtomIdentifier) {
  1217. int newToken = atomStrings.getAtom(ppToken.name);
  1218. if (newToken > 0)
  1219. resultToken = newToken;
  1220. else
  1221. parseContext.ppError(ppToken.loc, "combined token is invalid", "##", "");
  1222. }
  1223. } while (peekContinuedPasting(resultToken));
  1224. }
  1225. return resultToken;
  1226. }
  1227. // Checks if we've seen balanced #if...#endif
  1228. void TPpContext::missingEndifCheck()
  1229. {
  1230. if (ifdepth > 0)
  1231. parseContext.ppError(parseContext.getCurrentLoc(), "missing #endif", "", "");
  1232. }
  1233. } // end namespace glslang