glsl_ast.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. #include "glsl_ast.h"
  2. #include "glsl_parser.h"
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. static const char *token_to_str[4096] = {
  7. [CONST] = "const",
  8. [BOOL] = "bool",
  9. [FLOAT] = "float",
  10. [DOUBLE] = "double",
  11. [INT] = "int",
  12. [UINT] = "uint",
  13. [BREAK] = "break",
  14. [CONTINUE] = "continue",
  15. [DO] = "do",
  16. [ELSE] = "else",
  17. [FOR] = "for",
  18. [IF] = "if",
  19. [DISCARD] = "discard",
  20. [RETURN] = "return",
  21. [TRUE_VALUE] = "true",
  22. [FALSE_VALUE] = "false",
  23. [SWITCH] = "switch",
  24. [CASE] = "case",
  25. [DEFAULT] = "default",
  26. [SUBROUTINE] = "subroutine",
  27. [BVEC2] = "bvec2",
  28. [BVEC3] = "bvec3",
  29. [BVEC4] = "bvec4",
  30. [IVEC2] = "ivec2",
  31. [IVEC3] = "ivec3",
  32. [IVEC4] = "ivec4",
  33. [UVEC2] = "uvec2",
  34. [UVEC3] = "uvec3",
  35. [UVEC4] = "uvec4",
  36. [VEC2] = "vec2",
  37. [VEC3] = "vec3",
  38. [VEC4] = "vec4",
  39. [MAT2] = "mat2",
  40. [MAT3] = "mat3",
  41. [MAT4] = "mat4",
  42. [CENTROID] = "centroid",
  43. [IN] = "in",
  44. [OUT] = "out",
  45. [INOUT] = "inout",
  46. [UNIFORM] = "uniform",
  47. [PATCH] = "patch",
  48. [SAMPLE] = "sample",
  49. [BUFFER] = "buffer",
  50. [SHARED] = "shared",
  51. [COHERENT] = "coherent",
  52. [VOLATILE] = "volatile",
  53. [RESTRICT] = "restrict",
  54. [READONLY] = "readonly",
  55. [WRITEONLY] = "writeonly",
  56. [DVEC2] = "dvec2",
  57. [DVEC3] = "dvec3",
  58. [DVEC4] = "dvec4",
  59. [DMAT2] = "dmat2",
  60. [DMAT3] = "dmat3",
  61. [DMAT4] = "dmat4",
  62. [NOPERSPECTIVE] = "noperspective",
  63. [FLAT] = "flat",
  64. [SMOOTH] = "smooth",
  65. [LAYOUT] = "layout",
  66. [MAT2X2] = "mat2x2",
  67. [MAT2X3] = "mat2x3",
  68. [MAT2X4] = "mat2x4",
  69. [MAT3X2] = "mat3x2",
  70. [MAT3X3] = "mat3x3",
  71. [MAT3X4] = "mat3x4",
  72. [MAT4X2] = "mat4x2",
  73. [MAT4X3] = "mat4x3",
  74. [MAT4X4] = "mat4x4",
  75. [DMAT2X2] = "dmat2x2",
  76. [DMAT2X3] = "dmat2x3",
  77. [DMAT2X4] = "dmat2x4",
  78. [DMAT3X2] = "dmat3x2",
  79. [DMAT3X3] = "dmat3x3",
  80. [DMAT3X4] = "dmat3x4",
  81. [DMAT4X2] = "dmat4x2",
  82. [DMAT4X3] = "dmat4x3",
  83. [DMAT4X4] = "dmat4x4",
  84. [ATOMIC_UINT] = "atomic_uint",
  85. [SAMPLER1D] = "sampler1D",
  86. [SAMPLER2D] = "sampler2D",
  87. [SAMPLER3D] = "sampler3D",
  88. [SAMPLERCUBE] = "samplercube",
  89. [SAMPLER1DSHADOW] = "sampler1Dshadow",
  90. [SAMPLER2DSHADOW] = "sampler2Dshadow",
  91. [SAMPLERCUBESHADOW] = "samplercubeshadow",
  92. [SAMPLER1DARRAY] = "sampler1Darray",
  93. [SAMPLER2DARRAY] = "sampler2Darray",
  94. [SAMPLER1DARRAYSHADOW] = "sampler1Darrayshadow",
  95. [SAMPLER2DARRAYSHADOW] = "sampler2Darrayshadow",
  96. [ISAMPLER1D] = "isampler1D",
  97. [ISAMPLER2D] = "isampler2D",
  98. [ISAMPLER3D] = "isampler3D",
  99. [ISAMPLERCUBE] = "isamplercube",
  100. [ISAMPLER1DARRAY] = "isampler1Darray",
  101. [ISAMPLER2DARRAY] = "isampler2Darray",
  102. [USAMPLER1D] = "usampler1D",
  103. [USAMPLER2D] = "usampler2D",
  104. [USAMPLER3D] = "usampler3D",
  105. [USAMPLERCUBE] = "usamplercube",
  106. [USAMPLER1DARRAY] = "usampler1Darray",
  107. [USAMPLER2DARRAY] = "usampler2Darray",
  108. [SAMPLER2DRECT] = "sampler2Drect",
  109. [SAMPLER2DRECTSHADOW] = "sampler2Drectshadow",
  110. [ISAMPLER2DRECT] = "isampler2Drect",
  111. [USAMPLER2DRECT] = "usampler2Drect",
  112. [SAMPLERBUFFER] = "samplerbuffer",
  113. [ISAMPLERBUFFER] = "isamplerbuffer",
  114. [USAMPLERBUFFER] = "usamplerbuffer",
  115. [SAMPLERCUBEARRAY] = "samplercubearray",
  116. [SAMPLERCUBEARRAYSHADOW] = "samplercubearrayshadow",
  117. [ISAMPLERCUBEARRAY] = "isamplercubearray",
  118. [USAMPLERCUBEARRAY] = "usamplercubearray",
  119. [SAMPLER2DMS] = "sampler2Dms",
  120. [ISAMPLER2DMS] = "isampler2Dms",
  121. [USAMPLER2DMS] = "usampler2Dms",
  122. [SAMPLER2DMSARRAY] = "sampler2Dmsarray",
  123. [ISAMPLER2DMSARRAY] = "isampler2Dmsarray",
  124. [USAMPLER2DMSARRAY] = "usampler2Dmsarray",
  125. [IMAGE1D] = "image1D",
  126. [IIMAGE1D] = "iimage1D",
  127. [UIMAGE1D] = "uimage1D",
  128. [IMAGE2D] = "image2D",
  129. [IIMAGE2D] = "iimage2D",
  130. [UIMAGE2D] = "uimage2D",
  131. [IMAGE3D] = "image3D",
  132. [IIMAGE3D] = "iimage3D",
  133. [UIMAGE3D] = "uimage3D",
  134. [IMAGE2DRECT] = "image2Drect",
  135. [IIMAGE2DRECT] = "iimage2Drect",
  136. [UIMAGE2DRECT] = "uimage2Drect",
  137. [IMAGECUBE] = "imagecube",
  138. [IIMAGECUBE] = "iimagecube",
  139. [UIMAGECUBE] = "uimagecube",
  140. [IMAGEBUFFER] = "imagebuffer",
  141. [IIMAGEBUFFER] = "iimagebuffer",
  142. [UIMAGEBUFFER] = "uimagebuffer",
  143. [IMAGE1DARRAY] = "image1Darray",
  144. [IIMAGE1DARRAY] = "iimage1Darray",
  145. [UIMAGE1DARRAY] = "uimage1Darray",
  146. [IMAGE2DARRAY] = "image2Darray",
  147. [IIMAGE2DARRAY] = "iimage2Darray",
  148. [UIMAGE2DARRAY] = "uimage2Darray",
  149. [IMAGECUBEARRAY] = "imagecubearray",
  150. [IIMAGECUBEARRAY] = "iimagecubearray",
  151. [UIMAGECUBEARRAY] = "uimagecubearray",
  152. [IMAGE2DMS] = "image2Dms",
  153. [IIMAGE2DMS] = "iimage2Dms",
  154. [UIMAGE2DMS] = "uimage2Dms",
  155. [IMAGE2DMSARRAY] = "image2Dmsarray",
  156. [IIMAGE2DMSARRAY] = "iimage2Dmsarray",
  157. [UIMAGE2DMSARRAY] = "uimage2Dmsarray",
  158. [STRUCT] = "struct",
  159. [VOID] = "void",
  160. [WHILE] = "while",
  161. [LEFT_OP] = "<<",
  162. [RIGHT_OP] = ">>",
  163. [PRE_INC_OP] = "++",
  164. [PRE_DEC_OP] = "--",
  165. [POST_INC_OP] = "++",
  166. [POST_DEC_OP] = "--",
  167. [LE_OP] = "<=",
  168. [GE_OP] = ">=",
  169. [EQ_OP] = "==",
  170. [NE_OP] = "!=",
  171. [AND_OP] = "&&",
  172. [OR_OP] = "||",
  173. [XOR_OP] = "^^",
  174. [MUL_ASSIGN] = "*=",
  175. [DIV_ASSIGN] = "/=",
  176. [ADD_ASSIGN] = "+=",
  177. [MOD_ASSIGN] = "%=",
  178. [LEFT_ASSIGN] = "<<=",
  179. [RIGHT_ASSIGN] = ">>=",
  180. [AND_ASSIGN] = "&=",
  181. [XOR_ASSIGN] = "^=",
  182. [OR_ASSIGN] = "|=",
  183. [SUB_ASSIGN] = "-=",
  184. [LEFT_PAREN] = "(",
  185. [RIGHT_PAREN] = ")",
  186. [LEFT_BRACKET] = "[",
  187. [RIGHT_BRACKET] = "]",
  188. [LEFT_BRACE] = "{",
  189. [RIGHT_BRACE] = "}",
  190. [DOT] = ".",
  191. [COMMA] = ",",
  192. [COLON] = ":",
  193. [EQUAL] = "=",
  194. [SEMICOLON] = ";",
  195. [BANG] = "!",
  196. [DASH] = "-",
  197. [TILDE] = "~",
  198. [PLUS] = "+",
  199. [STAR] = "*",
  200. [SLASH] = "/",
  201. [PERCENT] = "%",
  202. [LEFT_ANGLE] = "<",
  203. [RIGHT_ANGLE] = ">",
  204. [VERTICAL_BAR] = "|",
  205. [CARET] = "^",
  206. [AMPERSAND] = "&",
  207. [QUESTION] = "?",
  208. [INVARIANT] = "invariant",
  209. [PRECISE] = "precise",
  210. [HIGHP] = "highp",
  211. [MEDIUMP] = "mediump",
  212. [LOWP] = "lowp",
  213. [PRECISION] = "precision",
  214. [UNARY_PLUS] = "+",
  215. [UNARY_DASH] = "-",
  216. [NUM_GLSL_TOKEN] = ""
  217. };
  218. static const char *code_to_str[4096] = {
  219. [CONST] = "CONST",
  220. [BOOL] = "BOOL",
  221. [FLOAT] = "FLOAT",
  222. [DOUBLE] = "DOUBLE",
  223. [INT] = "INT",
  224. [UINT] = "UINT",
  225. [BREAK] = "BREAK",
  226. [CONTINUE] = "CONTINUE",
  227. [DO] = "DO",
  228. [ELSE] = "ELSE",
  229. [FOR] = "FOR",
  230. [IF] = "IF",
  231. [DISCARD] = "DISCARD",
  232. [RETURN] = "RETURN",
  233. [RETURN_VALUE] = "RETURN_VALUE",
  234. [SWITCH] = "SWITCH",
  235. [CASE] = "CASE",
  236. [DEFAULT] = "DEFAULT",
  237. [SUBROUTINE] = "SUBROUTINE",
  238. [BVEC2] = "BVEC2",
  239. [BVEC3] = "BVEC3",
  240. [BVEC4] = "BVEC4",
  241. [IVEC2] = "IVEC2",
  242. [IVEC3] = "IVEC3",
  243. [IVEC4] = "IVEC4",
  244. [UVEC2] = "UVEC2",
  245. [UVEC3] = "UVEC3",
  246. [UVEC4] = "UVEC4",
  247. [VEC2] = "VEC2",
  248. [VEC3] = "VEC3",
  249. [VEC4] = "VEC4",
  250. [MAT2] = "MAT2",
  251. [MAT3] = "MAT3",
  252. [MAT4] = "MAT4",
  253. [CENTROID] = "CENTROID",
  254. [IN] = "IN",
  255. [OUT] = "OUT",
  256. [INOUT] = "INOUT",
  257. [UNIFORM] = "UNIFORM",
  258. [PATCH] = "PATCH",
  259. [SAMPLE] = "SAMPLE",
  260. [BUFFER] = "BUFFER",
  261. [SHARED] = "SHARED",
  262. [COHERENT] = "COHERENT",
  263. [VOLATILE] = "VOLATILE",
  264. [RESTRICT] = "RESTRICT",
  265. [READONLY] = "READONLY",
  266. [WRITEONLY] = "WRITEONLY",
  267. [DVEC2] = "DVEC2",
  268. [DVEC3] = "DVEC3",
  269. [DVEC4] = "DVEC4",
  270. [DMAT2] = "DMAT2",
  271. [DMAT3] = "DMAT3",
  272. [DMAT4] = "DMAT4",
  273. [NOPERSPECTIVE] = "NOPERSPECTIVE",
  274. [FLAT] = "FLAT",
  275. [SMOOTH] = "SMOOTH",
  276. [LAYOUT] = "LAYOUT",
  277. [MAT2X2] = "MAT2X2",
  278. [MAT2X3] = "MAT2X3",
  279. [MAT2X4] = "MAT2X4",
  280. [MAT3X2] = "MAT3X2",
  281. [MAT3X3] = "MAT3X3",
  282. [MAT3X4] = "MAT3X4",
  283. [MAT4X2] = "MAT4X2",
  284. [MAT4X3] = "MAT4X3",
  285. [MAT4X4] = "MAT4X4",
  286. [DMAT2X2] = "DMAT2X2",
  287. [DMAT2X3] = "DMAT2X3",
  288. [DMAT2X4] = "DMAT2X4",
  289. [DMAT3X2] = "DMAT3X2",
  290. [DMAT3X3] = "DMAT3X3",
  291. [DMAT3X4] = "DMAT3X4",
  292. [DMAT4X2] = "DMAT4X2",
  293. [DMAT4X3] = "DMAT4X3",
  294. [DMAT4X4] = "DMAT4X4",
  295. [ATOMIC_UINT] = "ATOMIC_UINT",
  296. [SAMPLER1D] = "SAMPLER1D",
  297. [SAMPLER2D] = "SAMPLER2D",
  298. [SAMPLER3D] = "SAMPLER3D",
  299. [SAMPLERCUBE] = "SAMPLERCUBE",
  300. [SAMPLER1DSHADOW] = "SAMPLER1DSHADOW",
  301. [SAMPLER2DSHADOW] = "SAMPLER2DSHADOW",
  302. [SAMPLERCUBESHADOW] = "SAMPLERCUBESHADOW",
  303. [SAMPLER1DARRAY] = "SAMPLER1DARRAY",
  304. [SAMPLER2DARRAY] = "SAMPLER2DARRAY",
  305. [SAMPLER1DARRAYSHADOW] = "SAMPLER1DARRAYSHADOW",
  306. [SAMPLER2DARRAYSHADOW] = "SAMPLER2DARRAYSHADOW",
  307. [ISAMPLER1D] = "ISAMPLER1D",
  308. [ISAMPLER2D] = "ISAMPLER2D",
  309. [ISAMPLER3D] = "ISAMPLER3D",
  310. [ISAMPLERCUBE] = "ISAMPLERCUBE",
  311. [ISAMPLER1DARRAY] = "ISAMPLER1DARRAY",
  312. [ISAMPLER2DARRAY] = "ISAMPLER2DARRAY",
  313. [USAMPLER1D] = "USAMPLER1D",
  314. [USAMPLER2D] = "USAMPLER2D",
  315. [USAMPLER3D] = "USAMPLER3D",
  316. [USAMPLERCUBE] = "USAMPLERCUBE",
  317. [USAMPLER1DARRAY] = "USAMPLER1DARRAY",
  318. [USAMPLER2DARRAY] = "USAMPLER2DARRAY",
  319. [SAMPLER2DRECT] = "SAMPLER2DRECT",
  320. [SAMPLER2DRECTSHADOW] = "SAMPLER2DRECTSHADOW",
  321. [ISAMPLER2DRECT] = "ISAMPLER2DRECT",
  322. [USAMPLER2DRECT] = "USAMPLER2DRECT",
  323. [SAMPLERBUFFER] = "SAMPLERBUFFER",
  324. [ISAMPLERBUFFER] = "ISAMPLERBUFFER",
  325. [USAMPLERBUFFER] = "USAMPLERBUFFER",
  326. [SAMPLERCUBEARRAY] = "SAMPLERCUBEARRAY",
  327. [SAMPLERCUBEARRAYSHADOW] = "SAMPLERCUBEARRAYSHADOW",
  328. [ISAMPLERCUBEARRAY] = "ISAMPLERCUBEARRAY",
  329. [USAMPLERCUBEARRAY] = "USAMPLERCUBEARRAY",
  330. [SAMPLER2DMS] = "SAMPLER2DMS",
  331. [ISAMPLER2DMS] = "ISAMPLER2DMS",
  332. [USAMPLER2DMS] = "USAMPLER2DMS",
  333. [SAMPLER2DMSARRAY] = "SAMPLER2DMSARRAY",
  334. [ISAMPLER2DMSARRAY] = "ISAMPLER2DMSARRAY",
  335. [USAMPLER2DMSARRAY] = "USAMPLER2DMSARRAY",
  336. [IMAGE1D] = "IMAGE1D",
  337. [IIMAGE1D] = "IIMAGE1D",
  338. [UIMAGE1D] = "UIMAGE1D",
  339. [IMAGE2D] = "IMAGE2D",
  340. [IIMAGE2D] = "IIMAGE2D",
  341. [UIMAGE2D] = "UIMAGE2D",
  342. [IMAGE3D] = "IMAGE3D",
  343. [IIMAGE3D] = "IIMAGE3D",
  344. [UIMAGE3D] = "UIMAGE3D",
  345. [IMAGE2DRECT] = "IMAGE2DRECT",
  346. [IIMAGE2DRECT] = "IIMAGE2DRECT",
  347. [UIMAGE2DRECT] = "UIMAGE2DRECT",
  348. [IMAGECUBE] = "IMAGECUBE",
  349. [IIMAGECUBE] = "IIMAGECUBE",
  350. [UIMAGECUBE] = "UIMAGECUBE",
  351. [IMAGEBUFFER] = "IMAGEBUFFER",
  352. [IIMAGEBUFFER] = "IIMAGEBUFFER",
  353. [UIMAGEBUFFER] = "UIMAGEBUFFER",
  354. [IMAGE1DARRAY] = "IMAGE1DARRAY",
  355. [IIMAGE1DARRAY] = "IIMAGE1DARRAY",
  356. [UIMAGE1DARRAY] = "UIMAGE1DARRAY",
  357. [IMAGE2DARRAY] = "IMAGE2DARRAY",
  358. [IIMAGE2DARRAY] = "IIMAGE2DARRAY",
  359. [UIMAGE2DARRAY] = "UIMAGE2DARRAY",
  360. [IMAGECUBEARRAY] = "IMAGECUBEARRAY",
  361. [IIMAGECUBEARRAY] = "IIMAGECUBEARRAY",
  362. [UIMAGECUBEARRAY] = "UIMAGECUBEARRAY",
  363. [IMAGE2DMS] = "IMAGE2DMS",
  364. [IIMAGE2DMS] = "IIMAGE2DMS",
  365. [UIMAGE2DMS] = "UIMAGE2DMS",
  366. [IMAGE2DMSARRAY] = "IMAGE2DMSARRAY",
  367. [IIMAGE2DMSARRAY] = "IIMAGE2DMSARRAY",
  368. [UIMAGE2DMSARRAY] = "UIMAGE2DMSARRAY",
  369. [STRUCT] = "STRUCT",
  370. [VOID] = "VOID",
  371. [WHILE] = "WHILE",
  372. [TRUE_VALUE] = "TRUE_VALUE",
  373. [FALSE_VALUE] = "FALSE_VALUE",
  374. [LEFT_OP] = "LEFT_OP",
  375. [RIGHT_OP] = "RIGHT_OP",
  376. [INC_OP] = "INC_OP",
  377. [DEC_OP] = "DEC_OP",
  378. [LE_OP] = "LE_OP",
  379. [GE_OP] = "GE_OP",
  380. [EQ_OP] = "EQ_OP",
  381. [NE_OP] = "NE_OP",
  382. [AND_OP] = "AND_OP",
  383. [OR_OP] = "OR_OP",
  384. [XOR_OP] = "XOR_OP",
  385. [MUL_ASSIGN] = "MUL_ASSIGN",
  386. [DIV_ASSIGN] = "DIV_ASSIGN",
  387. [ADD_ASSIGN] = "ADD_ASSIGN",
  388. [MOD_ASSIGN] = "MOD_ASSIGN",
  389. [LEFT_ASSIGN] = "LEFT_ASSIGN",
  390. [RIGHT_ASSIGN] = "RIGHT_ASSIGN",
  391. [AND_ASSIGN] = "AND_ASSIGN",
  392. [XOR_ASSIGN] = "XOR_ASSIGN",
  393. [OR_ASSIGN] = "OR_ASSIGN",
  394. [SUB_ASSIGN] = "SUB_ASSIGN",
  395. [LEFT_PAREN] = "LEFT_PAREN",
  396. [RIGHT_PAREN] = "RIGHT_PAREN",
  397. [LEFT_BRACKET] = "LEFT_BRACKET",
  398. [RIGHT_BRACKET] = "RIGHT_BRACKET",
  399. [LEFT_BRACE] = "LEFT_BRACE",
  400. [RIGHT_BRACE] = "RIGHT_BRACE",
  401. [DOT] = "DOT",
  402. [COMMA] = "COMMA",
  403. [COLON] = "COLON",
  404. [EQUAL] = "EQUAL",
  405. [SEMICOLON] = "SEMICOLON",
  406. [BANG] = "BANG",
  407. [DASH] = "DASH",
  408. [TILDE] = "TILDE",
  409. [PLUS] = "PLUS",
  410. [STAR] = "STAR",
  411. [SLASH] = "SLASH",
  412. [PERCENT] = "PERCENT",
  413. [LEFT_ANGLE] = "LEFT_ANGLE",
  414. [RIGHT_ANGLE] = "RIGHT_ANGLE",
  415. [VERTICAL_BAR] = "VERTICAL_BAR",
  416. [CARET] = "CARET",
  417. [AMPERSAND] = "AMPERSAND",
  418. [QUESTION] = "QUESTION",
  419. [INVARIANT] = "INVARIANT",
  420. [PRECISE] = "PRECISE",
  421. [HIGHP] = "HIGHP",
  422. [MEDIUMP] = "MEDIUMP",
  423. [LOWP] = "LOWP",
  424. [PRECISION] = "PRECISION",
  425. [UNARY_PLUS] = "UNARY_PLUS",
  426. [UNARY_DASH] = "UNARY_DASH",
  427. [PRE_INC_OP] = "PRE_INC_OP",
  428. [PRE_DEC_OP] = "PRE_DEC_OP",
  429. [POST_DEC_OP] = "POST_DEC_OP",
  430. [POST_INC_OP] = "POST_INC_OP",
  431. [ARRAY_REF_OP] = "ARRAY_REF_OP",
  432. [FUNCTION_CALL] = "FUNCTION_CALL",
  433. [TYPE_NAME_LIST] = "TYPE_NAME_LIST",
  434. [TYPE_SPECIFIER] = "TYPE_SPECIFIER",
  435. [POSTFIX_EXPRESSION] = "POSTFIX_EXPRESSION",
  436. [TYPE_QUALIFIER_LIST] = "TYPE_QUALIFIER_LIST",
  437. [STRUCT_DECLARATION] = "STRUCT_DECLARATION",
  438. [STRUCT_DECLARATOR] = "STRUCT_DECLARATOR",
  439. [STRUCT_SPECIFIER] = "STRUCT_SPECIFIER",
  440. [FUNCTION_DEFINITION] = "FUNCTION_DEFINITION",
  441. [DECLARATION] = "DECLARATION",
  442. [STATEMENT_LIST] = "STATEMENT_LIST",
  443. [TRANSLATION_UNIT] = "TRANSLATION_UNIT",
  444. [PRECISION_DECLARATION] = "PRECISION_DECLARATION",
  445. [BLOCK_DECLARATION] = "BLOCK_DECLARATION",
  446. [TYPE_QUALIFIER_DECLARATION] = "TYPE_QUALIFIER_DECLARATION",
  447. [IDENTIFIER_LIST] = "IDENTIFIER_LIST",
  448. [INIT_DECLARATOR_LIST] = "INIT_DECLARATOR_LIST",
  449. [FULLY_SPECIFIED_TYPE] = "FULLY_SPECIFIED_TYPE",
  450. [SINGLE_DECLARATION] = "SINGLE_DECLARATION",
  451. [SINGLE_INIT_DECLARATION] = "SINGLE_INIT_DECLARATION",
  452. [INITIALIZER_LIST] = "INITIALIZER_LIST",
  453. [EXPRESSION_STATEMENT] = "EXPRESSION_STATEMENT",
  454. [SELECTION_STATEMENT] = "SELECTION_STATEMENT",
  455. [SELECTION_STATEMENT_ELSE] = "SELECTION_STATEMENT_ELSE",
  456. [SWITCH_STATEMENT] = "SWITCH_STATEMENT",
  457. [FOR_REST_STATEMENT] = "FOR_REST_STATEMENT",
  458. [WHILE_STATEMENT] = "WHILE_STATEMENT",
  459. [DO_STATEMENT] = "DO_STATEMENT",
  460. [FOR_STATEMENT] = "FOR_STATEMENT",
  461. [CASE_LABEL] = "CASE_LABEL",
  462. [CONDITION_OPT] = "CONDITION_OPT",
  463. [ASSIGNMENT_CONDITION] = "ASSIGNMENT_CONDITION",
  464. [EXPRESSION_CONDITION] = "EXPRESSION_CONDITION",
  465. [FUNCTION_HEADER] = "FUNCTION_HEADER",
  466. [FUNCTION_DECLARATION] = "FUNCTION_DECLARATION",
  467. [FUNCTION_PARAMETER_LIST] = "FUNCTION_PARAMETER_LIST",
  468. [PARAMETER_DECLARATION] = "PARAMETER_DECLARATION",
  469. [PARAMETER_DECLARATOR] = "PARAMETER_DECLARATOR",
  470. [UNINITIALIZED_DECLARATION] = "UNINITIALIZED_DECLARATION",
  471. [ARRAY_SPECIFIER] = "ARRAY_SPECIFIER",
  472. [ARRAY_SPECIFIER_LIST] = "ARRAY_SPECIFIER_LIST",
  473. [STRUCT_DECLARATOR_LIST] = "STRUCT_DECLARATOR_LIST",
  474. [FUNCTION_CALL_PARAMETER_LIST] = "FUNCTION_CALL_PARAMETER_LIST",
  475. [STRUCT_DECLARATION_LIST] = "STRUCT_DECLARATION_LIST",
  476. [LAYOUT_QUALIFIER_ID] = "LAYOUT_QUALIFIER_ID",
  477. [LAYOUT_QUALIFIER_ID_LIST] = "LAYOUT_QUALIFIER_ID_LIST",
  478. [SUBROUTINE_TYPE] = "SUBROUTINE_TYPE",
  479. [PAREN_EXPRESSION] = "PAREN_EXPRESSION",
  480. [INIT_DECLARATOR] = "INIT_DECLARATOR",
  481. [INITIALIZER] = "INITIALIZER",
  482. [TERNARY_EXPRESSION] = "TERNARY_EXPRESSION",
  483. [SECTION] = "SECTION",
  484. [DECLARATION_STATEMENT_LIST] = "DECLARATION_STATEMENT_LIST",
  485. [SECTION_STATEMENT] = "SECTION_STATEMENT",
  486. [NUM_GLSL_TOKEN] = ""
  487. };
  488. bool glsl_ast_is_list_node(struct glsl_node *n)
  489. {
  490. switch(n->code) {
  491. case TYPE_NAME_LIST:
  492. case TYPE_QUALIFIER_LIST:
  493. case STATEMENT_LIST:
  494. case IDENTIFIER_LIST:
  495. case INIT_DECLARATOR_LIST:
  496. case INITIALIZER_LIST:
  497. case FUNCTION_PARAMETER_LIST:
  498. case ARRAY_SPECIFIER_LIST:
  499. case STRUCT_DECLARATOR_LIST:
  500. case STRUCT_DECLARATION_LIST:
  501. case TRANSLATION_UNIT:
  502. case FUNCTION_CALL_PARAMETER_LIST:
  503. case DECLARATION_STATEMENT_LIST:
  504. return true;
  505. default:
  506. return false;
  507. }
  508. }
  509. void glsl_ast_print(struct glsl_node *n, int depth)
  510. {
  511. int i;
  512. for (i = 0; i < depth; i++) {
  513. printf("\t");
  514. }
  515. if (code_to_str[n->code])
  516. printf("%s", code_to_str[n->code]);
  517. switch(n->code) {
  518. case IDENTIFIER:
  519. if (n->data.str) {
  520. if (code_to_str[n->code])
  521. printf(": ");
  522. printf("%s", n->data.str);
  523. }
  524. break;
  525. case FLOATCONSTANT:
  526. if (code_to_str[n->code])
  527. printf(": ");
  528. printf("%f", n->data.f);
  529. break;
  530. case DOUBLECONSTANT:
  531. if (code_to_str[n->code])
  532. printf(": ");
  533. printf("%f", n->data.d);
  534. break;
  535. case INTCONSTANT:
  536. if (code_to_str[n->code])
  537. printf(": ");
  538. printf("%d", n->data.i);
  539. break;
  540. case UINTCONSTANT:
  541. if (code_to_str[n->code])
  542. printf(": ");
  543. printf("%u", n->data.ui);
  544. break;
  545. }
  546. printf("\n");
  547. for (i = 0; i < n->child_count; i++) {
  548. glsl_ast_print((struct glsl_node *)n->children[i], depth + 1);
  549. }
  550. }
  551. void glsl_ast_walk_push_node(struct glsl_ast_walk_data *data, struct glsl_node *n)
  552. {
  553. data->node_stack[data->node_stack_level] = n;
  554. data->parent_stack[data->parent_stack_level] = n;
  555. data->parent_stack_idx[data->parent_stack_level] = data->node_stack_level;
  556. data->parent_stack_level++;
  557. data->node_stack_level++;
  558. }
  559. void glsl_ast_walk_init(struct glsl_ast_walk_data *data)
  560. {
  561. data->node_stack_level = 0;
  562. data->parent_stack_level = 0;
  563. }
  564. void glsl_ast_walk(struct glsl_ast_walk_data *data, intptr_t state,
  565. void (*enter_node)(struct glsl_ast_walk_data *data, struct glsl_node *n, intptr_t state),
  566. void (*exit_node)(struct glsl_ast_walk_data *data, struct glsl_node *n, intptr_t state))
  567. {
  568. while (1) {
  569. struct glsl_node *n;
  570. while (data->parent_stack_level && data->parent_stack_idx[data->parent_stack_level - 1] == data->node_stack_level) {
  571. data->parent_stack_level--;
  572. exit_node(data, data->parent_stack[data->parent_stack_level], state);
  573. }
  574. data->node_stack_level--;
  575. if (data->node_stack_level < 0)
  576. break;
  577. n = data->node_stack[data->node_stack_level];
  578. enter_node(data, n, state);
  579. }
  580. }
  581. static bool is_optional_list(struct glsl_node *n)
  582. {
  583. switch(n->code) {
  584. case ARRAY_SPECIFIER_LIST:
  585. case TYPE_QUALIFIER_LIST:
  586. return true;
  587. }
  588. return false;
  589. }
  590. struct string {
  591. char *s;
  592. int len;
  593. int capacity;
  594. };
  595. static void _glsl_ast_gen_glsl(struct glsl_node *n, struct string *out, int depth);
  596. static void string_cat(struct string *str, const char *format, ...)
  597. {
  598. int n;
  599. va_list vl;
  600. do {
  601. int left = str->capacity - str->len;
  602. va_start(vl, format);
  603. n = vsnprintf(str->s + str->len, left, format, vl);
  604. va_end(vl);
  605. if (n < left) {
  606. break;
  607. } else {
  608. str->capacity *= 2;
  609. str->s = realloc(str->s, str->capacity);
  610. }
  611. } while (1);
  612. str->len += n;
  613. }
  614. static void print_list_as_glsl(struct glsl_node *n, const char *prefix, const char *delim, const char *postfix, struct string *out, int depth)
  615. {
  616. int i, c = 0;
  617. string_cat(out,"%s", prefix);
  618. for (i = 0; i < n->child_count; i++) {
  619. if (!n->children[i]->child_count && is_optional_list(n->children[i]))
  620. continue;
  621. if (c)
  622. string_cat(out,"%s", delim);
  623. c++;
  624. _glsl_ast_gen_glsl(n->children[i], out, depth);
  625. }
  626. string_cat(out,"%s", postfix);
  627. }
  628. static void _glsl_ast_gen_glsl(struct glsl_node *n, struct string *out, int depth)
  629. {
  630. int i;
  631. int j;
  632. switch(n->code) {
  633. case IDENTIFIER:
  634. if (n->data.str)
  635. string_cat(out,"%s", n->data.str);
  636. break;
  637. case FLOATCONSTANT:
  638. string_cat(out,"%f", n->data.f);
  639. break;
  640. case DOUBLECONSTANT:
  641. string_cat(out,"%f", n->data.d);
  642. break;
  643. case INTCONSTANT:
  644. string_cat(out,"%d", n->data.i);
  645. break;
  646. case UINTCONSTANT:
  647. string_cat(out,"%u", n->data.ui);
  648. break;
  649. case TRANSLATION_UNIT:
  650. print_list_as_glsl(n, "", "\n", "\n", out, depth);
  651. break;
  652. case ARRAY_SPECIFIER_LIST:
  653. print_list_as_glsl(n, "", "", "", out, depth);
  654. break;
  655. case ARRAY_SPECIFIER:
  656. print_list_as_glsl(n, "[", "", "]", out, depth);
  657. break;
  658. case EQUAL:
  659. case MUL_ASSIGN:
  660. case DIV_ASSIGN:
  661. case MOD_ASSIGN:
  662. case ADD_ASSIGN:
  663. case SUB_ASSIGN:
  664. case LEFT_ASSIGN:
  665. case RIGHT_ASSIGN:
  666. case AND_ASSIGN:
  667. case XOR_ASSIGN:
  668. case OR_ASSIGN:
  669. case PLUS:
  670. case DASH:
  671. case STAR:
  672. case SLASH:
  673. case PERCENT:
  674. case AMPERSAND:
  675. case EQ_OP:
  676. case NE_OP:
  677. case LEFT_ANGLE:
  678. case RIGHT_ANGLE:
  679. case LE_OP:
  680. case GE_OP:
  681. case LEFT_OP:
  682. case RIGHT_OP:
  683. case CARET:
  684. case VERTICAL_BAR:
  685. case AND_OP:
  686. case OR_OP:
  687. case XOR_OP:
  688. _glsl_ast_gen_glsl(n->children[0], out, depth);
  689. if (token_to_str[n->code]) {
  690. string_cat(out," %s ", token_to_str[n->code]);
  691. } else {
  692. string_cat(out," <unknown operator %d> ", n->code);
  693. }
  694. _glsl_ast_gen_glsl(n->children[1], out, depth);
  695. break;
  696. case DOT:
  697. _glsl_ast_gen_glsl(n->children[0], out, depth);
  698. string_cat(out,".");
  699. _glsl_ast_gen_glsl(n->children[1], out, depth);
  700. break;
  701. case PRE_INC_OP:
  702. case PRE_DEC_OP:
  703. case UNARY_PLUS:
  704. case UNARY_DASH:
  705. case TILDE:
  706. case BANG:
  707. print_list_as_glsl(n, token_to_str[n->code], "", "", out, depth);
  708. break;
  709. case PAREN_EXPRESSION:
  710. print_list_as_glsl(n, "(", "", ")", out, depth);
  711. break;
  712. case POST_INC_OP:
  713. case POST_DEC_OP:
  714. print_list_as_glsl(n, "", "", token_to_str[n->code], out, depth);
  715. break;
  716. case FUNCTION_DECLARATION:
  717. case FUNCTION_HEADER:
  718. case FULLY_SPECIFIED_TYPE:
  719. case PARAMETER_DECLARATION:
  720. case PARAMETER_DECLARATOR:
  721. case TYPE_QUALIFIER_LIST:
  722. print_list_as_glsl(n, "", " ", "", out, depth);
  723. break;
  724. case FUNCTION_DEFINITION:
  725. print_list_as_glsl(n, "", " ", "\n", out, depth);
  726. break;
  727. case FUNCTION_CALL:
  728. _glsl_ast_gen_glsl(n->children[0], out, depth);
  729. _glsl_ast_gen_glsl(n->children[1], out, depth);
  730. break;
  731. case SELECTION_STATEMENT:
  732. string_cat(out,"if (");
  733. _glsl_ast_gen_glsl(n->children[0], out, depth);
  734. string_cat(out,") ");
  735. _glsl_ast_gen_glsl(n->children[1], out, depth);
  736. break;
  737. case ARRAY_REF_OP:
  738. _glsl_ast_gen_glsl(n->children[0], out, depth);
  739. string_cat(out,"[");
  740. _glsl_ast_gen_glsl(n->children[1], out, depth);
  741. string_cat(out,"]");
  742. break;
  743. case RETURN:
  744. string_cat(out,"return;\n");
  745. break;
  746. case RETURN_VALUE:
  747. string_cat(out,"return ");
  748. _glsl_ast_gen_glsl(n->children[0], out, depth);
  749. string_cat(out,";");
  750. break;
  751. case SELECTION_STATEMENT_ELSE:
  752. string_cat(out,"if (");
  753. _glsl_ast_gen_glsl(n->children[0], out, depth);
  754. string_cat(out,") ");
  755. _glsl_ast_gen_glsl(n->children[1], out, depth);
  756. string_cat(out," else ");
  757. _glsl_ast_gen_glsl(n->children[2], out, depth);
  758. string_cat(out,"\n");
  759. break;
  760. case SINGLE_DECLARATION:
  761. print_list_as_glsl(n, "", " ", "", out, depth);
  762. break;
  763. case SINGLE_INIT_DECLARATION:
  764. _glsl_ast_gen_glsl(n->children[0], out, depth);
  765. string_cat(out," ");
  766. _glsl_ast_gen_glsl(n->children[1], out, depth);
  767. string_cat(out," = ");
  768. _glsl_ast_gen_glsl(n->children[3], out, depth);
  769. break;
  770. case WHILE_STATEMENT:
  771. string_cat(out,"while (");
  772. _glsl_ast_gen_glsl(n->children[0], out, depth);
  773. string_cat(out,") ");
  774. _glsl_ast_gen_glsl(n->children[1], out, depth);
  775. break;
  776. case DO_STATEMENT:
  777. string_cat(out,"do ");
  778. _glsl_ast_gen_glsl(n->children[0], out, depth);
  779. string_cat(out," while ( ");
  780. _glsl_ast_gen_glsl(n->children[1], out, depth);
  781. string_cat(out," );");
  782. break;
  783. case FOR_STATEMENT:
  784. string_cat(out,"for (");
  785. _glsl_ast_gen_glsl(n->children[0], out, depth);
  786. string_cat(out," ");
  787. _glsl_ast_gen_glsl(n->children[1], out, depth);
  788. string_cat(out,") ");
  789. _glsl_ast_gen_glsl(n->children[2], out, depth);
  790. break;
  791. case ASSIGNMENT_CONDITION:
  792. _glsl_ast_gen_glsl(n->children[0], out, depth);
  793. string_cat(out," ");
  794. _glsl_ast_gen_glsl(n->children[1], out, depth);
  795. string_cat(out," = ");
  796. _glsl_ast_gen_glsl(n->children[2], out, depth);
  797. break;
  798. case STATEMENT_LIST:
  799. string_cat(out,"{\n");
  800. for (i = 0; i < n->child_count; i++) {
  801. for (j = 0; j < depth + 1; j++) string_cat(out,"\t");
  802. _glsl_ast_gen_glsl(n->children[i], out, depth + 1);
  803. string_cat(out,"\n");
  804. }
  805. for (j = 0; j < depth; j++) string_cat(out,"\t");
  806. string_cat(out,"}");
  807. break;
  808. case STRUCT_DECLARATION_LIST:
  809. for (i = 0; i < n->child_count; i++) {
  810. for (j = 0; j < depth + 1; j++) string_cat(out,"\t");
  811. _glsl_ast_gen_glsl(n->children[i], out, depth + 1);
  812. string_cat(out,"\n");
  813. }
  814. for (j = 0; j < depth; j++) string_cat(out,"\t");
  815. break;
  816. case BLOCK_DECLARATION:
  817. _glsl_ast_gen_glsl(n->children[0], out, depth);
  818. string_cat(out," ");
  819. _glsl_ast_gen_glsl(n->children[1], out, depth);
  820. string_cat(out," {\n");
  821. _glsl_ast_gen_glsl(n->children[2], out, depth);
  822. string_cat(out,"} ");
  823. _glsl_ast_gen_glsl(n->children[3], out, depth);
  824. if (n->children[4]->child_count) {
  825. string_cat(out," ");
  826. _glsl_ast_gen_glsl(n->children[4], out, depth);
  827. }
  828. break;
  829. case DECLARATION:
  830. _glsl_ast_gen_glsl(n->children[0], out, depth);
  831. string_cat(out,";");
  832. break;
  833. case BREAK:
  834. string_cat(out,"break;");
  835. break;
  836. case STRUCT_SPECIFIER:
  837. string_cat(out, "struct ");
  838. _glsl_ast_gen_glsl(n->children[0], out, depth);
  839. string_cat(out, " {\n");
  840. _glsl_ast_gen_glsl(n->children[1], out, depth);
  841. string_cat(out, "}");
  842. break;
  843. case STRUCT_DECLARATOR:
  844. print_list_as_glsl(n, "", " ", "", out, depth);
  845. break;
  846. case STRUCT_DECLARATOR_LIST:
  847. print_list_as_glsl(n, "", ",", "", out, depth);
  848. break;
  849. case STRUCT_DECLARATION:
  850. print_list_as_glsl(n, "", " ", ";", out, depth);
  851. break;
  852. case FUNCTION_PARAMETER_LIST:
  853. case FUNCTION_CALL_PARAMETER_LIST:
  854. print_list_as_glsl(n, "(", ", ", ")", out, depth);
  855. break;
  856. case FOR_REST_STATEMENT:
  857. _glsl_ast_gen_glsl(n->children[0], out, depth);
  858. string_cat(out,"; ");
  859. if (n->child_count == 2) {
  860. _glsl_ast_gen_glsl(n->children[1], out, depth);
  861. }
  862. break;
  863. case INIT_DECLARATOR:
  864. _glsl_ast_gen_glsl(n->children[0], out, depth);
  865. if (n->children[1]->child_count) {
  866. _glsl_ast_gen_glsl(n->children[1], out, depth);
  867. }
  868. if (n->child_count > 2) {
  869. string_cat(out," = ");
  870. _glsl_ast_gen_glsl(n->children[2], out, depth);
  871. }
  872. break;
  873. case INIT_DECLARATOR_LIST:
  874. print_list_as_glsl(n, "", ", ", "", out, depth);
  875. break;
  876. case INITIALIZER_LIST:
  877. print_list_as_glsl(n, "{", ", ", "}", out, depth);
  878. break;
  879. case TERNARY_EXPRESSION:
  880. _glsl_ast_gen_glsl(n->children[0], out, depth);
  881. string_cat(out," ? ");
  882. _glsl_ast_gen_glsl(n->children[1], out, depth);
  883. string_cat(out," : ");
  884. _glsl_ast_gen_glsl(n->children[2], out, depth);
  885. break;
  886. case TYPE_SPECIFIER:
  887. case POSTFIX_EXPRESSION:
  888. case INITIALIZER:
  889. case CONDITION_OPT:
  890. case EXPRESSION_CONDITION:
  891. print_list_as_glsl(n, "", "", "", out, depth);
  892. break;
  893. case EXPRESSION_STATEMENT:
  894. print_list_as_glsl(n, "", "", ";", out, depth);
  895. break;
  896. case DECLARATION_STATEMENT_LIST:
  897. print_list_as_glsl(n, "", "\n", "", out, depth);
  898. break;
  899. case SECTION_STATEMENT:
  900. _glsl_ast_gen_glsl(n->children[1], out, depth);
  901. break;
  902. default:
  903. if (token_to_str[n->code])
  904. string_cat(out,"%s", token_to_str[n->code]);
  905. else
  906. string_cat(out,"<unknown token %d>", n->code);
  907. break;
  908. }
  909. }
  910. char *glsl_ast_generate_glsl(struct glsl_node *n)
  911. {
  912. struct string s;
  913. s.s = malloc(1024);
  914. s.len = 0;
  915. s.capacity = 1024;
  916. _glsl_ast_gen_glsl(n, &s, 0);
  917. return s.s;
  918. }