ngtcon.pas 76 KB

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