ngtcon.pp 72 KB

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