ngtcon.pas 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl, Jonas Maebe
  3. Generates code/nodes for typed constant declarations
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ngtcon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. aasmdata,
  23. node,nbas,
  24. symtype, symbase, symdef,symsym;
  25. type
  26. ttypedconstbuilder = class
  27. protected
  28. current_old_block_type : tblock_type;
  29. tcsym: tstaticvarsym;
  30. { this procedure reads typed constants }
  31. procedure read_typed_const_data(def:tdef);
  32. procedure parse_orddef(def: torddef);
  33. procedure parse_floatdef(def: tfloatdef);
  34. procedure parse_classrefdef(def: tclassrefdef);
  35. procedure parse_pointerdef(def: tpointerdef);
  36. procedure parse_setdef(def: tsetdef);
  37. procedure parse_enumdef(def: tenumdef);
  38. procedure parse_stringdef(def: tstringdef);
  39. procedure parse_arraydef(def:tarraydef);virtual;abstract;
  40. procedure parse_procvardef(def:tprocvardef);virtual;abstract;
  41. procedure parse_recorddef(def:trecorddef);virtual;abstract;
  42. procedure parse_objectdef(def:tobjectdef);virtual;abstract;
  43. procedure tc_emit_orddef(def: torddef; var node: tnode);virtual;abstract;
  44. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode);virtual;abstract;
  45. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);virtual;abstract;
  46. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);virtual;abstract;
  47. procedure tc_emit_setdef(def: tsetdef; var node: tnode);virtual;abstract;
  48. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);virtual;abstract;
  49. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);virtual;abstract;
  50. public
  51. constructor create(sym: tstaticvarsym);
  52. end;
  53. ttypedconstbuilderclass = class of ttypedconstbuilder;
  54. { should be changed into nested type of tasmlisttypedconstbuilder when
  55. possible }
  56. tbitpackedval = record
  57. curval, nextval: aword;
  58. curbitoffset: smallint;
  59. loadbitsize,packedbitsize: byte;
  60. end;
  61. tasmlisttypedconstbuilder = class(ttypedconstbuilder)
  62. private
  63. list: tasmlist;
  64. curoffset: asizeint;
  65. function parse_single_packed_const(def: tdef; var bp: tbitpackedval): boolean;
  66. protected
  67. procedure parse_packed_array_def(def: tarraydef);
  68. procedure parse_arraydef(def:tarraydef);override;
  69. procedure parse_procvardef(def:tprocvardef);override;
  70. procedure parse_recorddef(def:trecorddef);override;
  71. procedure parse_objectdef(def:tobjectdef);override;
  72. procedure tc_emit_orddef(def: torddef; var node: tnode);override;
  73. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode); override;
  74. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);override;
  75. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);override;
  76. procedure tc_emit_setdef(def: tsetdef; var node: tnode);override;
  77. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);override;
  78. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);override;
  79. public
  80. constructor create(sym: tstaticvarsym);virtual;
  81. function parse_into_asmlist: tasmlist;
  82. end;
  83. tasmlisttypedconstbuilderclass = class of tasmlisttypedconstbuilder;
  84. tnodetreetypedconstbuilder = class(ttypedconstbuilder)
  85. protected
  86. resultblock: tblocknode;
  87. statmnt: tstatementnode;
  88. { when parsing a record, the base nade becomes a loadnode of the record,
  89. etc. }
  90. basenode: tnode;
  91. procedure parse_arraydef(def:tarraydef);override;
  92. procedure parse_procvardef(def:tprocvardef);override;
  93. procedure parse_recorddef(def:trecorddef);override;
  94. procedure parse_objectdef(def:tobjectdef);override;
  95. procedure tc_emit_orddef(def: torddef; var node: tnode);override;
  96. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode); override;
  97. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);override;
  98. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);override;
  99. procedure tc_emit_setdef(def: tsetdef; var node: tnode);override;
  100. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);override;
  101. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);override;
  102. public
  103. constructor create(sym: tstaticvarsym; previnit: tnode);virtual;
  104. destructor destroy;override;
  105. function parse_into_nodetree: tnode;
  106. end;
  107. tnodetreetypedconstbuilderclass = class of tnodetreetypedconstbuilder;
  108. var
  109. ctypedconstbuilder: ttypedconstbuilderclass;
  110. implementation
  111. uses
  112. SysUtils,
  113. systems,tokens,verbose,constexp,
  114. cutils,globals,widestr,scanner,
  115. symconst,symtable,
  116. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  117. { pass 1 }
  118. htypechk,procinfo,
  119. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  120. { parser specific stuff }
  121. pbase,pexpr,pdecvar,
  122. { codegen }
  123. cpuinfo,cgbase,dbgbase,
  124. wpobase,asmutils
  125. ;
  126. {$maxfpuregisters 0}
  127. {*****************************************************************************
  128. read typed const
  129. *****************************************************************************}
  130. procedure ttypedconstbuilder.parse_orddef(def:torddef);
  131. var
  132. n : tnode;
  133. begin
  134. n:=comp_expr(true,false);
  135. { for C-style booleans, true=-1 and false=0) }
  136. if is_cbool(def) then
  137. inserttypeconv(n,def);
  138. tc_emit_orddef(def,n);
  139. n.free;
  140. end;
  141. procedure ttypedconstbuilder.parse_floatdef(def:tfloatdef);
  142. var
  143. n : tnode;
  144. begin
  145. n:=comp_expr(true,false);
  146. tc_emit_floatdef(def,n);
  147. n.free;
  148. end;
  149. procedure ttypedconstbuilder.parse_classrefdef(def:tclassrefdef);
  150. var
  151. n : tnode;
  152. begin
  153. n:=comp_expr(true,false);
  154. case n.nodetype of
  155. loadvmtaddrn:
  156. begin
  157. { update wpo info }
  158. if not assigned(current_procinfo) or
  159. (po_inline in current_procinfo.procdef.procoptions) or
  160. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname) then
  161. tobjectdef(tclassrefdef(n.resultdef).pointeddef).register_maybe_created_object_type;
  162. end;
  163. end;
  164. tc_emit_classrefdef(def,n);
  165. n.free;
  166. end;
  167. procedure ttypedconstbuilder.parse_pointerdef(def:tpointerdef);
  168. var
  169. p: tnode;
  170. begin
  171. p:=comp_expr(true,false);
  172. tc_emit_pointerdef(def,p);
  173. p.free;
  174. end;
  175. procedure ttypedconstbuilder.parse_setdef(def:tsetdef);
  176. var
  177. p : tnode;
  178. begin
  179. p:=comp_expr(true,false);
  180. tc_emit_setdef(def,p);
  181. p.free;
  182. end;
  183. procedure ttypedconstbuilder.parse_enumdef(def:tenumdef);
  184. var
  185. p : tnode;
  186. begin
  187. p:=comp_expr(true,false);
  188. tc_emit_enumdef(def,p);
  189. p.free;
  190. end;
  191. procedure ttypedconstbuilder.parse_stringdef(def:tstringdef);
  192. var
  193. n : tnode;
  194. begin
  195. n:=comp_expr(true,false);
  196. tc_emit_stringdef(def,n);
  197. n.free;
  198. end;
  199. { ttypedconstbuilder }
  200. procedure ttypedconstbuilder.read_typed_const_data(def:tdef);
  201. var
  202. prev_old_block_type,
  203. old_block_type: tblock_type;
  204. begin
  205. old_block_type:=block_type;
  206. prev_old_block_type:=current_old_block_type;
  207. current_old_block_type:=old_block_type;
  208. block_type:=bt_const;
  209. case def.typ of
  210. orddef :
  211. parse_orddef(torddef(def));
  212. floatdef :
  213. parse_floatdef(tfloatdef(def));
  214. classrefdef :
  215. parse_classrefdef(tclassrefdef(def));
  216. pointerdef :
  217. parse_pointerdef(tpointerdef(def));
  218. setdef :
  219. parse_setdef(tsetdef(def));
  220. enumdef :
  221. parse_enumdef(tenumdef(def));
  222. stringdef :
  223. parse_stringdef(tstringdef(def));
  224. arraydef :
  225. parse_arraydef(tarraydef(def));
  226. procvardef:
  227. parse_procvardef(tprocvardef(def));
  228. recorddef:
  229. parse_recorddef(trecorddef(def));
  230. objectdef:
  231. parse_objectdef(tobjectdef(def));
  232. errordef:
  233. begin
  234. { try to consume something useful }
  235. if token=_LKLAMMER then
  236. consume_all_until(_RKLAMMER)
  237. else
  238. consume_all_until(_SEMICOLON);
  239. end;
  240. else
  241. Message(parser_e_type_const_not_possible);
  242. end;
  243. block_type:=old_block_type;
  244. current_old_block_type:=prev_old_block_type;
  245. end;
  246. constructor ttypedconstbuilder.create(sym: tstaticvarsym);
  247. begin
  248. tcsym:=sym;
  249. end;
  250. {*****************************************************************************
  251. Bitpacked value helpers
  252. *****************************************************************************}
  253. procedure initbitpackval(out bp: tbitpackedval; packedbitsize: byte);
  254. begin
  255. bp.curval:=0;
  256. bp.nextval:=0;
  257. bp.curbitoffset:=0;
  258. bp.packedbitsize:=packedbitsize;
  259. bp.loadbitsize:=packedbitsloadsize(bp.packedbitsize)*8;
  260. end;
  261. {$ifopt r+}
  262. {$define rangeon}
  263. {$r-}
  264. {$endif}
  265. {$ifopt q+}
  266. {$define overflowon}
  267. {$q-}
  268. {$endif}
  269. { (values between quotes below refer to fields of bp; fields not }
  270. { mentioned are unused by this routine) }
  271. { bitpacks "value" as bitpacked value of bitsize "packedbitsize" into }
  272. { "curval", which has already been filled up to "curbitoffset", and }
  273. { stores the spillover if any into "nextval". It also updates }
  274. { curbitoffset to reflect how many bits of currval are now used (can be }
  275. { > AIntBits in case of spillover) }
  276. procedure bitpackval(value: aword; var bp: tbitpackedval);
  277. var
  278. shiftcount: longint;
  279. begin
  280. if (target_info.endian=endian_big) then
  281. begin
  282. { bitpacked format: left-aligned (i.e., "big endian bitness") }
  283. bp.curval:=bp.curval or ((value shl (AIntBits-bp.packedbitsize)) shr bp.curbitoffset);
  284. shiftcount:=((AIntBits-bp.packedbitsize)-bp.curbitoffset);
  285. { carry-over to the next element? }
  286. if (shiftcount<0) then
  287. bp.nextval:=(value and ((aword(1) shl (-shiftcount))-1)) shl
  288. (AIntBits+shiftcount)
  289. end
  290. else
  291. begin
  292. { bitpacked format: right aligned (i.e., "little endian bitness") }
  293. bp.curval:=bp.curval or (value shl bp.curbitoffset);
  294. { carry-over to the next element? }
  295. if (bp.curbitoffset+bp.packedbitsize>AIntBits) then
  296. bp.nextval:=value shr (AIntBits-bp.curbitoffset)
  297. end;
  298. inc(bp.curbitoffset,bp.packedbitsize);
  299. end;
  300. {$ifdef rangeon}
  301. {$r+}
  302. {$undef rangeon}
  303. {$endif}
  304. {$ifdef overflowon}
  305. {$q+}
  306. {$undef overflowon}
  307. {$endif}
  308. procedure flush_packed_value(list: tasmlist; var bp: tbitpackedval);
  309. var
  310. bitstowrite: longint;
  311. writeval : byte;
  312. begin
  313. if (bp.curbitoffset < AIntBits) then
  314. begin
  315. { forced flush -> write multiple of loadsize }
  316. bitstowrite:=align(bp.curbitoffset,bp.loadbitsize);
  317. bp.curbitoffset:=0;
  318. end
  319. else
  320. begin
  321. bitstowrite:=AIntBits;
  322. dec(bp.curbitoffset,AIntBits);
  323. end;
  324. while (bitstowrite>=8) do
  325. begin
  326. if (target_info.endian=endian_little) then
  327. begin
  328. { write lowest byte }
  329. writeval:=byte(bp.curval);
  330. bp.curval:=bp.curval shr 8;
  331. end
  332. else
  333. begin
  334. { write highest byte }
  335. writeval:=bp.curval shr (AIntBits-8);
  336. bp.curval:=(bp.curval and (not($ff shl (AIntBits-8)))) shl 8;
  337. end;
  338. list.concat(tai_const.create_8bit(writeval));
  339. dec(bitstowrite,8);
  340. end;
  341. bp.curval:=bp.nextval;
  342. bp.nextval:=0;
  343. end;
  344. { parses a packed array constant }
  345. procedure tasmlisttypedconstbuilder.parse_packed_array_def(def: tarraydef);
  346. var
  347. i : aint;
  348. bp : tbitpackedval;
  349. begin
  350. if not(def.elementdef.typ in [orddef,enumdef]) then
  351. internalerror(2007022010);
  352. { begin of the array }
  353. consume(_LKLAMMER);
  354. initbitpackval(bp,def.elepackedbitsize);
  355. i:=def.lowrange;
  356. { can't use for-loop, fails when cross-compiling from }
  357. { 32 to 64 bit because i is then 64 bit }
  358. while (i<def.highrange) do
  359. begin
  360. { get next item of the packed array }
  361. if not parse_single_packed_const(def.elementdef,bp) then
  362. exit;
  363. consume(_COMMA);
  364. inc(i);
  365. end;
  366. { final item }
  367. if not parse_single_packed_const(def.elementdef,bp) then
  368. exit;
  369. { flush final incomplete value if necessary }
  370. if (bp.curbitoffset <> 0) then
  371. flush_packed_value(list,bp);
  372. consume(_RKLAMMER);
  373. end;
  374. constructor tasmlisttypedconstbuilder.create(sym: tstaticvarsym);
  375. begin
  376. inherited;
  377. list:=tasmlist.create;
  378. curoffset:=0;
  379. end;
  380. procedure tasmlisttypedconstbuilder.tc_emit_stringdef(def: tstringdef; var node: tnode);
  381. var
  382. strlength : aint;
  383. strval : pchar;
  384. strch : char;
  385. ll : tasmlabel;
  386. ca : pchar;
  387. winlike : boolean;
  388. begin
  389. { load strval and strlength of the constant tree }
  390. if (node.nodetype=stringconstn) or is_wide_or_unicode_string(def) or is_constwidecharnode(node) or
  391. ((node.nodetype=typen) and is_interfacecorba(ttypenode(node).typedef)) then
  392. begin
  393. { convert to the expected string type so that
  394. for widestrings strval is a pcompilerwidestring }
  395. inserttypeconv(node,def);
  396. if (not codegenerror) and
  397. (node.nodetype=stringconstn) then
  398. begin
  399. strlength:=tstringconstnode(node).len;
  400. strval:=tstringconstnode(node).value_str;
  401. end
  402. else
  403. begin
  404. { an error occurred trying to convert the result to a string }
  405. strlength:=-1;
  406. { it's possible that the type conversion could not be
  407. evaluated at compile-time }
  408. if not codegenerror then
  409. CGMessage(parser_e_widestring_to_ansi_compile_time);
  410. end;
  411. end
  412. else if is_constcharnode(node) then
  413. begin
  414. { strval:=pchar(@tordconstnode(node).value);
  415. THIS FAIL on BIG_ENDIAN MACHINES PM }
  416. strch:=chr(tordconstnode(node).value.svalue and $ff);
  417. strval:=@strch;
  418. strlength:=1
  419. end
  420. else if is_constresourcestringnode(node) then
  421. begin
  422. strval:=pchar(tconstsym(tloadnode(node).symtableentry).value.valueptr);
  423. strlength:=tconstsym(tloadnode(node).symtableentry).value.len;
  424. end
  425. else
  426. begin
  427. Message(parser_e_illegal_expression);
  428. strlength:=-1;
  429. end;
  430. if strlength>=0 then
  431. begin
  432. case def.stringtype of
  433. st_shortstring:
  434. begin
  435. if strlength>=def.size then
  436. begin
  437. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  438. strlength:=def.size-1;
  439. end;
  440. list.concat(Tai_const.Create_8bit(strlength));
  441. { this can also handle longer strings }
  442. getmem(ca,strlength+1);
  443. move(strval^,ca^,strlength);
  444. ca[strlength]:=#0;
  445. list.concat(Tai_string.Create_pchar(ca,strlength));
  446. { fillup with spaces if size is shorter }
  447. if def.size>strlength then
  448. begin
  449. getmem(ca,def.size-strlength);
  450. { def.size contains also the leading length, so we }
  451. { we have to subtract one }
  452. fillchar(ca[0],def.size-strlength-1,' ');
  453. ca[def.size-strlength-1]:=#0;
  454. { this can also handle longer strings }
  455. list.concat(Tai_string.Create_pchar(ca,def.size-strlength-1));
  456. end;
  457. end;
  458. st_ansistring:
  459. begin
  460. { an empty ansi string is nil! }
  461. if (strlength=0) then
  462. ll := nil
  463. else
  464. ll := emit_ansistring_const(current_asmdata.asmlists[al_const],strval,strlength);
  465. list.concat(Tai_const.Create_sym(ll));
  466. end;
  467. st_unicodestring,
  468. st_widestring:
  469. begin
  470. { an empty wide/unicode string is nil! }
  471. if (strlength=0) then
  472. ll := nil
  473. else
  474. begin
  475. winlike := (def.stringtype=st_widestring) and (tf_winlikewidestring in target_info.flags);
  476. ll := emit_unicodestring_const(current_asmdata.asmlists[al_const],
  477. strval,
  478. winlike);
  479. { Collect Windows widestrings that need initialization at startup.
  480. Local initialized vars are excluded because they are initialized
  481. at function entry instead. }
  482. if winlike and
  483. ((tcsym.owner.symtablelevel <= main_program_level) or
  484. (current_old_block_type=bt_const)) then
  485. begin
  486. current_asmdata.WideInits.Concat(
  487. TTCInitItem.Create(tcsym, curoffset, ll)
  488. );
  489. ll := nil;
  490. Include(tcsym.varoptions, vo_force_finalize);
  491. end;
  492. end;
  493. list.concat(Tai_const.Create_sym(ll));
  494. end;
  495. else
  496. internalerror(200107081);
  497. end;
  498. end;
  499. end;
  500. procedure tasmlisttypedconstbuilder.tc_emit_orddef(def: torddef; var node: tnode);
  501. var
  502. intvalue: tconstexprint;
  503. procedure do_error;
  504. begin
  505. if is_constnode(node) then
  506. IncompatibleTypes(node.resultdef, def)
  507. else
  508. Message(parser_e_illegal_expression);
  509. end;
  510. begin
  511. case def.ordtype of
  512. pasbool8,
  513. bool8bit :
  514. begin
  515. if is_constboolnode(node) then
  516. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)))
  517. else
  518. do_error;
  519. end;
  520. pasbool16,
  521. bool16bit :
  522. begin
  523. if is_constboolnode(node) then
  524. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)))
  525. else
  526. do_error;
  527. end;
  528. pasbool32,
  529. bool32bit :
  530. begin
  531. if is_constboolnode(node) then
  532. list.concat(Tai_const.Create_32bit(longint(tordconstnode(node).value.svalue)))
  533. else
  534. do_error;
  535. end;
  536. pasbool64,
  537. bool64bit :
  538. begin
  539. if is_constboolnode(node) then
  540. list.concat(Tai_const.Create_64bit(int64(tordconstnode(node).value.svalue)))
  541. else
  542. do_error;
  543. end;
  544. uchar :
  545. begin
  546. if is_constcharnode(node) or
  547. ((m_delphi in current_settings.modeswitches) and
  548. is_constwidecharnode(node) and
  549. (tordconstnode(node).value <= 255)) then
  550. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)))
  551. else
  552. do_error;
  553. end;
  554. uwidechar :
  555. begin
  556. if is_constcharnode(node) then
  557. inserttypeconv(node,cwidechartype);
  558. if is_constwidecharnode(node) then
  559. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)))
  560. else
  561. do_error;
  562. end;
  563. s8bit,u8bit,
  564. u16bit,s16bit,
  565. s32bit,u32bit,
  566. s64bit,u64bit :
  567. begin
  568. if is_constintnode(node) then
  569. begin
  570. testrange(def,tordconstnode(node).value,false,false);
  571. case def.size of
  572. 1 :
  573. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)));
  574. 2 :
  575. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)));
  576. 4 :
  577. list.concat(Tai_const.Create_32bit(longint(tordconstnode(node).value.svalue)));
  578. 8 :
  579. list.concat(Tai_const.Create_64bit(tordconstnode(node).value.svalue));
  580. end;
  581. end
  582. else
  583. do_error;
  584. end;
  585. scurrency:
  586. begin
  587. if is_constintnode(node) then
  588. intvalue := tordconstnode(node).value
  589. { allow bootstrapping }
  590. else if is_constrealnode(node) then
  591. intvalue:=PInt64(@trealconstnode(node).value_currency)^
  592. else
  593. begin
  594. intvalue:=0;
  595. IncompatibleTypes(node.resultdef, def);
  596. end;
  597. list.concat(Tai_const.Create_64bit(intvalue));
  598. end;
  599. else
  600. internalerror(200611052);
  601. end;
  602. end;
  603. procedure tasmlisttypedconstbuilder.tc_emit_floatdef(def: tfloatdef; var node: tnode);
  604. var
  605. value : bestreal;
  606. begin
  607. if is_constrealnode(node) then
  608. value:=trealconstnode(node).value_real
  609. else if is_constintnode(node) then
  610. value:=tordconstnode(node).value
  611. else if is_constnode(node) then
  612. IncompatibleTypes(node.resultdef, def)
  613. else
  614. Message(parser_e_illegal_expression);
  615. case def.floattype of
  616. s32real :
  617. list.concat(Tai_real_32bit.Create(ts32real(value)));
  618. s64real :
  619. {$ifdef ARM}
  620. if is_double_hilo_swapped then
  621. list.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  622. else
  623. {$endif ARM}
  624. list.concat(Tai_real_64bit.Create(ts64real(value)));
  625. s80real :
  626. list.concat(Tai_real_80bit.Create(value,s80floattype.size));
  627. sc80real :
  628. list.concat(Tai_real_80bit.Create(value,sc80floattype.size));
  629. s64comp :
  630. { the round is necessary for native compilers where comp isn't a float }
  631. list.concat(Tai_comp_64bit.Create(round(value)));
  632. s64currency:
  633. list.concat(Tai_comp_64bit.Create(round(value*10000)));
  634. s128real:
  635. list.concat(Tai_real_128bit.Create(value));
  636. else
  637. internalerror(200611053);
  638. end;
  639. end;
  640. procedure tasmlisttypedconstbuilder.tc_emit_classrefdef(def: tclassrefdef; var node: tnode);
  641. begin
  642. case node.nodetype of
  643. loadvmtaddrn:
  644. begin
  645. if not Tobjectdef(tclassrefdef(node.resultdef).pointeddef).is_related(tobjectdef(def.pointeddef)) then
  646. IncompatibleTypes(node.resultdef, def);
  647. list.concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol(Tobjectdef(tclassrefdef(node.resultdef).pointeddef).vmt_mangledname)));
  648. end;
  649. niln:
  650. list.concat(Tai_const.Create_sym(nil));
  651. else if is_constnode(node) then
  652. IncompatibleTypes(node.resultdef, def)
  653. else
  654. Message(parser_e_illegal_expression);
  655. end;
  656. end;
  657. procedure tasmlisttypedconstbuilder.tc_emit_pointerdef(def: tpointerdef; var node: tnode);
  658. var
  659. hp : tnode;
  660. srsym : tsym;
  661. pd : tprocdef;
  662. ca : pchar;
  663. pw : pcompilerwidestring;
  664. i,len : longint;
  665. base,
  666. offset : aint;
  667. v : Tconstexprint;
  668. ll : tasmlabel;
  669. varalign : shortint;
  670. begin
  671. { remove equal typecasts for pointer/nil addresses }
  672. if (node.nodetype=typeconvn) then
  673. with Ttypeconvnode(node) do
  674. if (left.nodetype in [addrn,niln]) and equal_defs(def,node.resultdef) then
  675. begin
  676. hp:=left;
  677. left:=nil;
  678. node.free;
  679. node:=hp;
  680. end;
  681. { allows horrible ofs(typeof(TButton)^) code !! }
  682. if (node.nodetype=addrn) then
  683. with Taddrnode(node) do
  684. if left.nodetype=derefn then
  685. begin
  686. hp:=tderefnode(left).left;
  687. tderefnode(left).left:=nil;
  688. node.free;
  689. node:=hp;
  690. end;
  691. { const pointer ? }
  692. if (node.nodetype = pointerconstn) then
  693. begin
  694. {$if sizeof(TConstPtrUInt)=8}
  695. list.concat(Tai_const.Create_64bit(int64(tpointerconstnode(node).value)));
  696. {$else}
  697. {$if sizeof(TConstPtrUInt)=4}
  698. list.concat(Tai_const.Create_32bit(longint(tpointerconstnode(node).value)));
  699. {$else}
  700. internalerror(200404122);
  701. {$endif} {$endif}
  702. end
  703. { nil pointer ? }
  704. else if node.nodetype=niln then
  705. list.concat(Tai_const.Create_sym(nil))
  706. { maybe pchar ? }
  707. else
  708. if is_char(def.pointeddef) and
  709. (node.nodetype<>addrn) then
  710. begin
  711. current_asmdata.getdatalabel(ll);
  712. list.concat(Tai_const.Create_sym(ll));
  713. if node.nodetype=stringconstn then
  714. varalign:=size_2_align(tstringconstnode(node).len)
  715. else
  716. varalign:=0;
  717. varalign:=const_align(varalign);
  718. new_section(current_asmdata.asmlists[al_const], sec_rodata, ll.name, varalign);
  719. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  720. if node.nodetype=stringconstn then
  721. begin
  722. len:=tstringconstnode(node).len;
  723. { For tp7 the maximum lentgh can be 255 }
  724. if (m_tp7 in current_settings.modeswitches) and
  725. (len>255) then
  726. len:=255;
  727. getmem(ca,len+2);
  728. move(tstringconstnode(node).value_str^,ca^,len+1);
  729. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,len+1));
  730. end
  731. else
  732. if is_constcharnode(node) then
  733. current_asmdata.asmlists[al_const].concat(Tai_string.Create(char(byte(tordconstnode(node).value.svalue))+#0))
  734. else
  735. IncompatibleTypes(node.resultdef, def);
  736. end
  737. { maybe pwidechar ? }
  738. else
  739. if is_widechar(def.pointeddef) and
  740. (node.nodetype<>addrn) then
  741. begin
  742. current_asmdata.getdatalabel(ll);
  743. list.concat(Tai_const.Create_sym(ll));
  744. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(sizeof(pint))));
  745. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(ll));
  746. if (node.nodetype in [stringconstn,ordconstn]) then
  747. begin
  748. { convert to widestring stringconstn }
  749. inserttypeconv(node,cwidestringtype);
  750. if (node.nodetype=stringconstn) and
  751. (tstringconstnode(node).cst_type in [cst_widestring,cst_unicodestring]) then
  752. begin
  753. pw:=pcompilerwidestring(tstringconstnode(node).value_str);
  754. for i:=0 to tstringconstnode(node).len-1 do
  755. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(pw^.data[i]));
  756. { ending #0 }
  757. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(0))
  758. end;
  759. end
  760. else
  761. IncompatibleTypes(node.resultdef, def);
  762. end
  763. else
  764. if (node.nodetype=addrn) or
  765. is_proc2procvar_load(node,pd) then
  766. begin
  767. { insert typeconv }
  768. inserttypeconv(node,def);
  769. hp:=node;
  770. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  771. hp:=tunarynode(hp).left;
  772. if (hp.nodetype=loadn) then
  773. begin
  774. hp:=node;
  775. offset:=0;
  776. while assigned(hp) and (hp.nodetype<>loadn) do
  777. begin
  778. case hp.nodetype of
  779. vecn :
  780. begin
  781. case tvecnode(hp).left.resultdef.typ of
  782. stringdef :
  783. begin
  784. { this seems OK for shortstring and ansistrings PM }
  785. { it is wrong for widestrings !! }
  786. len:=1;
  787. base:=0;
  788. end;
  789. arraydef :
  790. begin
  791. if not is_packed_array(tvecnode(hp).left.resultdef) then
  792. begin
  793. len:=tarraydef(tvecnode(hp).left.resultdef).elesize;
  794. base:=tarraydef(tvecnode(hp).left.resultdef).lowrange;
  795. end
  796. else
  797. begin
  798. Message(parser_e_packed_dynamic_open_array);
  799. len:=1;
  800. base:=0;
  801. end;
  802. end
  803. else
  804. Message(parser_e_illegal_expression);
  805. end;
  806. if is_constintnode(tvecnode(hp).right) then
  807. begin
  808. {Prevent overflow.}
  809. v:=get_ordinal_value(tvecnode(hp).right)-base;
  810. if (v<int64(low(offset))) or (v>int64(high(offset))) then
  811. message(parser_e_range_check_error);
  812. if high(offset)-offset div len>v then
  813. inc(offset,len*v.svalue)
  814. else
  815. message(parser_e_range_check_error);
  816. end
  817. else
  818. Message(parser_e_illegal_expression);
  819. end;
  820. subscriptn :
  821. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  822. typeconvn :
  823. begin
  824. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  825. Message(parser_e_illegal_expression);
  826. end;
  827. addrn :
  828. ;
  829. else
  830. Message(parser_e_illegal_expression);
  831. end;
  832. hp:=tunarynode(hp).left;
  833. end;
  834. srsym:=tloadnode(hp).symtableentry;
  835. case srsym.typ of
  836. procsym :
  837. begin
  838. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  839. if Tprocsym(srsym).ProcdefList.Count>1 then
  840. Message(parser_e_no_overloaded_procvars);
  841. if po_abstractmethod in pd.procoptions then
  842. Message(type_e_cant_take_address_of_abstract_method)
  843. else
  844. list.concat(Tai_const.Createname(pd.mangledname,offset));
  845. end;
  846. staticvarsym :
  847. list.concat(Tai_const.Createname(tstaticvarsym(srsym).mangledname,offset));
  848. labelsym :
  849. list.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,offset));
  850. constsym :
  851. if tconstsym(srsym).consttyp=constresourcestring then
  852. list.concat(Tai_const.Createname(make_mangledname('RESSTR',tconstsym(srsym).owner,tconstsym(srsym).name),sizeof(pint)))
  853. else
  854. Message(type_e_variable_id_expected);
  855. else
  856. Message(type_e_variable_id_expected);
  857. end;
  858. end
  859. else
  860. Message(parser_e_illegal_expression);
  861. end
  862. else
  863. { allow typeof(Object type)}
  864. if (node.nodetype=inlinen) and
  865. (tinlinenode(node).inlinenumber=in_typeof_x) then
  866. begin
  867. if (tinlinenode(node).left.nodetype=typen) then
  868. begin
  869. list.concat(Tai_const.createname(
  870. tobjectdef(tinlinenode(node).left.resultdef).vmt_mangledname,0));
  871. end
  872. else
  873. Message(parser_e_illegal_expression);
  874. end
  875. else
  876. Message(parser_e_illegal_expression);
  877. end;
  878. procedure tasmlisttypedconstbuilder.tc_emit_setdef(def: tsetdef; var node: tnode);
  879. type
  880. setbytes = array[0..31] of byte;
  881. Psetbytes = ^setbytes;
  882. var
  883. i: longint;
  884. begin
  885. if node.nodetype=setconstn then
  886. begin
  887. { be sure to convert to the correct result, else
  888. it can generate smallset data instead of normalset (PFV) }
  889. inserttypeconv(node,def);
  890. { we only allow const sets }
  891. if (node.nodetype<>setconstn) or
  892. assigned(tsetconstnode(node).left) then
  893. Message(parser_e_illegal_expression)
  894. else
  895. begin
  896. tsetconstnode(node).adjustforsetbase;
  897. { this writing is endian-dependant }
  898. if source_info.endian = target_info.endian then
  899. begin
  900. for i:=0 to node.resultdef.size-1 do
  901. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(node).value_set)^[i]));
  902. end
  903. else
  904. begin
  905. for i:=0 to node.resultdef.size-1 do
  906. list.concat(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(node).value_set)^[i])));
  907. end;
  908. end;
  909. end
  910. else
  911. Message(parser_e_illegal_expression);
  912. end;
  913. procedure tasmlisttypedconstbuilder.tc_emit_enumdef(def: tenumdef; var node: tnode);
  914. begin
  915. if node.nodetype=ordconstn then
  916. begin
  917. if equal_defs(node.resultdef,def) or
  918. is_subequal(node.resultdef,def) then
  919. begin
  920. case longint(node.resultdef.size) of
  921. 1 : list.concat(Tai_const.Create_8bit(Byte(tordconstnode(node).value.svalue)));
  922. 2 : list.concat(Tai_const.Create_16bit(Word(tordconstnode(node).value.svalue)));
  923. 4 : list.concat(Tai_const.Create_32bit(Longint(tordconstnode(node).value.svalue)));
  924. end;
  925. end
  926. else
  927. IncompatibleTypes(node.resultdef,def);
  928. end
  929. else
  930. Message(parser_e_illegal_expression);
  931. end;
  932. { parse a single constant and add it to the packed const info }
  933. { represented by curval etc (see explanation of bitpackval for }
  934. { what the different parameters mean) }
  935. function tasmlisttypedconstbuilder.parse_single_packed_const(def: tdef; var bp: tbitpackedval): boolean;
  936. var
  937. node: tnode;
  938. begin
  939. result:=true;
  940. node:=comp_expr(true,false);
  941. if (node.nodetype <> ordconstn) or
  942. (not equal_defs(node.resultdef,def) and
  943. not is_subequal(node.resultdef,def)) then
  944. begin
  945. node.free;
  946. incompatibletypes(node.resultdef,def);
  947. consume_all_until(_SEMICOLON);
  948. result:=false;
  949. exit;
  950. end;
  951. if (Tordconstnode(node).value<qword(low(Aword))) or (Tordconstnode(node).value>qword(high(Aword))) then
  952. message(parser_e_range_check_error)
  953. else
  954. bitpackval(Tordconstnode(node).value.uvalue,bp);
  955. if (bp.curbitoffset>=AIntBits) then
  956. flush_packed_value(list,bp);
  957. node.free;
  958. end;
  959. procedure tasmlisttypedconstbuilder.parse_arraydef(def:tarraydef);
  960. var
  961. n : tnode;
  962. i : longint;
  963. len : asizeint;
  964. ch : array[0..1] of char;
  965. ca : pbyte;
  966. int_const: tai_const;
  967. char_size: integer;
  968. oldoffset: asizeint;
  969. begin
  970. { dynamic array nil }
  971. if is_dynamic_array(def) then
  972. begin
  973. { Only allow nil initialization }
  974. consume(_NIL);
  975. list.concat(Tai_const.Create_sym(nil));
  976. end
  977. { packed array constant }
  978. else if is_packed_array(def) and
  979. ((def.elepackedbitsize mod 8 <> 0) or
  980. not ispowerof2(def.elepackedbitsize div 8,i)) then
  981. begin
  982. parse_packed_array_def(def);
  983. end
  984. { normal array const between brackets }
  985. else if try_to_consume(_LKLAMMER) then
  986. begin
  987. oldoffset:=curoffset;
  988. curoffset:=0;
  989. for i:=def.lowrange to def.highrange-1 do
  990. begin
  991. read_typed_const_data(def.elementdef);
  992. Inc(curoffset,def.elementdef.size);
  993. if token=_RKLAMMER then
  994. begin
  995. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  996. consume(_RKLAMMER);
  997. exit;
  998. end
  999. else
  1000. consume(_COMMA);
  1001. end;
  1002. read_typed_const_data(def.elementdef);
  1003. consume(_RKLAMMER);
  1004. curoffset:=oldoffset;
  1005. end
  1006. { if array of char then we allow also a string }
  1007. else if is_anychar(def.elementdef) then
  1008. begin
  1009. char_size:=def.elementdef.size;
  1010. n:=comp_expr(true,false);
  1011. if n.nodetype=stringconstn then
  1012. begin
  1013. len:=tstringconstnode(n).len;
  1014. case char_size of
  1015. 1:
  1016. ca:=pointer(tstringconstnode(n).value_str);
  1017. 2:
  1018. begin
  1019. inserttypeconv(n,cwidestringtype);
  1020. if n.nodetype<>stringconstn then
  1021. internalerror(2010033003);
  1022. ca:=pointer(pcompilerwidestring(tstringconstnode(n).value_str)^.data)
  1023. end;
  1024. else
  1025. internalerror(2010033005);
  1026. end;
  1027. { For tp7 the maximum lentgh can be 255 }
  1028. if (m_tp7 in current_settings.modeswitches) and
  1029. (len>255) then
  1030. len:=255;
  1031. end
  1032. else if is_constcharnode(n) then
  1033. begin
  1034. case char_size of
  1035. 1:
  1036. ch[0]:=chr(tordconstnode(n).value.uvalue and $ff);
  1037. 2:
  1038. begin
  1039. inserttypeconv(n,cwidechartype);
  1040. if not is_constwidecharnode(n) then
  1041. internalerror(2010033001);
  1042. widechar(ch):=widechar(tordconstnode(n).value.uvalue and $ffff);
  1043. end;
  1044. else
  1045. internalerror(2010033002);
  1046. end;
  1047. ca:=@ch;
  1048. len:=1;
  1049. end
  1050. else
  1051. begin
  1052. Message(parser_e_illegal_expression);
  1053. len:=0;
  1054. end;
  1055. if len>(def.highrange-def.lowrange+1) then
  1056. Message(parser_e_string_larger_array);
  1057. for i:=0 to def.highrange-def.lowrange do
  1058. begin
  1059. if i<len then
  1060. begin
  1061. case char_size of
  1062. 1:
  1063. int_const:=Tai_const.Create_char(char_size,pbyte(ca)^);
  1064. 2:
  1065. int_const:=Tai_const.Create_char(char_size,pword(ca)^);
  1066. else
  1067. internalerror(2010033004);
  1068. end;
  1069. inc(ca, char_size);
  1070. end
  1071. else
  1072. {Fill the remaining positions with #0.}
  1073. int_const:=Tai_const.Create_char(char_size,0);
  1074. list.concat(int_const)
  1075. end;
  1076. n.free;
  1077. end
  1078. else
  1079. begin
  1080. { we want the ( }
  1081. consume(_LKLAMMER);
  1082. end;
  1083. end;
  1084. procedure tasmlisttypedconstbuilder.parse_procvardef(def:tprocvardef);
  1085. var
  1086. tmpn,n : tnode;
  1087. pd : tprocdef;
  1088. begin
  1089. { Procvars and pointers are no longer compatible. }
  1090. { under tp: =nil or =var under fpc: =nil or =@var }
  1091. if try_to_consume(_NIL) then
  1092. begin
  1093. list.concat(Tai_const.Create_sym(nil));
  1094. if not def.is_addressonly then
  1095. list.concat(Tai_const.Create_sym(nil));
  1096. exit;
  1097. end;
  1098. { you can't assign a value other than NIL to a typed constant }
  1099. { which is a "procedure of object", because this also requires }
  1100. { address of an object/class instance, which is not known at }
  1101. { compile time (JM) }
  1102. if (po_methodpointer in def.procoptions) then
  1103. Message(parser_e_no_procvarobj_const);
  1104. { parse the rest too, so we can continue with error checking }
  1105. getprocvardef:=def;
  1106. n:=comp_expr(true,false);
  1107. getprocvardef:=nil;
  1108. if codegenerror then
  1109. begin
  1110. n.free;
  1111. exit;
  1112. end;
  1113. { let type conversion check everything needed }
  1114. inserttypeconv(n,def);
  1115. if codegenerror then
  1116. begin
  1117. n.free;
  1118. exit;
  1119. end;
  1120. { remove typeconvs, that will normally insert a lea
  1121. instruction which is not necessary for us }
  1122. while n.nodetype=typeconvn do
  1123. begin
  1124. tmpn:=ttypeconvnode(n).left;
  1125. ttypeconvnode(n).left:=nil;
  1126. n.free;
  1127. n:=tmpn;
  1128. end;
  1129. { remove addrn which we also don't need here }
  1130. if n.nodetype=addrn then
  1131. begin
  1132. tmpn:=taddrnode(n).left;
  1133. taddrnode(n).left:=nil;
  1134. n.free;
  1135. n:=tmpn;
  1136. end;
  1137. { we now need to have a loadn with a procsym }
  1138. if (n.nodetype=loadn) and
  1139. (tloadnode(n).symtableentry.typ=procsym) then
  1140. begin
  1141. pd:=tloadnode(n).procdef;
  1142. list.concat(Tai_const.createname(pd.mangledname,0));
  1143. { nested procvar typed consts can only be initialised with nil
  1144. (checked above) or with a global procedure (checked here),
  1145. because in other cases we need a valid frame pointer }
  1146. if is_nested_pd(def) then
  1147. begin
  1148. if is_nested_pd(pd) then
  1149. Message(parser_e_no_procvarnested_const);
  1150. list.concat(Tai_const.Create_sym(nil));
  1151. end
  1152. end
  1153. else
  1154. Message(parser_e_illegal_expression);
  1155. n.free;
  1156. end;
  1157. procedure tasmlisttypedconstbuilder.parse_recorddef(def:trecorddef);
  1158. var
  1159. n : tnode;
  1160. symidx : longint;
  1161. recsym,
  1162. srsym : tsym;
  1163. hs : string;
  1164. sorg,s : TIDString;
  1165. tmpguid : tguid;
  1166. recoffset,
  1167. fillbytes : aint;
  1168. bp : tbitpackedval;
  1169. error,
  1170. is_packed: boolean;
  1171. startoffset: aint;
  1172. procedure handle_stringconstn;
  1173. var
  1174. i : longint;
  1175. begin
  1176. hs:=strpas(tstringconstnode(n).value_str);
  1177. if string2guid(hs,tmpguid) then
  1178. begin
  1179. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  1180. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  1181. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  1182. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1183. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  1184. end
  1185. else
  1186. Message(parser_e_improper_guid_syntax);
  1187. end;
  1188. var
  1189. i : longint;
  1190. begin
  1191. { GUID }
  1192. if (def=rec_tguid) and (token=_ID) then
  1193. begin
  1194. n:=comp_expr(true,false);
  1195. if n.nodetype=stringconstn then
  1196. handle_stringconstn
  1197. else
  1198. begin
  1199. inserttypeconv(n,rec_tguid);
  1200. if n.nodetype=guidconstn then
  1201. begin
  1202. tmpguid:=tguidconstnode(n).value;
  1203. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  1204. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  1205. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  1206. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1207. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  1208. end
  1209. else
  1210. Message(parser_e_illegal_expression);
  1211. end;
  1212. n.free;
  1213. exit;
  1214. end;
  1215. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1216. begin
  1217. n:=comp_expr(true,false);
  1218. inserttypeconv(n,cshortstringtype);
  1219. if n.nodetype=stringconstn then
  1220. handle_stringconstn
  1221. else
  1222. Message(parser_e_illegal_expression);
  1223. n.free;
  1224. exit;
  1225. end;
  1226. { bitpacked record? }
  1227. is_packed:=is_packed_record_or_object(def);
  1228. if (is_packed) then
  1229. begin
  1230. { loadbitsize = 8, bitpacked records are always padded to }
  1231. { a multiple of a byte. packedbitsize will be set separately }
  1232. { for each field }
  1233. initbitpackval(bp,0);
  1234. bp.loadbitsize:=8;
  1235. end;
  1236. { normal record }
  1237. consume(_LKLAMMER);
  1238. recoffset:=0;
  1239. symidx:=0;
  1240. sorg:='';
  1241. srsym:=tsym(def.symtable.SymList[symidx]);
  1242. recsym := nil;
  1243. startoffset:=curoffset;
  1244. while token<>_RKLAMMER do
  1245. begin
  1246. s:=pattern;
  1247. sorg:=orgpattern;
  1248. consume(_ID);
  1249. consume(_COLON);
  1250. error := false;
  1251. recsym := tsym(def.symtable.Find(s));
  1252. if not assigned(recsym) then
  1253. begin
  1254. Message1(sym_e_illegal_field,sorg);
  1255. error := true;
  1256. end;
  1257. if (not error) and
  1258. (not assigned(srsym) or
  1259. (s <> srsym.name)) then
  1260. { possible variant record (JM) }
  1261. begin
  1262. { All parts of a variant start at the same offset }
  1263. { Also allow jumping from one variant part to another, }
  1264. { as long as the offsets match }
  1265. if (assigned(srsym) and
  1266. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1267. { srsym is not assigned after parsing w2 in the }
  1268. { typed const in the next example: }
  1269. { type tr = record case byte of }
  1270. { 1: (l1,l2: dword); }
  1271. { 2: (w1,w2: word); }
  1272. { end; }
  1273. { const r: tr = (w1:1;w2:1;l2:5); }
  1274. (tfieldvarsym(recsym).fieldoffset = recoffset) then
  1275. begin
  1276. srsym := recsym;
  1277. symidx := def.symtable.SymList.indexof(srsym)
  1278. end
  1279. { going backwards isn't allowed in any mode }
  1280. else if (tfieldvarsym(recsym).fieldoffset<recoffset) then
  1281. begin
  1282. Message(parser_e_invalid_record_const);
  1283. error := true;
  1284. end
  1285. { Delphi allows you to skip fields }
  1286. else if (m_delphi in current_settings.modeswitches) then
  1287. begin
  1288. Message1(parser_w_skipped_fields_before,sorg);
  1289. srsym := recsym;
  1290. end
  1291. { FPC and TP don't }
  1292. else
  1293. begin
  1294. Message1(parser_e_skipped_fields_before,sorg);
  1295. error := true;
  1296. end;
  1297. end;
  1298. if error then
  1299. consume_all_until(_SEMICOLON)
  1300. else
  1301. begin
  1302. { if needed fill (alignment) }
  1303. if tfieldvarsym(srsym).fieldoffset>recoffset then
  1304. begin
  1305. if not(is_packed) then
  1306. fillbytes:=tfieldvarsym(srsym).fieldoffset-recoffset
  1307. else
  1308. begin
  1309. flush_packed_value(list,bp);
  1310. { curoffset is now aligned to the next byte }
  1311. recoffset:=align(recoffset,8);
  1312. { offsets are in bits in this case }
  1313. fillbytes:=(tfieldvarsym(srsym).fieldoffset-recoffset) div 8;
  1314. end;
  1315. for i:=1 to fillbytes do
  1316. list.concat(Tai_const.Create_8bit(0))
  1317. end;
  1318. { new position }
  1319. recoffset:=tfieldvarsym(srsym).fieldoffset;
  1320. if not(is_packed) then
  1321. inc(recoffset,tfieldvarsym(srsym).vardef.size)
  1322. else
  1323. inc(recoffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1324. { read the data }
  1325. if not(is_packed) or
  1326. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1327. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1328. begin
  1329. if is_packed then
  1330. begin
  1331. flush_packed_value(list,bp);
  1332. recoffset:=align(recoffset,8);
  1333. end;
  1334. curoffset:=startoffset+tfieldvarsym(srsym).fieldoffset;
  1335. read_typed_const_data(tfieldvarsym(srsym).vardef);
  1336. end
  1337. else
  1338. begin
  1339. bp.packedbitsize:=tfieldvarsym(srsym).vardef.packedbitsize;
  1340. parse_single_packed_const(tfieldvarsym(srsym).vardef,bp);
  1341. end;
  1342. { keep previous field for checking whether whole }
  1343. { record was initialized (JM) }
  1344. recsym := srsym;
  1345. { goto next field }
  1346. inc(symidx);
  1347. if symidx<def.symtable.SymList.Count then
  1348. srsym:=tsym(def.symtable.SymList[symidx])
  1349. else
  1350. srsym:=nil;
  1351. if token=_SEMICOLON then
  1352. consume(_SEMICOLON)
  1353. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1354. consume(_COMMA)
  1355. else
  1356. break;
  1357. end;
  1358. end;
  1359. curoffset:=startoffset;
  1360. { are there any fields left, but don't complain if there only
  1361. come other variant parts after the last initialized field }
  1362. if assigned(srsym) and
  1363. (
  1364. (recsym=nil) or
  1365. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1366. ) then
  1367. Message1(parser_w_skipped_fields_after,sorg);
  1368. if not(is_packed) then
  1369. fillbytes:=def.size-recoffset
  1370. else
  1371. begin
  1372. flush_packed_value(list,bp);
  1373. recoffset:=align(recoffset,8);
  1374. fillbytes:=def.size-(recoffset div 8);
  1375. end;
  1376. for i:=1 to fillbytes do
  1377. list.concat(Tai_const.Create_8bit(0));
  1378. consume(_RKLAMMER);
  1379. end;
  1380. procedure tasmlisttypedconstbuilder.parse_objectdef(def:tobjectdef);
  1381. var
  1382. n : tnode;
  1383. i : longint;
  1384. obj : tobjectdef;
  1385. srsym : tsym;
  1386. st : tsymtable;
  1387. objoffset : aint;
  1388. s,sorg : TIDString;
  1389. vmtwritten : boolean;
  1390. startoffset:aint;
  1391. begin
  1392. { no support for packed object }
  1393. if is_packed_record_or_object(def) then
  1394. begin
  1395. Message(type_e_no_const_packed_record);
  1396. exit;
  1397. end;
  1398. { only allow nil for implicit pointer object types }
  1399. if is_implicit_pointer_object_type(def) then
  1400. begin
  1401. n:=comp_expr(true,false);
  1402. if n.nodetype<>niln then
  1403. begin
  1404. Message(parser_e_type_const_not_possible);
  1405. consume_all_until(_SEMICOLON);
  1406. end
  1407. else
  1408. list.concat(Tai_const.Create_sym(nil));
  1409. n.free;
  1410. exit;
  1411. end;
  1412. { for objects we allow it only if it doesn't contain a vmt }
  1413. if (oo_has_vmt in def.objectoptions) and
  1414. (m_fpc in current_settings.modeswitches) then
  1415. begin
  1416. Message(parser_e_type_object_constants);
  1417. exit;
  1418. end;
  1419. consume(_LKLAMMER);
  1420. startoffset:=curoffset;
  1421. objoffset:=0;
  1422. vmtwritten:=false;
  1423. while token<>_RKLAMMER do
  1424. begin
  1425. s:=pattern;
  1426. sorg:=orgpattern;
  1427. consume(_ID);
  1428. consume(_COLON);
  1429. srsym:=nil;
  1430. obj:=tobjectdef(def);
  1431. st:=obj.symtable;
  1432. while (srsym=nil) and assigned(st) do
  1433. begin
  1434. srsym:=tsym(st.Find(s));
  1435. if assigned(obj) then
  1436. obj:=obj.childof;
  1437. if assigned(obj) then
  1438. st:=obj.symtable
  1439. else
  1440. st:=nil;
  1441. end;
  1442. if (srsym=nil) or
  1443. (srsym.typ<>fieldvarsym) then
  1444. begin
  1445. if (srsym=nil) then
  1446. Message1(sym_e_id_not_found,sorg)
  1447. else
  1448. Message1(sym_e_illegal_field,sorg);
  1449. consume_all_until(_RKLAMMER);
  1450. break;
  1451. end
  1452. else
  1453. with tfieldvarsym(srsym) do
  1454. begin
  1455. { check position }
  1456. if fieldoffset<objoffset then
  1457. message(parser_e_invalid_record_const);
  1458. { check in VMT needs to be added for TP mode }
  1459. if not(vmtwritten) and
  1460. not(m_fpc in current_settings.modeswitches) and
  1461. (oo_has_vmt in def.objectoptions) and
  1462. (def.vmt_offset<fieldoffset) then
  1463. begin
  1464. for i:=1 to def.vmt_offset-objoffset do
  1465. list.concat(tai_const.create_8bit(0));
  1466. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1467. { this is more general }
  1468. objoffset:=def.vmt_offset + sizeof(pint);
  1469. vmtwritten:=true;
  1470. end;
  1471. { if needed fill }
  1472. if fieldoffset>objoffset then
  1473. for i:=1 to fieldoffset-objoffset do
  1474. list.concat(Tai_const.Create_8bit(0));
  1475. { new position }
  1476. objoffset:=fieldoffset+vardef.size;
  1477. { read the data }
  1478. curoffset:=startoffset+fieldoffset;
  1479. read_typed_const_data(vardef);
  1480. if not try_to_consume(_SEMICOLON) then
  1481. break;
  1482. end;
  1483. end;
  1484. curoffset:=startoffset;
  1485. if not(m_fpc in current_settings.modeswitches) and
  1486. (oo_has_vmt in def.objectoptions) and
  1487. (def.vmt_offset>=objoffset) then
  1488. begin
  1489. for i:=1 to def.vmt_offset-objoffset do
  1490. list.concat(tai_const.create_8bit(0));
  1491. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1492. { this is more general }
  1493. objoffset:=def.vmt_offset + sizeof(pint);
  1494. end;
  1495. for i:=1 to def.size-objoffset do
  1496. list.concat(Tai_const.Create_8bit(0));
  1497. consume(_RKLAMMER);
  1498. end;
  1499. function tasmlisttypedconstbuilder.parse_into_asmlist: tasmlist;
  1500. begin
  1501. read_typed_const_data(tcsym.vardef);
  1502. result:=list;
  1503. end;
  1504. { tnodetreetypedconstbuilder }
  1505. procedure tnodetreetypedconstbuilder.parse_arraydef(def: tarraydef);
  1506. var
  1507. n : tnode;
  1508. i : longint;
  1509. orgbase: tnode;
  1510. begin
  1511. { dynamic array nil }
  1512. if is_dynamic_array(def) then
  1513. begin
  1514. { Only allow nil initialization }
  1515. consume(_NIL);
  1516. addstatement(statmnt,cassignmentnode.create_internal(basenode,cnilnode.create));
  1517. basenode:=nil;
  1518. end
  1519. { array const between brackets }
  1520. else if try_to_consume(_LKLAMMER) then
  1521. begin
  1522. orgbase:=basenode;
  1523. for i:=def.lowrange to def.highrange-1 do
  1524. begin
  1525. basenode:=cvecnode.create(orgbase.getcopy,ctypeconvnode.create_explicit(genintconstnode(i),tarraydef(def).rangedef));
  1526. read_typed_const_data(def.elementdef);
  1527. if token=_RKLAMMER then
  1528. begin
  1529. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  1530. consume(_RKLAMMER);
  1531. exit;
  1532. end
  1533. else
  1534. consume(_COMMA);
  1535. end;
  1536. basenode:=cvecnode.create(orgbase,ctypeconvnode.create_explicit(genintconstnode(def.highrange),tarraydef(def).rangedef));
  1537. read_typed_const_data(def.elementdef);
  1538. consume(_RKLAMMER);
  1539. end
  1540. { if array of char then we allow also a string }
  1541. else if is_anychar(def.elementdef) then
  1542. begin
  1543. n:=comp_expr(true,false);
  1544. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1545. basenode:=nil;
  1546. end
  1547. else
  1548. begin
  1549. { we want the ( }
  1550. consume(_LKLAMMER);
  1551. end;
  1552. end;
  1553. procedure tnodetreetypedconstbuilder.parse_procvardef(def: tprocvardef);
  1554. begin
  1555. addstatement(statmnt,cassignmentnode.create_internal(basenode,comp_expr(true,false)));
  1556. basenode:=nil;
  1557. end;
  1558. procedure tnodetreetypedconstbuilder.parse_recorddef(def: trecorddef);
  1559. var
  1560. n,n2 : tnode;
  1561. orgbasenode : tnode;
  1562. symidx : longint;
  1563. recsym,
  1564. srsym : tsym;
  1565. sorg,s : TIDString;
  1566. recoffset : aint;
  1567. error,
  1568. is_packed: boolean;
  1569. procedure handle_stringconstn;
  1570. begin
  1571. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1572. basenode:=nil;
  1573. n:=nil;
  1574. end;
  1575. begin
  1576. { GUID }
  1577. if (def=rec_tguid) and (token=_ID) then
  1578. begin
  1579. n:=comp_expr(true,false);
  1580. if n.nodetype=stringconstn then
  1581. handle_stringconstn
  1582. else
  1583. begin
  1584. inserttypeconv(n,rec_tguid);
  1585. if n.nodetype=guidconstn then
  1586. begin
  1587. n2:=cstringconstnode.createstr(guid2string(tguidconstnode(n).value));
  1588. n.free;
  1589. n:=n2;
  1590. handle_stringconstn;
  1591. end
  1592. else
  1593. Message(parser_e_illegal_expression);
  1594. end;
  1595. n.free;
  1596. exit;
  1597. end;
  1598. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1599. begin
  1600. n:=comp_expr(true,false);
  1601. inserttypeconv(n,cshortstringtype);
  1602. if n.nodetype=stringconstn then
  1603. handle_stringconstn
  1604. else
  1605. Message(parser_e_illegal_expression);
  1606. n.free;
  1607. exit;
  1608. end;
  1609. { bitpacked record? }
  1610. is_packed:=is_packed_record_or_object(def);
  1611. { normal record }
  1612. consume(_LKLAMMER);
  1613. recoffset:=0;
  1614. symidx:=0;
  1615. sorg:='';
  1616. srsym:=tsym(def.symtable.SymList[symidx]);
  1617. recsym := nil;
  1618. orgbasenode:=basenode;
  1619. basenode:=nil;
  1620. while token<>_RKLAMMER do
  1621. begin
  1622. s:=pattern;
  1623. sorg:=orgpattern;
  1624. consume(_ID);
  1625. consume(_COLON);
  1626. error := false;
  1627. recsym := tsym(def.symtable.Find(s));
  1628. if not assigned(recsym) then
  1629. begin
  1630. Message1(sym_e_illegal_field,sorg);
  1631. error := true;
  1632. end;
  1633. if (not error) and
  1634. (not assigned(srsym) or
  1635. (s <> srsym.name)) then
  1636. { possible variant record (JM) }
  1637. begin
  1638. { All parts of a variant start at the same offset }
  1639. { Also allow jumping from one variant part to another, }
  1640. { as long as the offsets match }
  1641. if (assigned(srsym) and
  1642. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1643. { srsym is not assigned after parsing w2 in the }
  1644. { typed const in the next example: }
  1645. { type tr = record case byte of }
  1646. { 1: (l1,l2: dword); }
  1647. { 2: (w1,w2: word); }
  1648. { end; }
  1649. { const r: tr = (w1:1;w2:1;l2:5); }
  1650. (tfieldvarsym(recsym).fieldoffset = recoffset) then
  1651. begin
  1652. srsym := recsym;
  1653. symidx := def.symtable.SymList.indexof(srsym)
  1654. end
  1655. { going backwards isn't allowed in any mode }
  1656. else if (tfieldvarsym(recsym).fieldoffset<recoffset) then
  1657. begin
  1658. Message(parser_e_invalid_record_const);
  1659. error := true;
  1660. end
  1661. { Delphi allows you to skip fields }
  1662. else if (m_delphi in current_settings.modeswitches) then
  1663. begin
  1664. Message1(parser_w_skipped_fields_before,sorg);
  1665. srsym := recsym;
  1666. end
  1667. { FPC and TP don't }
  1668. else
  1669. begin
  1670. Message1(parser_e_skipped_fields_before,sorg);
  1671. error := true;
  1672. end;
  1673. end;
  1674. if error then
  1675. consume_all_until(_SEMICOLON)
  1676. else
  1677. begin
  1678. { skipping fill bytes happens automatically, since we only
  1679. initialize the defined fields }
  1680. { new position }
  1681. recoffset:=tfieldvarsym(srsym).fieldoffset;
  1682. if not(is_packed) then
  1683. inc(recoffset,tfieldvarsym(srsym).vardef.size)
  1684. else
  1685. inc(recoffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1686. { read the data }
  1687. if is_packed and
  1688. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1689. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1690. recoffset:=align(recoffset,8);
  1691. basenode:=csubscriptnode.create(srsym,orgbasenode.getcopy);
  1692. read_typed_const_data(tfieldvarsym(srsym).vardef);
  1693. { keep previous field for checking whether whole }
  1694. { record was initialized (JM) }
  1695. recsym := srsym;
  1696. { goto next field }
  1697. repeat
  1698. inc(symidx);
  1699. if symidx<def.symtable.SymList.Count then
  1700. srsym:=tsym(def.symtable.SymList[symidx])
  1701. else
  1702. srsym:=nil;
  1703. until (srsym=nil) or
  1704. (srsym.typ=fieldvarsym);
  1705. if token=_SEMICOLON then
  1706. consume(_SEMICOLON)
  1707. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1708. consume(_COMMA)
  1709. else
  1710. break;
  1711. end;
  1712. end;
  1713. { are there any fields left, but don't complain if there only
  1714. come other variant parts after the last initialized field }
  1715. if assigned(srsym) and
  1716. (
  1717. (recsym=nil) or
  1718. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1719. ) then
  1720. Message1(parser_w_skipped_fields_after,sorg);
  1721. orgbasenode.free;
  1722. basenode:=nil;
  1723. consume(_RKLAMMER);
  1724. end;
  1725. procedure tnodetreetypedconstbuilder.parse_objectdef(def: tobjectdef);
  1726. var
  1727. n,
  1728. orgbasenode : tnode;
  1729. obj : tobjectdef;
  1730. srsym : tsym;
  1731. st : tsymtable;
  1732. objoffset : aint;
  1733. s,sorg : TIDString;
  1734. begin
  1735. { no support for packed object }
  1736. if is_packed_record_or_object(def) then
  1737. begin
  1738. Message(type_e_no_const_packed_record);
  1739. exit;
  1740. end;
  1741. { only allow nil for implicit pointer object types }
  1742. if is_implicit_pointer_object_type(def) then
  1743. begin
  1744. n:=comp_expr(true,false);
  1745. if n.nodetype<>niln then
  1746. begin
  1747. Message(parser_e_type_const_not_possible);
  1748. consume_all_until(_SEMICOLON);
  1749. end
  1750. else
  1751. begin
  1752. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1753. n:=nil;
  1754. basenode:=nil;
  1755. end;
  1756. n.free;
  1757. exit;
  1758. end;
  1759. { for objects we allow it only if it doesn't contain a vmt }
  1760. if (oo_has_vmt in def.objectoptions) and
  1761. (m_fpc in current_settings.modeswitches) then
  1762. begin
  1763. Message(parser_e_type_object_constants);
  1764. exit;
  1765. end;
  1766. consume(_LKLAMMER);
  1767. objoffset:=0;
  1768. orgbasenode:=basenode;
  1769. basenode:=nil;
  1770. while token<>_RKLAMMER do
  1771. begin
  1772. s:=pattern;
  1773. sorg:=orgpattern;
  1774. consume(_ID);
  1775. consume(_COLON);
  1776. srsym:=nil;
  1777. obj:=tobjectdef(def);
  1778. st:=obj.symtable;
  1779. while (srsym=nil) and assigned(st) do
  1780. begin
  1781. srsym:=tsym(st.Find(s));
  1782. if assigned(obj) then
  1783. obj:=obj.childof;
  1784. if assigned(obj) then
  1785. st:=obj.symtable
  1786. else
  1787. st:=nil;
  1788. end;
  1789. if (srsym=nil) or
  1790. (srsym.typ<>fieldvarsym) then
  1791. begin
  1792. if (srsym=nil) then
  1793. Message1(sym_e_id_not_found,sorg)
  1794. else
  1795. Message1(sym_e_illegal_field,sorg);
  1796. consume_all_until(_RKLAMMER);
  1797. break;
  1798. end
  1799. else
  1800. with tfieldvarsym(srsym) do
  1801. begin
  1802. { check position }
  1803. if fieldoffset<objoffset then
  1804. message(parser_e_invalid_record_const);
  1805. { new position }
  1806. objoffset:=fieldoffset+vardef.size;
  1807. { read the data }
  1808. basenode:=csubscriptnode.create(srsym,orgbasenode.getcopy);
  1809. read_typed_const_data(vardef);
  1810. if not try_to_consume(_SEMICOLON) then
  1811. break;
  1812. end;
  1813. end;
  1814. consume(_RKLAMMER);
  1815. end;
  1816. procedure tnodetreetypedconstbuilder.tc_emit_orddef(def: torddef; var node: tnode);
  1817. begin
  1818. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1819. basenode:=nil;
  1820. node:=nil;
  1821. end;
  1822. procedure tnodetreetypedconstbuilder.tc_emit_floatdef(def: tfloatdef; var node: tnode);
  1823. begin
  1824. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1825. basenode:=nil;
  1826. node:=nil;
  1827. end;
  1828. procedure tnodetreetypedconstbuilder.tc_emit_classrefdef(def: tclassrefdef; var node: tnode);
  1829. begin
  1830. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1831. basenode:=nil;
  1832. node:=nil;
  1833. end;
  1834. procedure tnodetreetypedconstbuilder.tc_emit_pointerdef(def: tpointerdef; var node: tnode);
  1835. begin
  1836. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1837. basenode:=nil;
  1838. node:=nil;
  1839. end;
  1840. procedure tnodetreetypedconstbuilder.tc_emit_setdef(def: tsetdef; var node: tnode);
  1841. begin
  1842. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1843. basenode:=nil;
  1844. node:=nil;
  1845. end;
  1846. procedure tnodetreetypedconstbuilder.tc_emit_enumdef(def: tenumdef; var node: tnode);
  1847. begin
  1848. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1849. basenode:=nil;
  1850. node:=nil;
  1851. end;
  1852. procedure tnodetreetypedconstbuilder.tc_emit_stringdef(def: tstringdef; var node: tnode);
  1853. begin
  1854. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1855. basenode:=nil;
  1856. node:=nil;
  1857. end;
  1858. constructor tnodetreetypedconstbuilder.create(sym: tstaticvarsym; previnit: tnode);
  1859. begin
  1860. inherited create(sym);
  1861. basenode:=cloadnode.create(sym,sym.owner);
  1862. resultblock:=internalstatements(statmnt);
  1863. if assigned(previnit) then
  1864. addstatement(statmnt,previnit);
  1865. end;
  1866. destructor tnodetreetypedconstbuilder.destroy;
  1867. begin
  1868. freeandnil(basenode);
  1869. freeandnil(resultblock);
  1870. inherited destroy;
  1871. end;
  1872. function tnodetreetypedconstbuilder.parse_into_nodetree: tnode;
  1873. begin
  1874. read_typed_const_data(tcsym.vardef);
  1875. result:=self.resultblock;
  1876. self.resultblock:=nil;
  1877. end;
  1878. begin
  1879. { default to asmlist version, best for most targets }
  1880. ctypedconstbuilder:=tasmlisttypedconstbuilder;
  1881. end.