parse.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050
  1. /*
  2. * Stupid C parser, version 1e-6.
  3. *
  4. * Let's see how hard this is to do.
  5. *
  6. * Copyright (C) 2003 Transmeta Corp.
  7. * 2003-2004 Linus Torvalds
  8. * Copyright (C) 2004 Christopher Li
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. #include <limits.h>
  36. #include "lib.h"
  37. #include "allocate.h"
  38. #include "token.h"
  39. #include "parse.h"
  40. #include "symbol.h"
  41. #include "scope.h"
  42. #include "expression.h"
  43. #include "target.h"
  44. static struct symbol_list **function_symbol_list;
  45. struct symbol_list *function_computed_target_list;
  46. struct statement_list *function_computed_goto_list;
  47. static struct token *statement(struct token *token, struct statement **tree);
  48. static struct token *handle_attributes(struct token *token, struct decl_state *ctx);
  49. typedef struct token *declarator_t(struct token *, struct symbol *, struct decl_state *);
  50. static declarator_t
  51. struct_specifier, union_specifier, enum_specifier,
  52. attribute_specifier, typeof_specifier,
  53. storage_specifier, thread_specifier;
  54. static declarator_t generic_qualifier;
  55. static declarator_t autotype_specifier;
  56. static struct token *parse_if_statement(struct token *token, struct statement *stmt);
  57. static struct token *parse_return_statement(struct token *token, struct statement *stmt);
  58. static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
  59. static struct token *parse_default_statement(struct token *token, struct statement *stmt);
  60. static struct token *parse_case_statement(struct token *token, struct statement *stmt);
  61. static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
  62. static struct token *parse_for_statement(struct token *token, struct statement *stmt);
  63. static struct token *parse_while_statement(struct token *token, struct statement *stmt);
  64. static struct token *parse_do_statement(struct token *token, struct statement *stmt);
  65. static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
  66. static struct token *parse_context_statement(struct token *token, struct statement *stmt);
  67. static struct token *parse_range_statement(struct token *token, struct statement *stmt);
  68. static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
  69. static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
  70. static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
  71. typedef struct token *attr_t(struct token *, struct symbol *,
  72. struct decl_state *);
  73. static attr_t
  74. attribute_packed, attribute_aligned, attribute_modifier,
  75. attribute_function,
  76. attribute_bitwise,
  77. attribute_address_space, attribute_context,
  78. attribute_designated_init,
  79. attribute_transparent_union, ignore_attribute,
  80. attribute_mode, attribute_force;
  81. typedef struct symbol *to_mode_t(struct symbol *);
  82. static to_mode_t
  83. to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode;
  84. static to_mode_t to_pointer_mode, to_word_mode;
  85. enum {
  86. Set_T = 1,
  87. Set_S = 2,
  88. Set_Char = 4,
  89. Set_Int = 8,
  90. Set_Double = 16,
  91. Set_Float = 32,
  92. Set_Signed = 64,
  93. Set_Unsigned = 128,
  94. Set_Short = 256,
  95. Set_Long = 512,
  96. Set_Vlong = 1024,
  97. Set_Int128 = 2048,
  98. Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
  99. };
  100. enum {
  101. CInt = 0, CSInt, CUInt, CReal,
  102. };
  103. static void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod)
  104. {
  105. if (*mods & mod)
  106. warning(token->pos, "duplicated asm modifier");
  107. *mods |= mod;
  108. }
  109. static struct symbol_op typedef_op = {
  110. .type = KW_MODIFIER,
  111. .declarator = storage_specifier,
  112. };
  113. static struct symbol_op inline_op = {
  114. .type = KW_MODIFIER,
  115. .declarator = generic_qualifier,
  116. .asm_modifier = asm_modifier,
  117. };
  118. static struct symbol_op noreturn_op = {
  119. .type = KW_MODIFIER,
  120. .declarator = generic_qualifier,
  121. };
  122. static declarator_t alignas_specifier;
  123. static struct symbol_op alignas_op = {
  124. .type = KW_MODIFIER,
  125. .declarator = alignas_specifier,
  126. };
  127. static struct symbol_op auto_op = {
  128. .type = KW_MODIFIER,
  129. .declarator = storage_specifier,
  130. };
  131. static struct symbol_op register_op = {
  132. .type = KW_MODIFIER,
  133. .declarator = storage_specifier,
  134. };
  135. static struct symbol_op static_op = {
  136. .type = KW_MODIFIER|KW_STATIC,
  137. .declarator = storage_specifier,
  138. };
  139. static struct symbol_op extern_op = {
  140. .type = KW_MODIFIER,
  141. .declarator = storage_specifier,
  142. };
  143. static struct symbol_op thread_op = {
  144. .type = KW_MODIFIER,
  145. .declarator = thread_specifier,
  146. };
  147. static struct symbol_op const_op = {
  148. .type = KW_QUALIFIER,
  149. .declarator = generic_qualifier,
  150. };
  151. static struct symbol_op volatile_op = {
  152. .type = KW_QUALIFIER,
  153. .declarator = generic_qualifier,
  154. .asm_modifier = asm_modifier,
  155. };
  156. static struct symbol_op restrict_op = {
  157. .type = KW_QUALIFIER,
  158. .declarator = generic_qualifier,
  159. };
  160. static struct symbol_op atomic_op = {
  161. .type = KW_QUALIFIER,
  162. .declarator = generic_qualifier,
  163. };
  164. static struct symbol_op typeof_op = {
  165. .type = KW_SPECIFIER,
  166. .declarator = typeof_specifier,
  167. .test = Set_Any,
  168. .set = Set_S|Set_T,
  169. };
  170. static struct symbol_op autotype_op = {
  171. .type = KW_SPECIFIER,
  172. .declarator = autotype_specifier,
  173. .test = Set_Any,
  174. .set = Set_S|Set_T,
  175. };
  176. static struct symbol_op attribute_op = {
  177. .type = KW_ATTRIBUTE,
  178. .declarator = attribute_specifier,
  179. };
  180. static struct symbol_op struct_op = {
  181. .type = KW_SPECIFIER,
  182. .declarator = struct_specifier,
  183. .test = Set_Any,
  184. .set = Set_S|Set_T,
  185. };
  186. static struct symbol_op union_op = {
  187. .type = KW_SPECIFIER,
  188. .declarator = union_specifier,
  189. .test = Set_Any,
  190. .set = Set_S|Set_T,
  191. };
  192. static struct symbol_op enum_op = {
  193. .type = KW_SPECIFIER,
  194. .declarator = enum_specifier,
  195. .test = Set_Any,
  196. .set = Set_S|Set_T,
  197. };
  198. static struct symbol_op spec_op = {
  199. .type = KW_SPECIFIER | KW_EXACT,
  200. .test = Set_Any,
  201. .set = Set_S|Set_T,
  202. };
  203. static struct symbol_op char_op = {
  204. .type = KW_SPECIFIER,
  205. .test = Set_T|Set_Long|Set_Short,
  206. .set = Set_T|Set_Char,
  207. .class = CInt,
  208. };
  209. static struct symbol_op int_op = {
  210. .type = KW_SPECIFIER,
  211. .test = Set_T,
  212. .set = Set_T|Set_Int,
  213. };
  214. static struct symbol_op double_op = {
  215. .type = KW_SPECIFIER,
  216. .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
  217. .set = Set_T|Set_Double,
  218. .class = CReal,
  219. };
  220. static struct symbol_op float_op = {
  221. .type = KW_SPECIFIER,
  222. .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
  223. .set = Set_T|Set_Float,
  224. .class = CReal,
  225. };
  226. static struct symbol_op short_op = {
  227. .type = KW_SPECIFIER,
  228. .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
  229. .set = Set_Short,
  230. };
  231. static struct symbol_op signed_op = {
  232. .type = KW_SPECIFIER,
  233. .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
  234. .set = Set_Signed,
  235. .class = CSInt,
  236. };
  237. static struct symbol_op unsigned_op = {
  238. .type = KW_SPECIFIER,
  239. .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
  240. .set = Set_Unsigned,
  241. .class = CUInt,
  242. };
  243. static struct symbol_op long_op = {
  244. .type = KW_SPECIFIER,
  245. .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
  246. .set = Set_Long,
  247. };
  248. static struct symbol_op int128_op = {
  249. .type = KW_SPECIFIER,
  250. .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
  251. .set = Set_T|Set_Int128|Set_Vlong,
  252. .class = CInt,
  253. };
  254. static struct symbol_op if_op = {
  255. .statement = parse_if_statement,
  256. };
  257. static struct symbol_op return_op = {
  258. .statement = parse_return_statement,
  259. };
  260. static struct symbol_op loop_iter_op = {
  261. .statement = parse_loop_iterator,
  262. };
  263. static struct symbol_op default_op = {
  264. .statement = parse_default_statement,
  265. };
  266. static struct symbol_op case_op = {
  267. .statement = parse_case_statement,
  268. };
  269. static struct symbol_op switch_op = {
  270. .statement = parse_switch_statement,
  271. };
  272. static struct symbol_op for_op = {
  273. .statement = parse_for_statement,
  274. };
  275. static struct symbol_op while_op = {
  276. .statement = parse_while_statement,
  277. };
  278. static struct symbol_op do_op = {
  279. .statement = parse_do_statement,
  280. };
  281. static struct symbol_op goto_op = {
  282. .statement = parse_goto_statement,
  283. };
  284. static struct symbol_op __context___op = {
  285. .statement = parse_context_statement,
  286. .attribute = attribute_context,
  287. };
  288. static struct symbol_op range_op = {
  289. .statement = parse_range_statement,
  290. };
  291. static struct symbol_op asm_op = {
  292. .type = KW_ASM,
  293. .statement = parse_asm_statement,
  294. .toplevel = toplevel_asm_declaration,
  295. };
  296. static struct symbol_op static_assert_op = {
  297. .toplevel = parse_static_assert,
  298. };
  299. static struct symbol_op packed_op = {
  300. .attribute = attribute_packed,
  301. };
  302. static struct symbol_op aligned_op = {
  303. .attribute = attribute_aligned,
  304. };
  305. static struct symbol_op attr_mod_op = {
  306. .attribute = attribute_modifier,
  307. };
  308. static struct symbol_op attr_fun_op = {
  309. .attribute = attribute_function,
  310. };
  311. static struct symbol_op attr_bitwise_op = {
  312. .attribute = attribute_bitwise,
  313. };
  314. static struct symbol_op attr_force_op = {
  315. .attribute = attribute_force,
  316. };
  317. static struct symbol_op address_space_op = {
  318. .attribute = attribute_address_space,
  319. };
  320. static struct symbol_op mode_op = {
  321. .attribute = attribute_mode,
  322. };
  323. static struct symbol_op context_op = {
  324. .attribute = attribute_context,
  325. };
  326. static struct symbol_op designated_init_op = {
  327. .attribute = attribute_designated_init,
  328. };
  329. static struct symbol_op transparent_union_op = {
  330. .attribute = attribute_transparent_union,
  331. };
  332. static struct symbol_op ignore_attr_op = {
  333. .attribute = ignore_attribute,
  334. };
  335. static struct symbol_op mode_QI_op = {
  336. .type = KW_MODE,
  337. .to_mode = to_QI_mode
  338. };
  339. static struct symbol_op mode_HI_op = {
  340. .type = KW_MODE,
  341. .to_mode = to_HI_mode
  342. };
  343. static struct symbol_op mode_SI_op = {
  344. .type = KW_MODE,
  345. .to_mode = to_SI_mode
  346. };
  347. static struct symbol_op mode_DI_op = {
  348. .type = KW_MODE,
  349. .to_mode = to_DI_mode
  350. };
  351. static struct symbol_op mode_TI_op = {
  352. .type = KW_MODE,
  353. .to_mode = to_TI_mode
  354. };
  355. static struct symbol_op mode_pointer_op = {
  356. .type = KW_MODE,
  357. .to_mode = to_pointer_mode
  358. };
  359. static struct symbol_op mode_word_op = {
  360. .type = KW_MODE,
  361. .to_mode = to_word_mode
  362. };
  363. /*
  364. * Define the keyword and their effects.
  365. * The entries in the 'typedef' and put in NS_TYPEDEF and
  366. * are automatically set as reserved keyword while the ones
  367. * in the 'keyword' table are just put in NS_KEYWORD.
  368. *
  369. * The entries are added via the 3 macros:
  370. * N() for entries with "name" only,
  371. * D() for entries with "name" & "__name__",
  372. * A() for entries with "name", "__name" & "__name__",
  373. * U() for entries with "__name" & "__name__".
  374. */
  375. static struct init_keyword {
  376. const char *name;
  377. struct symbol_op *op;
  378. struct symbol *type;
  379. unsigned long mods;
  380. } typedefs[] = {
  381. #define N(I, O,...) { I, O,##__VA_ARGS__ }
  382. #define D(I, O,...) N(I,O,##__VA_ARGS__ ), \
  383. N("__" I "__",O,##__VA_ARGS__)
  384. #define A(I, O,...) N(I,O,##__VA_ARGS__ ), \
  385. N("__" I,O,##__VA_ARGS__), \
  386. N("__" I "__",O,##__VA_ARGS__)
  387. #define U(I, O,...) N("__" I,O,##__VA_ARGS__), \
  388. N("__" I "__",O,##__VA_ARGS__)
  389. /* Storage classes */
  390. N("auto", &auto_op, .mods = MOD_AUTO),
  391. N("register", &register_op, .mods = MOD_REGISTER),
  392. N("static", &static_op, .mods = MOD_STATIC),
  393. N("extern", &extern_op, .mods = MOD_EXTERN),
  394. N("__thread", &thread_op),
  395. N("_Thread_local", &thread_op),
  396. A("inline", &inline_op, .mods = MOD_INLINE),
  397. /* Typedef ... */
  398. N("typedef", &typedef_op, .mods = MOD_USERTYPE),
  399. A("typeof", &typeof_op),
  400. N("__auto_type", &autotype_op),
  401. /* Type qualifiers */
  402. A("const", &const_op, .mods = MOD_CONST),
  403. A("volatile", &volatile_op, .mods = MOD_VOLATILE),
  404. A("restrict", &restrict_op, .mods = MOD_RESTRICT),
  405. N("_Atomic", &atomic_op, .mods = MOD_ATOMIC),
  406. N("_Noreturn", &noreturn_op, .mods = MOD_NORETURN),
  407. N("_Alignas", &alignas_op),
  408. U("attribute", &attribute_op),
  409. /* Type specifiers */
  410. N("struct", &struct_op),
  411. N("union", &union_op),
  412. N("enum", &enum_op),
  413. N("void", &spec_op, .type = &void_ctype),
  414. N("char", &char_op),
  415. N("short", &short_op),
  416. N("int", &int_op),
  417. N("long", &long_op),
  418. N("float", &float_op),
  419. N("double", &double_op),
  420. A("signed", &signed_op),
  421. N("unsigned", &unsigned_op),
  422. N("__int128", &int128_op),
  423. N("_Bool", &spec_op, .type = &bool_ctype),
  424. /* Predeclared types */
  425. N("__builtin_va_list", &spec_op, .type = &ptr_ctype),
  426. N("__builtin_ms_va_list",&spec_op, .type = &ptr_ctype),
  427. N("__int128_t", &spec_op, .type = &sint128_ctype),
  428. N("__uint128_t", &spec_op, .type = &uint128_ctype),
  429. N("_Float32", &spec_op, .type = &float32_ctype),
  430. N("_Float32x", &spec_op, .type = &float32x_ctype),
  431. N("_Float64", &spec_op, .type = &float64_ctype),
  432. N("_Float64x", &spec_op, .type = &float64x_ctype),
  433. N("_Float128", &spec_op, .type = &float128_ctype),
  434. }, keywords[] = {
  435. /* Statements */
  436. N("if", &if_op),
  437. N("return", &return_op),
  438. N("break", &loop_iter_op),
  439. N("continue", &loop_iter_op),
  440. N("default", &default_op),
  441. N("case", &case_op),
  442. N("switch", &switch_op),
  443. N("for", &for_op),
  444. N("while", &while_op),
  445. N("do", &do_op),
  446. N("goto", &goto_op),
  447. A("asm", &asm_op),
  448. N("context", &context_op),
  449. N("__context__", &__context___op),
  450. N("__range__", &range_op),
  451. N("_Static_assert", &static_assert_op),
  452. /* Attributes */
  453. D("packed", &packed_op),
  454. D("aligned", &aligned_op),
  455. D("nocast", &attr_mod_op, .mods = MOD_NOCAST),
  456. D("noderef", &attr_mod_op, .mods = MOD_NODEREF),
  457. D("safe", &attr_mod_op, .mods = MOD_SAFE),
  458. D("unused", &attr_mod_op, .mods = MOD_UNUSED),
  459. D("externally_visible", &attr_mod_op, .mods = MOD_EXT_VISIBLE),
  460. D("force", &attr_force_op),
  461. D("bitwise", &attr_bitwise_op, .mods = MOD_BITWISE),
  462. D("address_space", &address_space_op),
  463. D("designated_init", &designated_init_op),
  464. D("transparent_union", &transparent_union_op),
  465. D("noreturn", &attr_fun_op, .mods = MOD_NORETURN),
  466. D("pure", &attr_fun_op, .mods = MOD_PURE),
  467. A("const", &attr_fun_op, .mods = MOD_PURE),
  468. D("gnu_inline", &attr_fun_op, .mods = MOD_GNU_INLINE),
  469. /* Modes */
  470. D("mode", &mode_op),
  471. D("QI", &mode_QI_op),
  472. D("HI", &mode_HI_op),
  473. D("SI", &mode_SI_op),
  474. D("DI", &mode_DI_op),
  475. D("TI", &mode_TI_op),
  476. D("byte", &mode_QI_op),
  477. D("pointer", &mode_pointer_op),
  478. D("word", &mode_word_op),
  479. };
  480. static const char *ignored_attributes[] = {
  481. #define GCC_ATTR(x) \
  482. STRINGIFY(x), \
  483. STRINGIFY(__##x##__),
  484. #include "gcc-attr-list.h"
  485. #undef GCC_ATTR
  486. "bounded",
  487. "__bounded__",
  488. "__noclone",
  489. "__nonnull",
  490. "__nothrow",
  491. };
  492. static void init_keyword(int stream, struct init_keyword *kw, enum namespace ns)
  493. {
  494. struct symbol *sym = create_symbol(stream, kw->name, SYM_KEYWORD, ns);
  495. sym->ident->keyword = 1;
  496. sym->ident->reserved |= (ns == NS_TYPEDEF);
  497. sym->ctype.modifiers = kw->mods;
  498. sym->ctype.base_type = kw->type;
  499. sym->op = kw->op;
  500. }
  501. void init_parser(int stream)
  502. {
  503. int i;
  504. for (i = 0; i < ARRAY_SIZE(typedefs); i++)
  505. init_keyword(stream, &typedefs[i], NS_TYPEDEF);
  506. for (i = 0; i < ARRAY_SIZE(keywords); i++)
  507. init_keyword(stream, &keywords[i], NS_KEYWORD);
  508. for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
  509. const char * name = ignored_attributes[i];
  510. struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
  511. NS_KEYWORD);
  512. if (!sym->op) {
  513. sym->ident->keyword = 1;
  514. sym->op = &ignore_attr_op;
  515. }
  516. }
  517. }
  518. static struct token *skip_to(struct token *token, int op)
  519. {
  520. while (!match_op(token, op) && !eof_token(token))
  521. token = token->next;
  522. return token;
  523. }
  524. static struct token bad_token = { .pos.type = TOKEN_BAD };
  525. struct token *expect(struct token *token, int op, const char *where)
  526. {
  527. if (!match_op(token, op)) {
  528. if (token != &bad_token) {
  529. bad_token.next = token;
  530. sparse_error(token->pos, "Expected %s %s", show_special(op), where);
  531. sparse_error(token->pos, "got %s", show_token(token));
  532. }
  533. if (op == ';')
  534. return skip_to(token, op);
  535. return &bad_token;
  536. }
  537. return token->next;
  538. }
  539. ///
  540. // issue an error message on new parsing errors
  541. // @token: the current token
  542. // @errmsg: the error message
  543. // If the current token is from a previous error, an error message
  544. // has already been issued, so nothing more is done.
  545. // Otherwise, @errmsg is displayed followed by the current token.
  546. static void unexpected(struct token *token, const char *errmsg)
  547. {
  548. if (token == &bad_token)
  549. return;
  550. sparse_error(token->pos, "%s", errmsg);
  551. sparse_error(token->pos, "got %s", show_token(token));
  552. }
  553. // Add a symbol to the list of function-local symbols
  554. static void fn_local_symbol(struct symbol *sym)
  555. {
  556. if (function_symbol_list)
  557. add_symbol(function_symbol_list, sym);
  558. }
  559. struct statement *alloc_statement(struct position pos, int type)
  560. {
  561. struct statement *stmt = __alloc_statement(0);
  562. stmt->type = type;
  563. stmt->pos = pos;
  564. return stmt;
  565. }
  566. static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
  567. static void apply_modifiers(struct position pos, struct decl_state *ctx)
  568. {
  569. struct symbol *ctype;
  570. if (!ctx->mode)
  571. return;
  572. ctype = ctx->mode->to_mode(ctx->ctype.base_type);
  573. if (!ctype)
  574. sparse_error(pos, "don't know how to apply mode to %s",
  575. show_typename(ctx->ctype.base_type));
  576. else
  577. ctx->ctype.base_type = ctype;
  578. }
  579. static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
  580. {
  581. struct symbol *sym = alloc_symbol(pos, type);
  582. sym->ctype.base_type = ctype->base_type;
  583. sym->ctype.modifiers = ctype->modifiers;
  584. ctype->base_type = sym;
  585. ctype->modifiers = 0;
  586. return sym;
  587. }
  588. /*
  589. * NOTE! NS_LABEL is not just a different namespace,
  590. * it also ends up using function scope instead of the
  591. * regular symbol scope.
  592. */
  593. struct symbol *label_symbol(struct token *token, int used)
  594. {
  595. struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
  596. if (!sym) {
  597. sym = alloc_symbol(token->pos, SYM_LABEL);
  598. bind_symbol(sym, token->ident, NS_LABEL);
  599. if (used)
  600. sym->used = 1;
  601. fn_local_symbol(sym);
  602. }
  603. return sym;
  604. }
  605. static struct token *struct_union_enum_specifier(enum type type,
  606. struct token *token, struct decl_state *ctx,
  607. struct token *(*parse)(struct token *, struct symbol *))
  608. {
  609. struct symbol *sym;
  610. struct position *repos;
  611. token = handle_attributes(token, ctx);
  612. if (token_type(token) == TOKEN_IDENT) {
  613. sym = lookup_symbol(token->ident, NS_STRUCT);
  614. if (!sym ||
  615. (is_outer_scope(sym->scope) &&
  616. (match_op(token->next,';') || match_op(token->next,'{')))) {
  617. // Either a new symbol, or else an out-of-scope
  618. // symbol being redefined.
  619. sym = alloc_symbol(token->pos, type);
  620. bind_symbol(sym, token->ident, NS_STRUCT);
  621. }
  622. if (sym->type != type)
  623. error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
  624. ctx->ctype.base_type = sym;
  625. repos = &token->pos;
  626. token = token->next;
  627. if (match_op(token, '{')) {
  628. // The following test is actually wrong for empty
  629. // structs, but (1) they are not C99, (2) gcc does
  630. // the same thing, and (3) it's easier.
  631. if (sym->symbol_list)
  632. error_die(token->pos, "redefinition of %s", show_typename (sym));
  633. sym->pos = *repos;
  634. token = parse(token->next, sym);
  635. token = expect(token, '}', "at end of struct-union-enum-specifier");
  636. // Mark the structure as needing re-examination
  637. sym->examined = 0;
  638. sym->endpos = token->pos;
  639. }
  640. return token;
  641. }
  642. // private struct/union/enum type
  643. if (!match_op(token, '{')) {
  644. sparse_error(token->pos, "expected declaration");
  645. ctx->ctype.base_type = &bad_ctype;
  646. return token;
  647. }
  648. sym = alloc_symbol(token->pos, type);
  649. set_current_scope(sym); // used by dissect
  650. token = parse(token->next, sym);
  651. ctx->ctype.base_type = sym;
  652. token = expect(token, '}', "at end of specifier");
  653. sym->endpos = token->pos;
  654. return token;
  655. }
  656. static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
  657. {
  658. struct symbol *field, *last = NULL;
  659. struct token *res;
  660. res = struct_declaration_list(token, &sym->symbol_list);
  661. FOR_EACH_PTR(sym->symbol_list, field) {
  662. if (!field->ident) {
  663. struct symbol *base = field->ctype.base_type;
  664. if (base && base->type == SYM_BITFIELD)
  665. continue;
  666. }
  667. if (last)
  668. last->next_subobject = field;
  669. last = field;
  670. } END_FOR_EACH_PTR(field);
  671. return res;
  672. }
  673. static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
  674. {
  675. return struct_declaration_list(token, &sym->symbol_list);
  676. }
  677. static struct token *struct_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
  678. {
  679. return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
  680. }
  681. static struct token *union_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
  682. {
  683. return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
  684. }
  685. ///
  686. // safe right shift
  687. //
  688. // This allow to use a shift amount as big (or bigger)
  689. // than the width of the value to be shifted, in which case
  690. // the result is, of course, 0.
  691. static unsigned long long rshift(unsigned long long val, unsigned int n)
  692. {
  693. if (n >= (sizeof(val) * 8))
  694. return 0;
  695. return val >> n;
  696. }
  697. struct range {
  698. long long neg;
  699. unsigned long long pos;
  700. };
  701. static void update_range(struct range *range, unsigned long long uval, struct symbol *vtype)
  702. {
  703. long long sval = uval;
  704. if (is_signed_type(vtype) && (sval < 0)) {
  705. if (sval < range->neg)
  706. range->neg = sval;
  707. } else {
  708. if (uval > range->pos)
  709. range->pos = uval;
  710. }
  711. }
  712. static int type_is_ok(struct symbol *type, struct range range)
  713. {
  714. int shift = type->bit_size;
  715. int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
  716. if (!is_unsigned)
  717. shift--;
  718. if (rshift(range.pos, shift))
  719. return 0;
  720. if (range.neg == 0)
  721. return 1;
  722. if (is_unsigned)
  723. return 0;
  724. if (rshift(~range.neg, shift))
  725. return 0;
  726. return 1;
  727. }
  728. static struct range type_range(struct symbol *type)
  729. {
  730. struct range range;
  731. unsigned int size = type->bit_size;
  732. unsigned long long max;
  733. long long min;
  734. if (is_signed_type(type)) {
  735. min = sign_bit(size);
  736. max = min - 1;
  737. } else {
  738. min = 0;
  739. max = bits_mask(size);
  740. }
  741. range.pos = max;
  742. range.neg = min;
  743. return range;
  744. }
  745. static int val_in_range(struct range *range, long long sval, struct symbol *vtype)
  746. {
  747. unsigned long long uval = sval;
  748. if (is_signed_type(vtype) && (sval < 0))
  749. return range->neg <= sval;
  750. else
  751. return uval <= range->pos;
  752. }
  753. static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
  754. {
  755. struct range irange = type_range(&int_ctype);
  756. struct symbol *sym;
  757. FOR_EACH_PTR(list, sym) {
  758. struct expression *expr = sym->initializer;
  759. struct symbol *ctype;
  760. long long val;
  761. if (expr->type != EXPR_VALUE)
  762. continue;
  763. ctype = expr->ctype;
  764. val = get_expression_value(expr);
  765. if (is_int_type(ctype) && val_in_range(&irange, val, ctype)) {
  766. expr->ctype = &int_ctype;
  767. continue;
  768. }
  769. cast_value(expr, base_type, expr, ctype);
  770. expr->ctype = base_type;
  771. } END_FOR_EACH_PTR(sym);
  772. }
  773. static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
  774. {
  775. unsigned long long lastval = 0;
  776. struct symbol *ctype = NULL, *base_type = NULL;
  777. struct range range = { };
  778. int mix_bitwise = 0;
  779. parent->examined = 1;
  780. parent->ctype.base_type = &int_ctype;
  781. while (token_type(token) == TOKEN_IDENT) {
  782. struct expression *expr = NULL;
  783. struct token *next = token->next;
  784. struct decl_state ctx = { };
  785. struct symbol *sym;
  786. // FIXME: only 'deprecated' should be accepted
  787. next = handle_attributes(next, &ctx);
  788. if (match_op(next, '=')) {
  789. next = constant_expression(next->next, &expr);
  790. lastval = get_expression_value(expr);
  791. ctype = &void_ctype;
  792. if (expr && expr->ctype)
  793. ctype = expr->ctype;
  794. } else if (!ctype) {
  795. ctype = &int_ctype;
  796. } else if (is_int_type(ctype)) {
  797. lastval++;
  798. } else {
  799. error_die(token->pos, "can't increment the last enum member");
  800. }
  801. if (!expr) {
  802. expr = alloc_expression(token->pos, EXPR_VALUE);
  803. expr->value = lastval;
  804. expr->ctype = ctype;
  805. }
  806. sym = alloc_symbol(token->pos, SYM_NODE);
  807. bind_symbol(sym, token->ident, NS_SYMBOL);
  808. sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
  809. sym->initializer = expr;
  810. sym->enum_member = 1;
  811. sym->ctype.base_type = parent;
  812. add_ptr_list(&parent->symbol_list, sym);
  813. if (base_type != &bad_ctype) {
  814. if (ctype->type == SYM_NODE)
  815. ctype = ctype->ctype.base_type;
  816. if (ctype->type == SYM_ENUM) {
  817. if (ctype == parent)
  818. ctype = base_type;
  819. else
  820. ctype = ctype->ctype.base_type;
  821. }
  822. /*
  823. * base_type rules:
  824. * - if all enums are of the same type, then
  825. * the base_type is that type (two first
  826. * cases)
  827. * - if enums are of different types, they
  828. * all have to be integer types, and the
  829. * base type is at least "int_ctype".
  830. * - otherwise the base_type is "bad_ctype".
  831. */
  832. if (!base_type || ctype == &bad_ctype) {
  833. base_type = ctype;
  834. } else if (ctype == base_type) {
  835. /* nothing */
  836. } else if (is_int_type(base_type) && is_int_type(ctype)) {
  837. base_type = &int_ctype;
  838. } else if (is_restricted_type(base_type) != is_restricted_type(ctype)) {
  839. if (!mix_bitwise++) {
  840. warning(expr->pos, "mixed bitwiseness");
  841. }
  842. } else if (is_restricted_type(base_type) && base_type != ctype) {
  843. sparse_error(expr->pos, "incompatible restricted type");
  844. info(expr->pos, " expected: %s", show_typename(base_type));
  845. info(expr->pos, " got: %s", show_typename(ctype));
  846. base_type = &bad_ctype;
  847. } else if (base_type != &bad_ctype) {
  848. sparse_error(token->pos, "bad enum definition");
  849. base_type = &bad_ctype;
  850. }
  851. parent->ctype.base_type = base_type;
  852. }
  853. if (is_int_type(base_type)) {
  854. update_range(&range, lastval, ctype);
  855. }
  856. token = next;
  857. sym->endpos = token->pos;
  858. if (!match_op(token, ','))
  859. break;
  860. token = token->next;
  861. }
  862. if (!base_type) {
  863. sparse_error(token->pos, "empty enum definition");
  864. base_type = &bad_ctype;
  865. }
  866. else if (!is_int_type(base_type))
  867. ;
  868. else if (type_is_ok(&uint_ctype, range))
  869. base_type = &uint_ctype;
  870. else if (type_is_ok(&int_ctype, range))
  871. base_type = &int_ctype;
  872. else if (type_is_ok(&ulong_ctype, range))
  873. base_type = &ulong_ctype;
  874. else if (type_is_ok(&long_ctype, range))
  875. base_type = &long_ctype;
  876. else if (type_is_ok(&ullong_ctype, range))
  877. base_type = &ullong_ctype;
  878. else if (type_is_ok(&llong_ctype, range))
  879. base_type = &llong_ctype;
  880. else
  881. base_type = &bad_ctype;
  882. parent->ctype.base_type = base_type;
  883. parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
  884. parent->examined = 0;
  885. if (mix_bitwise)
  886. return token;
  887. cast_enum_list(parent->symbol_list, base_type);
  888. return token;
  889. }
  890. static struct token *enum_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
  891. {
  892. struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
  893. struct ctype *ctype = &ctx->ctype.base_type->ctype;
  894. if (!ctype->base_type)
  895. ctype->base_type = &incomplete_ctype;
  896. return ret;
  897. }
  898. static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
  899. static struct token *typeof_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
  900. {
  901. if (!match_op(token, '(')) {
  902. sparse_error(token->pos, "expected '(' after typeof");
  903. return token;
  904. }
  905. if (lookup_type(token->next)) {
  906. struct symbol *sym;
  907. token = typename(token->next, &sym, NULL);
  908. ctx->ctype.base_type = sym->ctype.base_type;
  909. apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
  910. } else {
  911. struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
  912. token = parse_expression(token->next, &typeof_sym->initializer);
  913. typeof_sym->endpos = token->pos;
  914. if (!typeof_sym->initializer) {
  915. sparse_error(token->pos, "expected expression after the '(' token");
  916. typeof_sym = &bad_ctype;
  917. }
  918. ctx->ctype.base_type = typeof_sym;
  919. }
  920. return expect(token, ')', "after typeof");
  921. }
  922. static struct token *autotype_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
  923. {
  924. ctx->ctype.base_type = &autotype_ctype;
  925. ctx->autotype = 1;
  926. return token;
  927. }
  928. static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
  929. {
  930. struct expression *expr = NULL;
  931. if (match_op(token, '('))
  932. token = parens_expression(token, &expr, "in attribute");
  933. return token;
  934. }
  935. static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
  936. {
  937. if (!ctx->ctype.alignment)
  938. ctx->ctype.alignment = 1;
  939. return token;
  940. }
  941. static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
  942. {
  943. int alignment = max_alignment;
  944. struct expression *expr = NULL;
  945. if (match_op(token, '(')) {
  946. token = parens_expression(token, &expr, "in attribute");
  947. if (expr)
  948. alignment = const_expression_value(expr);
  949. }
  950. if (alignment & (alignment-1)) {
  951. warning(token->pos, "I don't like non-power-of-2 alignments");
  952. return token;
  953. } else if (alignment > ctx->ctype.alignment)
  954. ctx->ctype.alignment = alignment;
  955. return token;
  956. }
  957. static void apply_mod(struct position *pos, unsigned long *mods, unsigned long mod)
  958. {
  959. if (*mods & mod & ~MOD_DUP_OK)
  960. warning(*pos, "duplicate %s", modifier_name(mod));
  961. *mods |= mod;
  962. }
  963. static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
  964. {
  965. apply_mod(pos, &ctx->modifiers, qual);
  966. }
  967. static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
  968. {
  969. apply_mod(&token->pos, &ctx->ctype.modifiers, attr->ctype.modifiers);
  970. return token;
  971. }
  972. static struct token *attribute_function(struct token *token, struct symbol *attr, struct decl_state *ctx)
  973. {
  974. apply_mod(&token->pos, &ctx->f_modifiers, attr->ctype.modifiers);
  975. return token;
  976. }
  977. static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
  978. {
  979. if (Wbitwise)
  980. attribute_modifier(token, attr, ctx);
  981. return token;
  982. }
  983. static struct ident *numerical_address_space(int asn)
  984. {
  985. char buff[32];
  986. if (!asn)
  987. return NULL;
  988. sprintf(buff, "<asn:%d>", asn);
  989. return built_in_ident(buff);
  990. }
  991. static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
  992. {
  993. struct expression *expr = NULL;
  994. struct ident *as = NULL;
  995. struct token *next;
  996. token = expect(token, '(', "after address_space attribute");
  997. switch (token_type(token)) {
  998. case TOKEN_NUMBER:
  999. next = primary_expression(token, &expr);
  1000. if (expr->type != EXPR_VALUE)
  1001. goto invalid;
  1002. as = numerical_address_space(expr->value);
  1003. break;
  1004. case TOKEN_IDENT:
  1005. next = token->next;
  1006. as = token->ident;
  1007. break;
  1008. default:
  1009. next = token->next;
  1010. invalid:
  1011. as = NULL;
  1012. warning(token->pos, "invalid address space name");
  1013. }
  1014. if (Waddress_space && as) {
  1015. if (ctx->ctype.as)
  1016. sparse_error(token->pos,
  1017. "multiple address spaces given: %s & %s",
  1018. show_as(ctx->ctype.as), show_as(as));
  1019. ctx->ctype.as = as;
  1020. }
  1021. token = expect(next, ')', "after address_space attribute");
  1022. return token;
  1023. }
  1024. static struct symbol *to_QI_mode(struct symbol *ctype)
  1025. {
  1026. if (ctype->ctype.base_type != &int_type)
  1027. return NULL;
  1028. if (ctype == &char_ctype)
  1029. return ctype;
  1030. return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
  1031. : &schar_ctype;
  1032. }
  1033. static struct symbol *to_HI_mode(struct symbol *ctype)
  1034. {
  1035. if (ctype->ctype.base_type != &int_type)
  1036. return NULL;
  1037. return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
  1038. : &sshort_ctype;
  1039. }
  1040. static struct symbol *to_SI_mode(struct symbol *ctype)
  1041. {
  1042. if (ctype->ctype.base_type != &int_type)
  1043. return NULL;
  1044. return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
  1045. : &sint_ctype;
  1046. }
  1047. static struct symbol *to_DI_mode(struct symbol *ctype)
  1048. {
  1049. if (ctype->ctype.base_type != &int_type)
  1050. return NULL;
  1051. return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
  1052. : &sllong_ctype;
  1053. }
  1054. static struct symbol *to_TI_mode(struct symbol *ctype)
  1055. {
  1056. if (ctype->ctype.base_type != &int_type)
  1057. return NULL;
  1058. return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint128_ctype
  1059. : &sint128_ctype;
  1060. }
  1061. static struct symbol *to_pointer_mode(struct symbol *ctype)
  1062. {
  1063. if (ctype->ctype.base_type != &int_type)
  1064. return NULL;
  1065. return ctype->ctype.modifiers & MOD_UNSIGNED ? uintptr_ctype
  1066. : intptr_ctype;
  1067. }
  1068. static struct symbol *to_word_mode(struct symbol *ctype)
  1069. {
  1070. if (ctype->ctype.base_type != &int_type)
  1071. return NULL;
  1072. return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
  1073. : &slong_ctype;
  1074. }
  1075. static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
  1076. {
  1077. token = expect(token, '(', "after mode attribute");
  1078. if (token_type(token) == TOKEN_IDENT) {
  1079. struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
  1080. if (mode && mode->op->type & KW_MODE)
  1081. ctx->mode = mode->op;
  1082. else
  1083. sparse_error(token->pos, "unknown mode attribute %s", show_ident(token->ident));
  1084. token = token->next;
  1085. } else
  1086. sparse_error(token->pos, "expect attribute mode symbol\n");
  1087. token = expect(token, ')', "after mode attribute");
  1088. return token;
  1089. }
  1090. static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
  1091. {
  1092. struct context *context = alloc_context();
  1093. struct expression *args[3];
  1094. int idx = 0;
  1095. token = expect(token, '(', "after context attribute");
  1096. token = conditional_expression(token, &args[0]);
  1097. token = expect(token, ',', "after context 1st argument");
  1098. token = conditional_expression(token, &args[1]);
  1099. if (match_op(token, ',')) {
  1100. token = token->next;
  1101. token = conditional_expression(token, &args[2]);
  1102. token = expect(token, ')', "after context 3rd argument");
  1103. context->context = args[0];
  1104. idx++;
  1105. } else {
  1106. token = expect(token, ')', "after context 2nd argument");
  1107. }
  1108. context->in = get_expression_value(args[idx++]);
  1109. context->out = get_expression_value(args[idx++]);
  1110. add_ptr_list(&ctx->ctype.contexts, context);
  1111. return token;
  1112. }
  1113. static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
  1114. {
  1115. if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
  1116. ctx->ctype.base_type->designated_init = 1;
  1117. else
  1118. warning(token->pos, "attribute designated_init applied to non-structure type");
  1119. return token;
  1120. }
  1121. static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
  1122. {
  1123. if (Wtransparent_union)
  1124. warning(token->pos, "attribute __transparent_union__");
  1125. if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
  1126. ctx->ctype.base_type->transparent_union = 1;
  1127. else
  1128. warning(token->pos, "attribute __transparent_union__ applied to non-union type");
  1129. return token;
  1130. }
  1131. static struct token *recover_unknown_attribute(struct token *token)
  1132. {
  1133. struct expression *expr = NULL;
  1134. if (Wunknown_attribute)
  1135. warning(token->pos, "unknown attribute '%s'", show_ident(token->ident));
  1136. token = token->next;
  1137. if (match_op(token, '('))
  1138. token = parens_expression(token, &expr, "in attribute");
  1139. return token;
  1140. }
  1141. static struct token *attribute_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
  1142. {
  1143. token = expect(token, '(', "after attribute");
  1144. token = expect(token, '(', "after attribute");
  1145. while (token_type(token) == TOKEN_IDENT) {
  1146. struct symbol *attr = lookup_keyword(token->ident, NS_KEYWORD);
  1147. if (attr && attr->op->attribute)
  1148. token = attr->op->attribute(token->next, attr, ctx);
  1149. else
  1150. token = recover_unknown_attribute(token);
  1151. if (!match_op(token, ','))
  1152. break;
  1153. token = token->next;
  1154. }
  1155. token = expect(token, ')', "after attribute");
  1156. token = expect(token, ')', "after attribute");
  1157. return token;
  1158. }
  1159. static unsigned long decl_modifiers(struct decl_state *ctx)
  1160. {
  1161. unsigned long mods = ctx->ctype.modifiers & MOD_DECLARE;
  1162. ctx->ctype.modifiers &= ~MOD_DECLARE;
  1163. return ctx->storage_class | mods;
  1164. }
  1165. static struct token *storage_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
  1166. {
  1167. int is_tls = ctx->ctype.modifiers & MOD_TLS;
  1168. unsigned long class = sym->ctype.modifiers;
  1169. const char *storage = modifier_name(class);
  1170. /* __thread can be used alone, or with extern or static */
  1171. if (is_tls && (class & ~(MOD_STATIC|MOD_EXTERN)))
  1172. sparse_error(next->pos, "__thread cannot be used with '%s'", storage);
  1173. else if (!ctx->storage_class)
  1174. ctx->storage_class = class;
  1175. else if (ctx->storage_class == class)
  1176. sparse_error(next->pos, "duplicate %s", storage);
  1177. else
  1178. sparse_error(next->pos, "multiple storage classes");
  1179. return next;
  1180. }
  1181. static struct token *thread_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
  1182. {
  1183. /* This GCC extension can be used alone, or with extern or static */
  1184. if (!(ctx->storage_class & ~(MOD_STATIC|MOD_EXTERN))) {
  1185. apply_qualifier(&next->pos, &ctx->ctype, MOD_TLS);
  1186. } else {
  1187. sparse_error(next->pos, "__thread cannot be used with '%s'",
  1188. modifier_name(ctx->storage_class));
  1189. }
  1190. return next;
  1191. }
  1192. static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
  1193. {
  1194. ctx->forced = 1;
  1195. return token;
  1196. }
  1197. static struct token *alignas_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
  1198. {
  1199. int alignment = 0;
  1200. if (!match_op(token, '(')) {
  1201. sparse_error(token->pos, "expected '(' after _Alignas");
  1202. return token;
  1203. }
  1204. if (lookup_type(token->next)) {
  1205. struct symbol *sym = NULL;
  1206. token = typename(token->next, &sym, NULL);
  1207. sym = examine_symbol_type(sym);
  1208. alignment = sym->ctype.alignment;
  1209. token = expect(token, ')', "after _Alignas(...");
  1210. } else {
  1211. struct expression *expr = NULL;
  1212. token = parens_expression(token, &expr, "after _Alignas");
  1213. if (!expr)
  1214. return token;
  1215. alignment = const_expression_value(expr);
  1216. }
  1217. if (alignment < 0) {
  1218. warning(token->pos, "non-positive alignment");
  1219. return token;
  1220. }
  1221. if (alignment & (alignment-1)) {
  1222. warning(token->pos, "non-power-of-2 alignment");
  1223. return token;
  1224. }
  1225. if (alignment > ctx->ctype.alignment)
  1226. ctx->ctype.alignment = alignment;
  1227. return token;
  1228. }
  1229. static struct token *generic_qualifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
  1230. {
  1231. apply_qualifier(&next->pos, &ctx->ctype, sym->ctype.modifiers);
  1232. return next;
  1233. }
  1234. static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
  1235. {
  1236. unsigned long mod = thistype->modifiers;
  1237. if (mod)
  1238. apply_qualifier(&pos, ctype, mod);
  1239. /* Context */
  1240. concat_ptr_list((struct ptr_list *)thistype->contexts,
  1241. (struct ptr_list **)&ctype->contexts);
  1242. /* Alignment */
  1243. if (thistype->alignment > ctype->alignment)
  1244. ctype->alignment = thistype->alignment;
  1245. /* Address space */
  1246. if (thistype->as)
  1247. ctype->as = thistype->as;
  1248. }
  1249. static void specifier_conflict(struct position pos, int what, struct ident *new)
  1250. {
  1251. const char *old;
  1252. if (what & (Set_S | Set_T))
  1253. goto Catch_all;
  1254. if (what & Set_Char)
  1255. old = "char";
  1256. else if (what & Set_Double)
  1257. old = "double";
  1258. else if (what & Set_Float)
  1259. old = "float";
  1260. else if (what & Set_Signed)
  1261. old = "signed";
  1262. else if (what & Set_Unsigned)
  1263. old = "unsigned";
  1264. else if (what & Set_Short)
  1265. old = "short";
  1266. else if (what & Set_Long)
  1267. old = "long";
  1268. else
  1269. old = "long long";
  1270. sparse_error(pos, "impossible combination of type specifiers: %s %s",
  1271. old, show_ident(new));
  1272. return;
  1273. Catch_all:
  1274. sparse_error(pos, "two or more data types in declaration specifiers");
  1275. }
  1276. static struct symbol * const int_types[] =
  1277. {&char_ctype, &short_ctype, &int_ctype, &long_ctype, &llong_ctype, &int128_ctype};
  1278. static struct symbol * const signed_types[] =
  1279. {&schar_ctype, &sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
  1280. &sint128_ctype};
  1281. static struct symbol * const unsigned_types[] =
  1282. {&uchar_ctype, &ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
  1283. &uint128_ctype};
  1284. static struct symbol * const real_types[] =
  1285. {&float_ctype, &double_ctype, &ldouble_ctype};
  1286. static struct symbol * const * const types[] = {
  1287. [CInt] = int_types + 2,
  1288. [CSInt] = signed_types + 2,
  1289. [CUInt] = unsigned_types + 2,
  1290. [CReal] = real_types + 1,
  1291. };
  1292. struct symbol *ctype_integer(int size, int want_unsigned)
  1293. {
  1294. return types[want_unsigned ? CUInt : CInt][size];
  1295. }
  1296. static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
  1297. {
  1298. while (token_type(t) == TOKEN_IDENT) {
  1299. struct symbol *s = lookup_keyword(t->ident, NS_TYPEDEF);
  1300. if (!s)
  1301. break;
  1302. if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
  1303. break;
  1304. t = t->next;
  1305. if (s->op->declarator)
  1306. t = s->op->declarator(t, s, ctx);
  1307. }
  1308. return t;
  1309. }
  1310. static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
  1311. {
  1312. int seen = 0;
  1313. int class = CInt;
  1314. int rank = 0;
  1315. while (token_type(token) == TOKEN_IDENT) {
  1316. struct symbol *s = lookup_symbol(token->ident,
  1317. NS_TYPEDEF | NS_SYMBOL);
  1318. if (!s || !(s->namespace & NS_TYPEDEF))
  1319. break;
  1320. if (s->type != SYM_KEYWORD) {
  1321. if (seen & Set_Any)
  1322. break;
  1323. seen |= Set_S | Set_T;
  1324. ctx->ctype.base_type = s->ctype.base_type;
  1325. apply_ctype(token->pos, &s->ctype, &ctx->ctype);
  1326. token = token->next;
  1327. continue;
  1328. }
  1329. if (s->op->type & KW_SPECIFIER) {
  1330. if (seen & s->op->test) {
  1331. specifier_conflict(token->pos,
  1332. seen & s->op->test,
  1333. token->ident);
  1334. break;
  1335. }
  1336. seen |= s->op->set;
  1337. class += s->op->class;
  1338. if (s->op->set & Set_Int128)
  1339. rank = 3;
  1340. else if (s->op->set & Set_Char)
  1341. rank = -2;
  1342. if (s->op->set & (Set_Short|Set_Float)) {
  1343. rank = -1;
  1344. } else if (s->op->set & Set_Long && rank++) {
  1345. if (class == CReal) {
  1346. specifier_conflict(token->pos,
  1347. Set_Vlong,
  1348. &double_ident);
  1349. break;
  1350. }
  1351. seen |= Set_Vlong;
  1352. }
  1353. }
  1354. token = token->next;
  1355. if (s->op->declarator) // Note: this eats attributes
  1356. token = s->op->declarator(token, s, ctx);
  1357. if (s->op->type & KW_EXACT) {
  1358. ctx->ctype.base_type = s->ctype.base_type;
  1359. ctx->ctype.modifiers |= s->ctype.modifiers;
  1360. }
  1361. }
  1362. if (!(seen & Set_S)) { /* not set explicitly? */
  1363. struct symbol *base = &incomplete_ctype;
  1364. if (seen & Set_Any)
  1365. base = types[class][rank];
  1366. ctx->ctype.base_type = base;
  1367. }
  1368. if (ctx->ctype.modifiers & MOD_BITWISE) {
  1369. struct symbol *type;
  1370. ctx->ctype.modifiers &= ~MOD_BITWISE;
  1371. if (!is_int_type(ctx->ctype.base_type)) {
  1372. sparse_error(token->pos, "invalid modifier");
  1373. return token;
  1374. }
  1375. type = alloc_symbol(token->pos, SYM_BASETYPE);
  1376. *type = *ctx->ctype.base_type;
  1377. type->ctype.modifiers &= ~MOD_SPECIFIER;
  1378. type->ctype.base_type = ctx->ctype.base_type;
  1379. type->type = SYM_RESTRICT;
  1380. ctx->ctype.base_type = type;
  1381. create_fouled(type);
  1382. }
  1383. return token;
  1384. }
  1385. static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
  1386. {
  1387. struct expression *expr = NULL;
  1388. int has_static = 0;
  1389. while (token_type(token) == TOKEN_IDENT) {
  1390. struct symbol *sym = lookup_keyword(token->ident, NS_TYPEDEF);
  1391. if (!sym || !(sym->op->type & (KW_STATIC|KW_QUALIFIER)))
  1392. break;
  1393. if (has_static && (sym->op->type & KW_STATIC))
  1394. sparse_error(token->pos, "duplicate array static declarator");
  1395. has_static |= (sym->op->type & KW_STATIC);
  1396. token = token->next;
  1397. }
  1398. if (match_op(token, '*') && match_op(token->next, ']')) {
  1399. // FIXME: '[*]' is treated like '[]'
  1400. token = token->next;
  1401. } else {
  1402. token = assignment_expression(token, &expr);
  1403. }
  1404. sym->array_size = expr;
  1405. return token;
  1406. }
  1407. static struct token *parameter_type_list(struct token *, struct symbol *);
  1408. static struct token *identifier_list(struct token *, struct symbol *);
  1409. static struct token *declarator(struct token *token, struct decl_state *ctx);
  1410. static struct token *handle_asm_name(struct token *token, struct decl_state *ctx)
  1411. {
  1412. struct expression *expr;
  1413. struct symbol *keyword;
  1414. if (token_type(token) != TOKEN_IDENT)
  1415. return token;
  1416. keyword = lookup_keyword(token->ident, NS_KEYWORD);
  1417. if (!keyword)
  1418. return token;
  1419. if (!(keyword->op->type & KW_ASM))
  1420. return token;
  1421. token = token->next;
  1422. token = expect(token, '(', "after asm");
  1423. token = string_expression(token, &expr, "asm name");
  1424. token = expect(token, ')', "after asm");
  1425. return token;
  1426. }
  1427. ///
  1428. // test if @token is '__attribute__' (or one of its variant)
  1429. static bool match_attribute(struct token *token)
  1430. {
  1431. struct symbol *sym;
  1432. if (token_type(token) != TOKEN_IDENT)
  1433. return false;
  1434. sym = lookup_keyword(token->ident, NS_TYPEDEF);
  1435. if (!sym)
  1436. return false;
  1437. return sym->op->type & KW_ATTRIBUTE;
  1438. }
  1439. static struct token *skip_attribute(struct token *token)
  1440. {
  1441. token = token->next;
  1442. if (match_op(token, '(')) {
  1443. int depth = 1;
  1444. token = token->next;
  1445. while (depth && !eof_token(token)) {
  1446. if (token_type(token) == TOKEN_SPECIAL) {
  1447. if (token->special == '(')
  1448. depth++;
  1449. else if (token->special == ')')
  1450. depth--;
  1451. }
  1452. token = token->next;
  1453. }
  1454. }
  1455. return token;
  1456. }
  1457. static struct token *skip_attributes(struct token *token)
  1458. {
  1459. while (match_attribute(token)) {
  1460. token = expect(token->next, '(', "after attribute");
  1461. token = expect(token, '(', "after attribute");
  1462. while (token_type(token) == TOKEN_IDENT) {
  1463. token = skip_attribute(token);
  1464. if (!match_op(token, ','))
  1465. break;
  1466. token = token->next;
  1467. }
  1468. token = expect(token, ')', "after attribute");
  1469. token = expect(token, ')', "after attribute");
  1470. }
  1471. return token;
  1472. }
  1473. static struct token *handle_attributes(struct token *token, struct decl_state *ctx)
  1474. {
  1475. while (match_attribute(token))
  1476. token = attribute_specifier(token->next, NULL, ctx);
  1477. return token;
  1478. }
  1479. static int is_nested(struct token *token, struct token **p,
  1480. int prefer_abstract)
  1481. {
  1482. /*
  1483. * This can be either a parameter list or a grouping.
  1484. * For the direct (non-abstract) case, we know if must be
  1485. * a parameter list if we already saw the identifier.
  1486. * For the abstract case, we know if must be a parameter
  1487. * list if it is empty or starts with a type.
  1488. */
  1489. struct token *next = token->next;
  1490. *p = next = skip_attributes(next);
  1491. if (token_type(next) == TOKEN_IDENT) {
  1492. if (lookup_type(next))
  1493. return !prefer_abstract;
  1494. return 1;
  1495. }
  1496. if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
  1497. return 0;
  1498. return 1;
  1499. }
  1500. enum kind {
  1501. Empty, K_R, Proto, Bad_Func,
  1502. };
  1503. static enum kind which_func(struct token *token,
  1504. struct ident **n,
  1505. int prefer_abstract)
  1506. {
  1507. struct token *next = token->next;
  1508. if (token_type(next) == TOKEN_IDENT) {
  1509. if (lookup_type(next))
  1510. return Proto;
  1511. /* identifier list not in definition; complain */
  1512. if (prefer_abstract)
  1513. warning(token->pos,
  1514. "identifier list not in definition");
  1515. return K_R;
  1516. }
  1517. if (token_type(next) != TOKEN_SPECIAL)
  1518. return Bad_Func;
  1519. if (next->special == ')') {
  1520. /* don't complain about those */
  1521. if (!n || match_op(next->next, ';') || match_op(next->next, ','))
  1522. return Empty;
  1523. if (Wstrict_prototypes)
  1524. warning(next->pos,
  1525. "non-ANSI function declaration of function '%s'",
  1526. show_ident(*n));
  1527. return Empty;
  1528. }
  1529. if (next->special == SPECIAL_ELLIPSIS) {
  1530. warning(next->pos,
  1531. "variadic functions must have one named argument");
  1532. return Proto;
  1533. }
  1534. return Bad_Func;
  1535. }
  1536. static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
  1537. {
  1538. struct ctype *ctype = &ctx->ctype;
  1539. struct token *next;
  1540. struct ident **p = ctx->ident;
  1541. if (ctx->ident && token_type(token) == TOKEN_IDENT) {
  1542. *ctx->ident = token->ident;
  1543. token = token->next;
  1544. } else if (match_op(token, '(') &&
  1545. is_nested(token, &next, ctx->prefer_abstract)) {
  1546. struct symbol *base_type = ctype->base_type;
  1547. if (token->next != next)
  1548. next = handle_attributes(token->next, ctx);
  1549. token = declarator(next, ctx);
  1550. token = expect(token, ')', "in nested declarator");
  1551. while (ctype->base_type != base_type)
  1552. ctype = &ctype->base_type->ctype;
  1553. p = NULL;
  1554. }
  1555. if (match_op(token, '(')) {
  1556. enum kind kind = which_func(token, p, ctx->prefer_abstract);
  1557. struct symbol *fn;
  1558. fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
  1559. ctype->modifiers |= ctx->f_modifiers;
  1560. token = token->next;
  1561. if (kind == K_R)
  1562. token = identifier_list(token, fn);
  1563. else if (kind == Proto)
  1564. token = parameter_type_list(token, fn);
  1565. token = expect(token, ')', "in function declarator");
  1566. fn->endpos = token->pos;
  1567. return token;
  1568. }
  1569. while (match_op(token, '[')) {
  1570. struct symbol *array;
  1571. array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
  1572. token = abstract_array_declarator(token->next, array);
  1573. token = expect(token, ']', "in abstract_array_declarator");
  1574. array->endpos = token->pos;
  1575. ctype = &array->ctype;
  1576. }
  1577. return token;
  1578. }
  1579. static struct token *pointer(struct token *token, struct decl_state *ctx)
  1580. {
  1581. while (match_op(token,'*')) {
  1582. struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
  1583. ptr->ctype.modifiers = ctx->ctype.modifiers;
  1584. ptr->ctype.base_type = ctx->ctype.base_type;
  1585. ptr->ctype.as = ctx->ctype.as;
  1586. ptr->ctype.contexts = ctx->ctype.contexts;
  1587. ctx->ctype.modifiers = 0;
  1588. ctx->ctype.base_type = ptr;
  1589. ctx->ctype.as = NULL;
  1590. ctx->ctype.contexts = NULL;
  1591. ctx->ctype.alignment = 0;
  1592. token = handle_qualifiers(token->next, ctx);
  1593. ctx->ctype.base_type->endpos = token->pos;
  1594. }
  1595. return token;
  1596. }
  1597. static struct token *declarator(struct token *token, struct decl_state *ctx)
  1598. {
  1599. token = pointer(token, ctx);
  1600. return direct_declarator(token, ctx);
  1601. }
  1602. static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
  1603. {
  1604. struct ctype *ctype = &ctx->ctype;
  1605. struct expression *expr;
  1606. struct symbol *bitfield;
  1607. long long width;
  1608. if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
  1609. sparse_error(token->pos, "invalid bitfield specifier for type %s.",
  1610. show_typename(ctype->base_type));
  1611. // Parse this to recover gracefully.
  1612. return conditional_expression(token->next, &expr);
  1613. }
  1614. bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
  1615. token = conditional_expression(token->next, &expr);
  1616. width = const_expression_value(expr);
  1617. bitfield->bit_size = width;
  1618. if (width < 0 || width > INT_MAX || (*ctx->ident && width == 0)) {
  1619. sparse_error(token->pos, "bitfield '%s' has invalid width (%lld)",
  1620. show_ident(*ctx->ident), width);
  1621. width = -1;
  1622. } else if (*ctx->ident) {
  1623. struct symbol *base_type = bitfield->ctype.base_type;
  1624. struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
  1625. int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
  1626. if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
  1627. // Valid values are either {-1;0} or {0}, depending on integer
  1628. // representation. The latter makes for very efficient code...
  1629. sparse_error(token->pos, "dubious one-bit signed bitfield");
  1630. }
  1631. if (Wdefault_bitfield_sign &&
  1632. bitfield_type->type != SYM_ENUM &&
  1633. !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
  1634. is_signed) {
  1635. // The sign of bitfields is unspecified by default.
  1636. warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
  1637. }
  1638. }
  1639. bitfield->bit_size = width;
  1640. bitfield->endpos = token->pos;
  1641. bitfield->ident = *ctx->ident;
  1642. return token;
  1643. }
  1644. static struct token *declaration_list(struct token *token, struct symbol_list **list)
  1645. {
  1646. struct decl_state ctx = {.prefer_abstract = 0};
  1647. struct ctype saved;
  1648. unsigned long mod;
  1649. token = declaration_specifiers(token, &ctx);
  1650. mod = decl_modifiers(&ctx);
  1651. saved = ctx.ctype;
  1652. for (;;) {
  1653. struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
  1654. ctx.ident = &decl->ident;
  1655. token = declarator(token, &ctx);
  1656. if (match_op(token, ':'))
  1657. token = handle_bitfield(token, &ctx);
  1658. token = handle_attributes(token, &ctx);
  1659. apply_modifiers(token->pos, &ctx);
  1660. decl->ctype = ctx.ctype;
  1661. decl->ctype.modifiers |= mod;
  1662. decl->endpos = token->pos;
  1663. add_symbol(list, decl);
  1664. if (!match_op(token, ','))
  1665. break;
  1666. token = token->next;
  1667. ctx.ctype = saved;
  1668. }
  1669. return token;
  1670. }
  1671. static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
  1672. {
  1673. while (!match_op(token, '}')) {
  1674. if (match_ident(token, &_Static_assert_ident)) {
  1675. token = parse_static_assert(token, NULL);
  1676. continue;
  1677. }
  1678. if (!match_op(token, ';'))
  1679. token = declaration_list(token, list);
  1680. if (!match_op(token, ';')) {
  1681. sparse_error(token->pos, "expected ; at end of declaration");
  1682. break;
  1683. }
  1684. token = token->next;
  1685. }
  1686. return token;
  1687. }
  1688. static struct token *parameter_declaration(struct token *token, struct symbol *sym)
  1689. {
  1690. struct decl_state ctx = {.prefer_abstract = 1};
  1691. token = declaration_specifiers(token, &ctx);
  1692. ctx.ident = &sym->ident;
  1693. token = declarator(token, &ctx);
  1694. token = handle_attributes(token, &ctx);
  1695. apply_modifiers(token->pos, &ctx);
  1696. sym->ctype = ctx.ctype;
  1697. sym->ctype.modifiers |= decl_modifiers(&ctx);
  1698. sym->endpos = token->pos;
  1699. sym->forced_arg = ctx.forced;
  1700. return token;
  1701. }
  1702. struct token *typename(struct token *token, struct symbol **p, int *forced)
  1703. {
  1704. struct decl_state ctx = {.prefer_abstract = 1};
  1705. unsigned long class;
  1706. struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
  1707. *p = sym;
  1708. token = declaration_specifiers(token, &ctx);
  1709. token = declarator(token, &ctx);
  1710. apply_modifiers(token->pos, &ctx);
  1711. sym->ctype = ctx.ctype;
  1712. sym->endpos = token->pos;
  1713. class = ctx.storage_class;
  1714. if (forced)
  1715. *forced = ctx.forced;
  1716. if (class)
  1717. warning(sym->pos, "storage class in typename (%s%s)",
  1718. modifier_string(class), show_typename(sym));
  1719. return token;
  1720. }
  1721. static struct token *expression_statement(struct token *token, struct expression **tree)
  1722. {
  1723. token = parse_expression(token, tree);
  1724. return expect(token, ';', "at end of statement");
  1725. }
  1726. static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
  1727. struct asm_operand_list **inout)
  1728. {
  1729. /* Allow empty operands */
  1730. if (match_op(token->next, ':') || match_op(token->next, ')'))
  1731. return token->next;
  1732. do {
  1733. struct asm_operand *op = __alloc_asm_operand(0);
  1734. if (match_op(token->next, '[') &&
  1735. token_type(token->next->next) == TOKEN_IDENT &&
  1736. match_op(token->next->next->next, ']')) {
  1737. op->name = token->next->next->ident;
  1738. token = token->next->next->next;
  1739. }
  1740. token = token->next;
  1741. token = string_expression(token, &op->constraint, "asm constraint");
  1742. token = parens_expression(token, &op->expr, "in asm parameter");
  1743. add_ptr_list(inout, op);
  1744. } while (match_op(token, ','));
  1745. return token;
  1746. }
  1747. static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
  1748. struct expression_list **clobbers)
  1749. {
  1750. struct expression *expr;
  1751. do {
  1752. token = primary_expression(token->next, &expr);
  1753. if (expr)
  1754. add_expression(clobbers, expr);
  1755. } while (match_op(token, ','));
  1756. return token;
  1757. }
  1758. static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
  1759. struct symbol_list **labels)
  1760. {
  1761. struct symbol *label;
  1762. do {
  1763. token = token->next; /* skip ':' and ',' */
  1764. if (token_type(token) != TOKEN_IDENT)
  1765. return token;
  1766. label = label_symbol(token, 1);
  1767. add_symbol(labels, label);
  1768. token = token->next;
  1769. } while (match_op(token, ','));
  1770. return token;
  1771. }
  1772. static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
  1773. {
  1774. unsigned long mods = 0;
  1775. token = token->next;
  1776. stmt->type = STMT_ASM;
  1777. while (token_type(token) == TOKEN_IDENT) {
  1778. struct symbol *s = lookup_keyword(token->ident, NS_TYPEDEF);
  1779. if (s && s->op->asm_modifier)
  1780. s->op->asm_modifier(token, &mods, s->ctype.modifiers);
  1781. else if (token->ident == &goto_ident)
  1782. asm_modifier(token, &mods, MOD_ASM_GOTO);
  1783. token = token->next;
  1784. }
  1785. token = expect(token, '(', "after asm");
  1786. token = string_expression(token, &stmt->asm_string, "inline asm");
  1787. if (match_op(token, ':'))
  1788. token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
  1789. if (match_op(token, ':'))
  1790. token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
  1791. if (match_op(token, ':'))
  1792. token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
  1793. if (match_op(token, ':') && (mods & MOD_ASM_GOTO))
  1794. token = parse_asm_labels(token, stmt, &stmt->asm_labels);
  1795. token = expect(token, ')', "after asm");
  1796. return expect(token, ';', "at end of asm-statement");
  1797. }
  1798. static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
  1799. {
  1800. struct expression *cond = NULL, *message = NULL;
  1801. token = expect(token->next, '(', "after _Static_assert");
  1802. token = constant_expression(token, &cond);
  1803. if (!cond)
  1804. sparse_error(token->pos, "Expected constant expression");
  1805. if (match_op(token, ',')) {
  1806. token = token->next;
  1807. token = string_expression(token, &message, "_Static_assert()");
  1808. if (!message)
  1809. cond = NULL;
  1810. }
  1811. token = expect(token, ')', "after diagnostic message in _Static_assert");
  1812. token = expect(token, ';', "after _Static_assert()");
  1813. if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE) {
  1814. const char *sep = "", *msg = "";
  1815. if (message) {
  1816. sep = ": ";
  1817. msg = show_string(message->string);
  1818. }
  1819. sparse_error(cond->pos, "static assertion failed%s%s", sep, msg);
  1820. }
  1821. return token;
  1822. }
  1823. /* Make a statement out of an expression */
  1824. static struct statement *make_statement(struct expression *expr)
  1825. {
  1826. struct statement *stmt;
  1827. if (!expr)
  1828. return NULL;
  1829. stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
  1830. stmt->expression = expr;
  1831. return stmt;
  1832. }
  1833. /*
  1834. * All iterators have two symbols associated with them:
  1835. * the "continue" and "break" symbols, which are targets
  1836. * for continue and break statements respectively.
  1837. *
  1838. * They are in a special name-space, but they follow
  1839. * all the normal visibility rules, so nested iterators
  1840. * automatically work right.
  1841. */
  1842. static void start_iterator(struct statement *stmt)
  1843. {
  1844. struct symbol *cont, *brk;
  1845. start_block_scope();
  1846. cont = alloc_symbol(stmt->pos, SYM_NODE);
  1847. bind_symbol(cont, &continue_ident, NS_ITERATOR);
  1848. brk = alloc_symbol(stmt->pos, SYM_NODE);
  1849. bind_symbol(brk, &break_ident, NS_ITERATOR);
  1850. stmt->type = STMT_ITERATOR;
  1851. stmt->iterator_break = brk;
  1852. stmt->iterator_continue = cont;
  1853. fn_local_symbol(brk);
  1854. fn_local_symbol(cont);
  1855. }
  1856. static void end_iterator(struct statement *stmt)
  1857. {
  1858. end_block_scope();
  1859. }
  1860. static struct statement *start_function(struct symbol *sym)
  1861. {
  1862. struct symbol *ret;
  1863. struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
  1864. start_function_scope();
  1865. ret = alloc_symbol(sym->pos, SYM_NODE);
  1866. ret->ctype = sym->ctype.base_type->ctype;
  1867. ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_QUALIFIER | MOD_TLS | MOD_ACCESS | MOD_NOCAST | MOD_NODEREF);
  1868. ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
  1869. bind_symbol(ret, &return_ident, NS_ITERATOR);
  1870. stmt->ret = ret;
  1871. fn_local_symbol(ret);
  1872. // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
  1873. current_fn = sym;
  1874. return stmt;
  1875. }
  1876. static void end_function(struct symbol *sym)
  1877. {
  1878. current_fn = NULL;
  1879. end_function_scope();
  1880. }
  1881. /*
  1882. * A "switch()" statement, like an iterator, has a
  1883. * the "break" symbol associated with it. It works
  1884. * exactly like the iterator break - it's the target
  1885. * for any break-statements in scope, and means that
  1886. * "break" handling doesn't even need to know whether
  1887. * it's breaking out of an iterator or a switch.
  1888. *
  1889. * In addition, the "case" symbol is a marker for the
  1890. * case/default statements to find the switch statement
  1891. * that they are associated with.
  1892. */
  1893. static void start_switch(struct statement *stmt)
  1894. {
  1895. struct symbol *brk, *switch_case;
  1896. start_block_scope();
  1897. brk = alloc_symbol(stmt->pos, SYM_NODE);
  1898. bind_symbol(brk, &break_ident, NS_ITERATOR);
  1899. switch_case = alloc_symbol(stmt->pos, SYM_NODE);
  1900. bind_symbol(switch_case, &case_ident, NS_ITERATOR);
  1901. switch_case->stmt = stmt;
  1902. stmt->type = STMT_SWITCH;
  1903. stmt->switch_break = brk;
  1904. stmt->switch_case = switch_case;
  1905. fn_local_symbol(brk);
  1906. fn_local_symbol(switch_case);
  1907. }
  1908. static void end_switch(struct statement *stmt)
  1909. {
  1910. if (!stmt->switch_case->symbol_list)
  1911. warning(stmt->pos, "switch with no cases");
  1912. end_block_scope();
  1913. }
  1914. static void add_case_statement(struct statement *stmt)
  1915. {
  1916. struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
  1917. struct symbol *sym;
  1918. if (!target) {
  1919. sparse_error(stmt->pos, "not in switch scope");
  1920. stmt->type = STMT_NONE;
  1921. return;
  1922. }
  1923. sym = alloc_symbol(stmt->pos, SYM_NODE);
  1924. add_symbol(&target->symbol_list, sym);
  1925. sym->stmt = stmt;
  1926. stmt->case_label = sym;
  1927. fn_local_symbol(sym);
  1928. }
  1929. static struct token *parse_return_statement(struct token *token, struct statement *stmt)
  1930. {
  1931. struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
  1932. if (!target)
  1933. error_die(token->pos, "internal error: return without a function target");
  1934. stmt->type = STMT_RETURN;
  1935. stmt->ret_target = target;
  1936. return expression_statement(token->next, &stmt->ret_value);
  1937. }
  1938. static void validate_for_loop_decl(struct symbol *sym)
  1939. {
  1940. unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
  1941. if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
  1942. const char *name = show_ident(sym->ident);
  1943. sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
  1944. sym->ctype.modifiers &= ~MOD_STORAGE;
  1945. }
  1946. }
  1947. static struct token *parse_for_statement(struct token *token, struct statement *stmt)
  1948. {
  1949. struct symbol_list *syms;
  1950. struct expression *e1, *e2, *e3;
  1951. struct statement *iterator;
  1952. start_iterator(stmt);
  1953. token = expect(token->next, '(', "after 'for'");
  1954. syms = NULL;
  1955. e1 = NULL;
  1956. /* C99 variable declaration? */
  1957. if (lookup_type(token)) {
  1958. token = external_declaration(token, &syms, validate_for_loop_decl);
  1959. } else {
  1960. token = parse_expression(token, &e1);
  1961. token = expect(token, ';', "in 'for'");
  1962. }
  1963. token = parse_expression(token, &e2);
  1964. token = expect(token, ';', "in 'for'");
  1965. token = parse_expression(token, &e3);
  1966. token = expect(token, ')', "in 'for'");
  1967. token = statement(token, &iterator);
  1968. stmt->iterator_syms = syms;
  1969. stmt->iterator_pre_statement = make_statement(e1);
  1970. stmt->iterator_pre_condition = e2;
  1971. stmt->iterator_post_statement = make_statement(e3);
  1972. stmt->iterator_post_condition = NULL;
  1973. stmt->iterator_statement = iterator;
  1974. end_iterator(stmt);
  1975. return token;
  1976. }
  1977. static struct token *parse_while_statement(struct token *token, struct statement *stmt)
  1978. {
  1979. struct expression *expr;
  1980. struct statement *iterator;
  1981. start_iterator(stmt);
  1982. token = parens_expression(token->next, &expr, "after 'while'");
  1983. token = statement(token, &iterator);
  1984. stmt->iterator_pre_condition = expr;
  1985. stmt->iterator_post_condition = NULL;
  1986. stmt->iterator_statement = iterator;
  1987. end_iterator(stmt);
  1988. return token;
  1989. }
  1990. static struct token *parse_do_statement(struct token *token, struct statement *stmt)
  1991. {
  1992. struct expression *expr;
  1993. struct statement *iterator;
  1994. start_iterator(stmt);
  1995. token = statement(token->next, &iterator);
  1996. if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
  1997. token = token->next;
  1998. else
  1999. sparse_error(token->pos, "expected 'while' after 'do'");
  2000. token = parens_expression(token, &expr, "after 'do-while'");
  2001. stmt->iterator_post_condition = expr;
  2002. stmt->iterator_statement = iterator;
  2003. end_iterator(stmt);
  2004. if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
  2005. warning(iterator->pos, "do-while statement is not a compound statement");
  2006. return expect(token, ';', "after statement");
  2007. }
  2008. static struct token *parse_if_statement(struct token *token, struct statement *stmt)
  2009. {
  2010. stmt->type = STMT_IF;
  2011. token = parens_expression(token->next, &stmt->if_conditional, "after if");
  2012. token = statement(token, &stmt->if_true);
  2013. if (token_type(token) != TOKEN_IDENT)
  2014. return token;
  2015. if (token->ident != &else_ident)
  2016. return token;
  2017. return statement(token->next, &stmt->if_false);
  2018. }
  2019. static inline struct token *case_statement(struct token *token, struct statement *stmt)
  2020. {
  2021. stmt->type = STMT_CASE;
  2022. token = expect(token, ':', "after default/case");
  2023. add_case_statement(stmt);
  2024. return statement(token, &stmt->case_statement);
  2025. }
  2026. static struct token *parse_case_statement(struct token *token, struct statement *stmt)
  2027. {
  2028. token = parse_expression(token->next, &stmt->case_expression);
  2029. if (match_op(token, SPECIAL_ELLIPSIS))
  2030. token = parse_expression(token->next, &stmt->case_to);
  2031. return case_statement(token, stmt);
  2032. }
  2033. static struct token *parse_default_statement(struct token *token, struct statement *stmt)
  2034. {
  2035. return case_statement(token->next, stmt);
  2036. }
  2037. static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
  2038. {
  2039. struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
  2040. stmt->type = STMT_GOTO;
  2041. stmt->goto_label = target;
  2042. if (!target)
  2043. sparse_error(stmt->pos, "break/continue not in iterator scope");
  2044. return expect(token->next, ';', "at end of statement");
  2045. }
  2046. static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
  2047. {
  2048. stmt->type = STMT_SWITCH;
  2049. start_switch(stmt);
  2050. token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
  2051. token = statement(token, &stmt->switch_statement);
  2052. end_switch(stmt);
  2053. return token;
  2054. }
  2055. static void warn_label_usage(struct position def, struct position use, struct ident *ident)
  2056. {
  2057. const char *id = show_ident(ident);
  2058. sparse_error(use, "label '%s' used outside statement expression", id);
  2059. info(def, " label '%s' defined here", id);
  2060. current_fn->bogus_linear = 1;
  2061. }
  2062. void check_label_usage(struct symbol *label, struct position use_pos)
  2063. {
  2064. struct statement *def = label->stmt;
  2065. if (def) {
  2066. if (!is_in_scope(def->label_scope, label_scope))
  2067. warn_label_usage(def->pos, use_pos, label->ident);
  2068. } else if (!label->label_scope) {
  2069. label->label_scope = label_scope;
  2070. label->label_pos = use_pos;
  2071. }
  2072. }
  2073. static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
  2074. {
  2075. stmt->type = STMT_GOTO;
  2076. token = token->next;
  2077. if (match_op(token, '*')) {
  2078. token = parse_expression(token->next, &stmt->goto_expression);
  2079. add_statement(&function_computed_goto_list, stmt);
  2080. } else if (token_type(token) == TOKEN_IDENT) {
  2081. struct symbol *label = label_symbol(token, 1);
  2082. stmt->goto_label = label;
  2083. check_label_usage(label, stmt->pos);
  2084. token = token->next;
  2085. } else {
  2086. sparse_error(token->pos, "Expected identifier or goto expression");
  2087. }
  2088. return expect(token, ';', "at end of statement");
  2089. }
  2090. static struct token *parse_context_statement(struct token *token, struct statement *stmt)
  2091. {
  2092. stmt->type = STMT_CONTEXT;
  2093. token = token->next;
  2094. token = expect(token, '(', "after __context__ statement");
  2095. token = assignment_expression(token, &stmt->expression);
  2096. if (!stmt->expression)
  2097. unexpected(token, "expression expected after '('");
  2098. if (match_op(token, ',')) {
  2099. token = token->next;
  2100. stmt->context = stmt->expression;
  2101. token = assignment_expression(token, &stmt->expression);
  2102. if (!stmt->expression)
  2103. unexpected(token, "expression expected after ','");
  2104. }
  2105. token = expect(token, ')', "at end of __context__ statement");
  2106. return expect(token, ';', "at end of statement");
  2107. }
  2108. static struct token *parse_range_statement(struct token *token, struct statement *stmt)
  2109. {
  2110. stmt->type = STMT_RANGE;
  2111. token = token->next;
  2112. token = expect(token, '(', "after __range__ statement");
  2113. token = assignment_expression(token, &stmt->range_expression);
  2114. token = expect(token, ',', "after range expression");
  2115. token = assignment_expression(token, &stmt->range_low);
  2116. token = expect(token, ',', "after low range");
  2117. token = assignment_expression(token, &stmt->range_high);
  2118. token = expect(token, ')', "after range statement");
  2119. return expect(token, ';', "after range statement");
  2120. }
  2121. static struct token *handle_label_attributes(struct token *token, struct symbol *label)
  2122. {
  2123. struct decl_state ctx = { };
  2124. token = handle_attributes(token, &ctx);
  2125. label->label_modifiers = ctx.ctype.modifiers;
  2126. return token;
  2127. }
  2128. static struct token *statement(struct token *token, struct statement **tree)
  2129. {
  2130. struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
  2131. *tree = stmt;
  2132. if (token_type(token) == TOKEN_IDENT) {
  2133. struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
  2134. if (s && s->op->statement)
  2135. return s->op->statement(token, stmt);
  2136. if (match_op(token->next, ':')) {
  2137. struct symbol *s = label_symbol(token, 0);
  2138. token = handle_label_attributes(token->next->next, s);
  2139. if (s->stmt) {
  2140. sparse_error(stmt->pos, "label '%s' redefined", show_ident(s->ident));
  2141. // skip the label to avoid multiple definitions
  2142. return statement(token, tree);
  2143. }
  2144. stmt->type = STMT_LABEL;
  2145. stmt->label_identifier = s;
  2146. stmt->label_scope = label_scope;
  2147. if (s->label_scope) {
  2148. if (!is_in_scope(label_scope, s->label_scope))
  2149. warn_label_usage(stmt->pos, s->label_pos, s->ident);
  2150. }
  2151. s->stmt = stmt;
  2152. return statement(token, &stmt->label_statement);
  2153. }
  2154. }
  2155. if (match_op(token, '{')) {
  2156. token = compound_statement(token->next, stmt);
  2157. return expect(token, '}', "at end of compound statement");
  2158. }
  2159. stmt->type = STMT_EXPRESSION;
  2160. return expression_statement(token, &stmt->expression);
  2161. }
  2162. /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
  2163. static struct token *label_statement(struct token *token)
  2164. {
  2165. while (token_type(token) == TOKEN_IDENT) {
  2166. struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
  2167. /* it's block-scope, but we want label namespace */
  2168. bind_symbol_with_scope(sym, token->ident, NS_LABEL, block_scope);
  2169. fn_local_symbol(sym);
  2170. token = token->next;
  2171. if (!match_op(token, ','))
  2172. break;
  2173. token = token->next;
  2174. }
  2175. return expect(token, ';', "at end of label declaration");
  2176. }
  2177. static struct token * statement_list(struct token *token, struct statement_list **list)
  2178. {
  2179. int seen_statement = 0;
  2180. while (token_type(token) == TOKEN_IDENT &&
  2181. token->ident == &__label___ident)
  2182. token = label_statement(token->next);
  2183. for (;;) {
  2184. struct statement * stmt;
  2185. if (eof_token(token))
  2186. break;
  2187. if (match_op(token, '}'))
  2188. break;
  2189. if (match_ident(token, &_Static_assert_ident)) {
  2190. token = parse_static_assert(token, NULL);
  2191. continue;
  2192. }
  2193. if (lookup_type(token)) {
  2194. if (seen_statement) {
  2195. warning(token->pos, "mixing declarations and code");
  2196. seen_statement = 0;
  2197. }
  2198. stmt = alloc_statement(token->pos, STMT_DECLARATION);
  2199. token = external_declaration(token, &stmt->declaration, NULL);
  2200. } else {
  2201. seen_statement = Wdeclarationafterstatement;
  2202. token = statement(token, &stmt);
  2203. }
  2204. add_statement(list, stmt);
  2205. }
  2206. return token;
  2207. }
  2208. static struct token *identifier_list(struct token *token, struct symbol *fn)
  2209. {
  2210. struct symbol_list **list = &fn->arguments;
  2211. for (;;) {
  2212. struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
  2213. sym->ident = token->ident;
  2214. token = token->next;
  2215. sym->endpos = token->pos;
  2216. sym->ctype.base_type = &incomplete_ctype;
  2217. add_symbol(list, sym);
  2218. if (!match_op(token, ',') ||
  2219. token_type(token->next) != TOKEN_IDENT ||
  2220. lookup_type(token->next))
  2221. break;
  2222. token = token->next;
  2223. }
  2224. return token;
  2225. }
  2226. static struct token *parameter_type_list(struct token *token, struct symbol *fn)
  2227. {
  2228. struct symbol_list **list = &fn->arguments;
  2229. for (;;) {
  2230. struct symbol *sym;
  2231. if (match_op(token, SPECIAL_ELLIPSIS)) {
  2232. fn->variadic = 1;
  2233. token = token->next;
  2234. break;
  2235. }
  2236. sym = alloc_symbol(token->pos, SYM_NODE);
  2237. token = parameter_declaration(token, sym);
  2238. if (sym->ctype.base_type == &void_ctype) {
  2239. /* Special case: (void) */
  2240. if (!*list && !sym->ident)
  2241. break;
  2242. warning(token->pos, "void parameter");
  2243. }
  2244. add_symbol(list, sym);
  2245. if (!match_op(token, ','))
  2246. break;
  2247. token = token->next;
  2248. }
  2249. return token;
  2250. }
  2251. struct token *compound_statement(struct token *token, struct statement *stmt)
  2252. {
  2253. stmt->type = STMT_COMPOUND;
  2254. start_block_scope();
  2255. token = statement_list(token, &stmt->stmts);
  2256. end_block_scope();
  2257. return token;
  2258. }
  2259. static struct expression *identifier_expression(struct token *token)
  2260. {
  2261. struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
  2262. expr->expr_ident = token->ident;
  2263. return expr;
  2264. }
  2265. static struct expression *index_expression(struct expression *from, struct expression *to)
  2266. {
  2267. int idx_from, idx_to;
  2268. struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
  2269. idx_from = const_expression_value(from);
  2270. idx_to = idx_from;
  2271. if (to) {
  2272. idx_to = const_expression_value(to);
  2273. if (idx_to < idx_from || idx_from < 0)
  2274. warning(from->pos, "nonsense array initializer index range");
  2275. }
  2276. expr->idx_from = idx_from;
  2277. expr->idx_to = idx_to;
  2278. return expr;
  2279. }
  2280. static struct token *single_initializer(struct expression **ep, struct token *token)
  2281. {
  2282. int expect_equal = 0;
  2283. struct token *next = token->next;
  2284. struct expression **tail = ep;
  2285. int nested;
  2286. *ep = NULL;
  2287. if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
  2288. struct expression *expr = identifier_expression(token);
  2289. if (Wold_initializer)
  2290. warning(token->pos, "obsolete struct initializer, use C99 syntax");
  2291. token = initializer(&expr->ident_expression, next->next);
  2292. if (expr->ident_expression)
  2293. *ep = expr;
  2294. return token;
  2295. }
  2296. for (tail = ep, nested = 0; ; nested++, next = token->next) {
  2297. if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
  2298. struct expression *expr = identifier_expression(next);
  2299. *tail = expr;
  2300. tail = &expr->ident_expression;
  2301. expect_equal = 1;
  2302. token = next->next;
  2303. } else if (match_op(token, '[')) {
  2304. struct expression *from = NULL, *to = NULL, *expr;
  2305. token = constant_expression(token->next, &from);
  2306. if (!from) {
  2307. sparse_error(token->pos, "Expected constant expression");
  2308. break;
  2309. }
  2310. if (match_op(token, SPECIAL_ELLIPSIS))
  2311. token = constant_expression(token->next, &to);
  2312. expr = index_expression(from, to);
  2313. *tail = expr;
  2314. tail = &expr->idx_expression;
  2315. token = expect(token, ']', "at end of initializer index");
  2316. if (nested)
  2317. expect_equal = 1;
  2318. } else {
  2319. break;
  2320. }
  2321. }
  2322. if (nested && !expect_equal) {
  2323. if (!match_op(token, '='))
  2324. warning(token->pos, "obsolete array initializer, use C99 syntax");
  2325. else
  2326. expect_equal = 1;
  2327. }
  2328. if (expect_equal)
  2329. token = expect(token, '=', "at end of initializer index");
  2330. token = initializer(tail, token);
  2331. if (!*tail)
  2332. *ep = NULL;
  2333. return token;
  2334. }
  2335. static struct token *initializer_list(struct expression_list **list, struct token *token)
  2336. {
  2337. struct expression *expr;
  2338. for (;;) {
  2339. token = single_initializer(&expr, token);
  2340. if (!expr)
  2341. break;
  2342. add_expression(list, expr);
  2343. if (!match_op(token, ','))
  2344. break;
  2345. token = token->next;
  2346. }
  2347. return token;
  2348. }
  2349. struct token *initializer(struct expression **tree, struct token *token)
  2350. {
  2351. if (match_op(token, '{')) {
  2352. struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
  2353. *tree = expr;
  2354. if (!Wuniversal_initializer) {
  2355. struct token *next = token->next;
  2356. // '{ 0 }' is equivalent to '{ }' except for some
  2357. // warnings, like using 0 to initialize a null-pointer.
  2358. if (match_token_zero(next)) {
  2359. if (match_op(next->next, '}'))
  2360. expr->zero_init = 1;
  2361. }
  2362. }
  2363. token = initializer_list(&expr->expr_list, token->next);
  2364. return expect(token, '}', "at end of initializer");
  2365. }
  2366. return assignment_expression(token, tree);
  2367. }
  2368. static void declare_argument(struct symbol *sym, struct symbol *fn)
  2369. {
  2370. if (!sym->ident) {
  2371. sparse_error(sym->pos, "no identifier for function argument");
  2372. return;
  2373. }
  2374. bind_symbol(sym, sym->ident, NS_SYMBOL);
  2375. }
  2376. static struct token *parse_function_body(struct token *token, struct symbol *decl,
  2377. struct symbol_list **list)
  2378. {
  2379. struct symbol_list **old_symbol_list;
  2380. struct symbol *base_type = decl->ctype.base_type;
  2381. struct statement *stmt, **p;
  2382. struct symbol *prev;
  2383. struct symbol *arg;
  2384. old_symbol_list = function_symbol_list;
  2385. if (decl->ctype.modifiers & MOD_INLINE) {
  2386. function_symbol_list = &decl->inline_symbol_list;
  2387. p = &base_type->inline_stmt;
  2388. } else {
  2389. function_symbol_list = &decl->symbol_list;
  2390. p = &base_type->stmt;
  2391. }
  2392. function_computed_target_list = NULL;
  2393. function_computed_goto_list = NULL;
  2394. if ((decl->ctype.modifiers & (MOD_EXTERN|MOD_INLINE)) == MOD_EXTERN) {
  2395. if (Wexternal_function_has_definition)
  2396. warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
  2397. }
  2398. if (!(decl->ctype.modifiers & MOD_STATIC))
  2399. decl->ctype.modifiers |= MOD_EXTERN;
  2400. stmt = start_function(decl);
  2401. *p = stmt;
  2402. FOR_EACH_PTR (base_type->arguments, arg) {
  2403. declare_argument(arg, base_type);
  2404. } END_FOR_EACH_PTR(arg);
  2405. token = statement_list(token->next, &stmt->stmts);
  2406. end_function(decl);
  2407. if (!(decl->ctype.modifiers & MOD_INLINE))
  2408. add_symbol(list, decl);
  2409. check_declaration(decl);
  2410. decl->definition = decl;
  2411. prev = decl->same_symbol;
  2412. if (prev && prev->definition) {
  2413. warning(decl->pos, "multiple definitions for function '%s'",
  2414. show_ident(decl->ident));
  2415. info(prev->definition->pos, " the previous one is here");
  2416. } else {
  2417. while (prev) {
  2418. rebind_scope(prev, decl->scope);
  2419. prev->definition = decl;
  2420. prev = prev->same_symbol;
  2421. }
  2422. }
  2423. function_symbol_list = old_symbol_list;
  2424. if (function_computed_goto_list) {
  2425. if (!function_computed_target_list)
  2426. warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
  2427. else {
  2428. FOR_EACH_PTR(function_computed_goto_list, stmt) {
  2429. stmt->target_list = function_computed_target_list;
  2430. } END_FOR_EACH_PTR(stmt);
  2431. }
  2432. }
  2433. return expect(token, '}', "at end of function");
  2434. }
  2435. static void promote_k_r_types(struct symbol *arg)
  2436. {
  2437. struct symbol *base = arg->ctype.base_type;
  2438. if (base && base->ctype.base_type == &int_type && base->rank < 0) {
  2439. arg->ctype.base_type = &int_ctype;
  2440. }
  2441. }
  2442. static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
  2443. {
  2444. struct symbol_list *real_args = fn->ctype.base_type->arguments;
  2445. struct symbol *arg;
  2446. FOR_EACH_PTR(real_args, arg) {
  2447. struct symbol *type;
  2448. /* This is quadratic in the number of arguments. We _really_ don't care */
  2449. FOR_EACH_PTR(argtypes, type) {
  2450. if (type->ident == arg->ident)
  2451. goto match;
  2452. } END_FOR_EACH_PTR(type);
  2453. if (Wimplicit_int) {
  2454. sparse_error(arg->pos, "missing type declaration for parameter '%s'",
  2455. show_ident(arg->ident));
  2456. }
  2457. type = alloc_symbol(arg->pos, SYM_NODE);
  2458. type->ident = arg->ident;
  2459. type->ctype.base_type = &int_ctype;
  2460. match:
  2461. type->used = 1;
  2462. /* "char" and "short" promote to "int" */
  2463. promote_k_r_types(type);
  2464. arg->ctype = type->ctype;
  2465. } END_FOR_EACH_PTR(arg);
  2466. FOR_EACH_PTR(argtypes, arg) {
  2467. if (!arg->used)
  2468. warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
  2469. } END_FOR_EACH_PTR(arg);
  2470. }
  2471. static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
  2472. struct symbol_list **list)
  2473. {
  2474. struct symbol_list *args = NULL;
  2475. if (Wold_style_definition)
  2476. warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
  2477. do {
  2478. token = declaration_list(token, &args);
  2479. if (!match_op(token, ';')) {
  2480. sparse_error(token->pos, "expected ';' at end of parameter declaration");
  2481. break;
  2482. }
  2483. token = token->next;
  2484. } while (lookup_type(token));
  2485. apply_k_r_types(args, decl);
  2486. if (!match_op(token, '{')) {
  2487. sparse_error(token->pos, "expected function body");
  2488. return token;
  2489. }
  2490. return parse_function_body(token, decl, list);
  2491. }
  2492. static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
  2493. {
  2494. struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
  2495. struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
  2496. struct statement *stmt;
  2497. anon->ctype.base_type = fn;
  2498. stmt = alloc_statement(token->pos, STMT_NONE);
  2499. fn->stmt = stmt;
  2500. token = parse_asm_statement(token, stmt);
  2501. // FIXME: add_symbol(list, anon);
  2502. return token;
  2503. }
  2504. struct token *external_declaration(struct token *token, struct symbol_list **list,
  2505. validate_decl_t validate_decl)
  2506. {
  2507. struct ident *ident = NULL;
  2508. struct symbol *decl;
  2509. struct decl_state ctx = { .ident = &ident };
  2510. struct ctype saved;
  2511. struct symbol *base_type;
  2512. unsigned long mod;
  2513. int is_typedef;
  2514. /* Top-level inline asm or static assertion? */
  2515. if (token_type(token) == TOKEN_IDENT) {
  2516. struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
  2517. if (s && s->op->toplevel)
  2518. return s->op->toplevel(token, list);
  2519. }
  2520. /* Parse declaration-specifiers, if any */
  2521. token = declaration_specifiers(token, &ctx);
  2522. mod = decl_modifiers(&ctx);
  2523. decl = alloc_symbol(token->pos, SYM_NODE);
  2524. /* Just a type declaration? */
  2525. if (match_op(token, ';')) {
  2526. apply_modifiers(token->pos, &ctx);
  2527. return token->next;
  2528. }
  2529. saved = ctx.ctype;
  2530. token = declarator(token, &ctx);
  2531. token = handle_asm_name(token, &ctx);
  2532. token = handle_attributes(token, &ctx);
  2533. apply_modifiers(token->pos, &ctx);
  2534. decl->ctype = ctx.ctype;
  2535. decl->ctype.modifiers |= mod;
  2536. decl->endpos = token->pos;
  2537. /* Just a type declaration? */
  2538. if (!ident) {
  2539. warning(token->pos, "missing identifier in declaration");
  2540. return expect(token, ';', "at the end of type declaration");
  2541. }
  2542. /* type define declaration? */
  2543. is_typedef = ctx.storage_class == MOD_USERTYPE;
  2544. /* Typedefs don't have meaningful storage */
  2545. if (is_typedef)
  2546. decl->ctype.modifiers |= MOD_USERTYPE;
  2547. bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
  2548. base_type = decl->ctype.base_type;
  2549. if (is_typedef) {
  2550. if (base_type && !base_type->ident) {
  2551. switch (base_type->type) {
  2552. case SYM_STRUCT:
  2553. case SYM_UNION:
  2554. case SYM_ENUM:
  2555. case SYM_RESTRICT:
  2556. base_type->ident = ident;
  2557. break;
  2558. default:
  2559. break;
  2560. }
  2561. }
  2562. } else if (base_type && base_type->type == SYM_FN) {
  2563. if (base_type->ctype.base_type == &autotype_ctype) {
  2564. sparse_error(decl->pos, "'%s()' has __auto_type return type",
  2565. show_ident(decl->ident));
  2566. base_type->ctype.base_type = &int_ctype;
  2567. }
  2568. if (base_type->ctype.base_type == &incomplete_ctype) {
  2569. warning(decl->pos, "'%s()' has implicit return type",
  2570. show_ident(decl->ident));
  2571. base_type->ctype.base_type = &int_ctype;
  2572. }
  2573. /* apply attributes placed after the declarator */
  2574. decl->ctype.modifiers |= ctx.f_modifiers;
  2575. /* K&R argument declaration? */
  2576. if (lookup_type(token))
  2577. return parse_k_r_arguments(token, decl, list);
  2578. if (match_op(token, '{'))
  2579. return parse_function_body(token, decl, list);
  2580. if (!(decl->ctype.modifiers & MOD_STATIC))
  2581. decl->ctype.modifiers |= MOD_EXTERN;
  2582. } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
  2583. sparse_error(token->pos, "void declaration");
  2584. }
  2585. if (base_type == &incomplete_ctype) {
  2586. warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident));
  2587. decl->ctype.base_type = &int_ctype;;
  2588. }
  2589. for (;;) {
  2590. if (!is_typedef && match_op(token, '=')) {
  2591. struct token *next = token->next;
  2592. token = initializer(&decl->initializer, next);
  2593. if (token == next)
  2594. sparse_error(token->pos, "expression expected before '%s'", show_token(token));
  2595. }
  2596. if (!is_typedef) {
  2597. if (validate_decl)
  2598. validate_decl(decl);
  2599. if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
  2600. warning(decl->pos, "symbol with external linkage has initializer");
  2601. decl->ctype.modifiers &= ~MOD_EXTERN;
  2602. }
  2603. if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
  2604. add_symbol(list, decl);
  2605. fn_local_symbol(decl);
  2606. }
  2607. }
  2608. check_declaration(decl);
  2609. if (decl->same_symbol) {
  2610. decl->definition = decl->same_symbol->definition;
  2611. decl->op = decl->same_symbol->op;
  2612. if (is_typedef) {
  2613. // TODO: handle -std=c89 --pedantic
  2614. check_duplicates(decl);
  2615. }
  2616. }
  2617. if (ctx.autotype) {
  2618. const char *msg = NULL;
  2619. if (decl->ctype.base_type != &autotype_ctype)
  2620. msg = "on non-identifier";
  2621. else if (match_op(token, ','))
  2622. msg = "on declaration list";
  2623. else if (!decl->initializer)
  2624. msg = "without initializer";
  2625. else if (decl->initializer->type == EXPR_SYMBOL &&
  2626. decl->initializer->symbol == decl)
  2627. msg = "on self-init var";
  2628. if (msg) {
  2629. sparse_error(decl->pos, "__auto_type %s", msg);
  2630. decl->ctype.base_type = &bad_ctype;
  2631. }
  2632. }
  2633. if (!match_op(token, ','))
  2634. break;
  2635. token = token->next;
  2636. ident = NULL;
  2637. decl = alloc_symbol(token->pos, SYM_NODE);
  2638. ctx.ctype = saved;
  2639. token = handle_attributes(token, &ctx);
  2640. token = declarator(token, &ctx);
  2641. token = handle_asm_name(token, &ctx);
  2642. token = handle_attributes(token, &ctx);
  2643. apply_modifiers(token->pos, &ctx);
  2644. decl->ctype = ctx.ctype;
  2645. decl->ctype.modifiers |= mod;
  2646. decl->endpos = token->pos;
  2647. if (!ident) {
  2648. sparse_error(token->pos, "expected identifier name in type definition");
  2649. return token;
  2650. }
  2651. if (is_typedef)
  2652. decl->ctype.modifiers |= MOD_USERTYPE;
  2653. bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
  2654. /* Function declarations are automatically extern unless specifically static */
  2655. base_type = decl->ctype.base_type;
  2656. if (!is_typedef && base_type && base_type->type == SYM_FN) {
  2657. if (!(decl->ctype.modifiers & MOD_STATIC))
  2658. decl->ctype.modifiers |= MOD_EXTERN;
  2659. }
  2660. }
  2661. return expect(token, ';', "at end of declaration");
  2662. }