ngtcon.pas 76 KB

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