ngtcon.pas 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl, Jonas Maebe
  3. Generates code/nodes for typed constant declarations
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ngtcon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. aasmdata,
  23. node,nbas,
  24. symtype, symbase, symdef,symsym;
  25. type
  26. ttypedconstbuilder = class
  27. protected
  28. current_old_block_type : tblock_type;
  29. tcsym: tstaticvarsym;
  30. { this procedure reads typed constants }
  31. procedure read_typed_const_data(def:tdef);
  32. procedure parse_orddef(def: torddef);
  33. procedure parse_floatdef(def: tfloatdef);
  34. procedure parse_classrefdef(def: tclassrefdef);
  35. procedure parse_pointerdef(def: tpointerdef);
  36. procedure parse_setdef(def: tsetdef);
  37. procedure parse_enumdef(def: tenumdef);
  38. procedure parse_stringdef(def: tstringdef);
  39. procedure parse_arraydef(def:tarraydef);virtual;abstract;
  40. procedure parse_procvardef(def:tprocvardef);virtual;abstract;
  41. procedure parse_recorddef(def:trecorddef);virtual;abstract;
  42. procedure parse_objectdef(def:tobjectdef);virtual;abstract;
  43. procedure tc_emit_orddef(def: torddef; var node: tnode);virtual;abstract;
  44. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode);virtual;abstract;
  45. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);virtual;abstract;
  46. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);virtual;abstract;
  47. procedure tc_emit_setdef(def: tsetdef; var node: tnode);virtual;abstract;
  48. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);virtual;abstract;
  49. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);virtual;abstract;
  50. public
  51. constructor create(sym: tstaticvarsym);
  52. end;
  53. ttypedconstbuilderclass = class of ttypedconstbuilder;
  54. { should be changed into nested type of tasmlisttypedconstbuilder when
  55. possible }
  56. tbitpackedval = record
  57. curval, nextval: aword;
  58. curbitoffset: smallint;
  59. loadbitsize,packedbitsize: byte;
  60. end;
  61. tasmlisttypedconstbuilder = class(ttypedconstbuilder)
  62. private
  63. list: tasmlist;
  64. curoffset: asizeint;
  65. function parse_single_packed_const(def: tdef; var bp: tbitpackedval): boolean;
  66. protected
  67. procedure parse_packed_array_def(def: tarraydef);
  68. procedure parse_arraydef(def:tarraydef);override;
  69. procedure parse_procvardef(def:tprocvardef);override;
  70. procedure parse_recorddef(def:trecorddef);override;
  71. procedure parse_objectdef(def:tobjectdef);override;
  72. procedure tc_emit_orddef(def: torddef; var node: tnode);override;
  73. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode); override;
  74. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);override;
  75. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);override;
  76. procedure tc_emit_setdef(def: tsetdef; var node: tnode);override;
  77. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);override;
  78. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);override;
  79. public
  80. constructor create(sym: tstaticvarsym);virtual;
  81. function parse_into_asmlist: tasmlist;
  82. end;
  83. tasmlisttypedconstbuilderclass = class of tasmlisttypedconstbuilder;
  84. tnodetreetypedconstbuilder = class(ttypedconstbuilder)
  85. protected
  86. resultblock: tblocknode;
  87. statmnt: tstatementnode;
  88. { when parsing a record, the base nade becomes a loadnode of the record,
  89. etc. }
  90. basenode: tnode;
  91. procedure parse_arraydef(def:tarraydef);override;
  92. procedure parse_procvardef(def:tprocvardef);override;
  93. procedure parse_recorddef(def:trecorddef);override;
  94. procedure parse_objectdef(def:tobjectdef);override;
  95. procedure tc_emit_orddef(def: torddef; var node: tnode);override;
  96. procedure tc_emit_floatdef(def: tfloatdef; var node: tnode); override;
  97. procedure tc_emit_classrefdef(def: tclassrefdef; var node: tnode);override;
  98. procedure tc_emit_pointerdef(def: tpointerdef; var node: tnode);override;
  99. procedure tc_emit_setdef(def: tsetdef; var node: tnode);override;
  100. procedure tc_emit_enumdef(def: tenumdef; var node: tnode);override;
  101. procedure tc_emit_stringdef(def: tstringdef; var node: tnode);override;
  102. public
  103. constructor create(sym: tstaticvarsym; previnit: tnode);virtual;
  104. destructor destroy;override;
  105. function parse_into_nodetree: tnode;
  106. end;
  107. tnodetreetypedconstbuilderclass = class of tnodetreetypedconstbuilder;
  108. var
  109. ctypedconstbuilder: ttypedconstbuilderclass;
  110. implementation
  111. uses
  112. SysUtils,
  113. 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. strch : char;
  384. ll : tasmlabofs;
  385. ca : pchar;
  386. winlike : boolean;
  387. hsym : tconstsym;
  388. begin
  389. { load strval and strlength of the constant tree }
  390. if (node.nodetype=stringconstn) or is_wide_or_unicode_string(def) or is_constwidecharnode(node) or
  391. ((node.nodetype=typen) and is_interfacecorba(ttypenode(node).typedef)) then
  392. begin
  393. { convert to the expected string type so that
  394. for widestrings strval is a pcompilerwidestring }
  395. inserttypeconv(node,def);
  396. if (not codegenerror) and
  397. (node.nodetype=stringconstn) then
  398. begin
  399. strlength:=tstringconstnode(node).len;
  400. strval:=tstringconstnode(node).value_str;
  401. end
  402. else
  403. begin
  404. { an error occurred trying to convert the result to a string }
  405. strlength:=-1;
  406. { it's possible that the type conversion could not be
  407. evaluated at compile-time }
  408. if not codegenerror then
  409. CGMessage(parser_e_widestring_to_ansi_compile_time);
  410. end;
  411. end
  412. else if is_constcharnode(node) then
  413. begin
  414. { strval:=pchar(@tordconstnode(node).value);
  415. THIS FAIL on BIG_ENDIAN MACHINES PM }
  416. strch:=chr(tordconstnode(node).value.svalue and $ff);
  417. strval:=@strch;
  418. strlength:=1
  419. end
  420. else if is_constresourcestringnode(node) then
  421. begin
  422. hsym:=tconstsym(tloadnode(node).symtableentry);
  423. strval:=pchar(hsym.value.valueptr);
  424. strlength:=hsym.value.len;
  425. { Delphi-compatible (mis)feature:
  426. Link AnsiString constants to their initializing resourcestring,
  427. enabling them to be (re)translated at runtime.
  428. Wide/UnicodeString are currently rejected above (with incorrect error message).
  429. ShortStrings cannot be handled unless another table is built for them;
  430. considering this acceptable, because Delphi rejects them altogether.
  431. }
  432. if (not is_shortstring(def)) and
  433. ((tcsym.owner.symtablelevel<=main_program_level) or
  434. (current_old_block_type=bt_const)) then
  435. begin
  436. current_asmdata.ResStrInits.Concat(
  437. TTCInitItem.Create(tcsym,curoffset,
  438. current_asmdata.RefAsmSymbol(make_mangledname('RESSTR',hsym.owner,hsym.name),AT_DATA))
  439. );
  440. Include(tcsym.varoptions,vo_force_finalize);
  441. end;
  442. end
  443. else
  444. begin
  445. Message(parser_e_illegal_expression);
  446. strlength:=-1;
  447. end;
  448. if strlength>=0 then
  449. begin
  450. case def.stringtype of
  451. st_shortstring:
  452. begin
  453. if strlength>=def.size then
  454. begin
  455. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  456. strlength:=def.size-1;
  457. end;
  458. list.concat(Tai_const.Create_8bit(strlength));
  459. { this can also handle longer strings }
  460. getmem(ca,strlength+1);
  461. move(strval^,ca^,strlength);
  462. ca[strlength]:=#0;
  463. list.concat(Tai_string.Create_pchar(ca,strlength));
  464. { fillup with spaces if size is shorter }
  465. if def.size>strlength then
  466. begin
  467. getmem(ca,def.size-strlength);
  468. { def.size contains also the leading length, so we }
  469. { we have to subtract one }
  470. fillchar(ca[0],def.size-strlength-1,' ');
  471. ca[def.size-strlength-1]:=#0;
  472. { this can also handle longer strings }
  473. list.concat(Tai_string.Create_pchar(ca,def.size-strlength-1));
  474. end;
  475. end;
  476. st_ansistring:
  477. begin
  478. { an empty ansi string is nil! }
  479. if (strlength=0) then
  480. begin
  481. ll.lab:=nil;
  482. ll.ofs:=0;
  483. end
  484. else
  485. ll:=emit_ansistring_const(current_asmdata.asmlists[al_const],strval,strlength,def.encoding);
  486. list.concat(Tai_const.Create_sym_offset(ll.lab,ll.ofs));
  487. end;
  488. st_unicodestring,
  489. st_widestring:
  490. begin
  491. { an empty wide/unicode string is nil! }
  492. if (strlength=0) then
  493. begin
  494. ll.lab:=nil;
  495. ll.ofs:=0;
  496. end
  497. else
  498. begin
  499. winlike:=(def.stringtype=st_widestring) and (tf_winlikewidestring in target_info.flags);
  500. ll:=emit_unicodestring_const(current_asmdata.asmlists[al_const],
  501. strval,
  502. def.encoding,
  503. winlike);
  504. { Collect Windows widestrings that need initialization at startup.
  505. Local initialized vars are excluded because they are initialized
  506. at function entry instead. }
  507. if winlike and
  508. ((tcsym.owner.symtablelevel<=main_program_level) or
  509. (current_old_block_type=bt_const)) then
  510. begin
  511. if ll.ofs<>0 then
  512. internalerror(2012051704);
  513. current_asmdata.WideInits.Concat(
  514. TTCInitItem.Create(tcsym,curoffset,ll.lab)
  515. );
  516. ll.lab:=nil;
  517. ll.ofs:=0;
  518. Include(tcsym.varoptions,vo_force_finalize);
  519. end;
  520. end;
  521. list.concat(Tai_const.Create_sym_offset(ll.lab,ll.ofs));
  522. end;
  523. else
  524. internalerror(200107081);
  525. end;
  526. end;
  527. end;
  528. procedure tasmlisttypedconstbuilder.tc_emit_orddef(def: torddef; var node: tnode);
  529. var
  530. intvalue: tconstexprint;
  531. procedure do_error;
  532. begin
  533. if is_constnode(node) then
  534. IncompatibleTypes(node.resultdef, def)
  535. else if not(parse_generic) then
  536. Message(parser_e_illegal_expression);
  537. end;
  538. begin
  539. case def.ordtype of
  540. pasbool8,
  541. bool8bit :
  542. begin
  543. if is_constboolnode(node) then
  544. begin
  545. testrange(def,tordconstnode(node).value,false,false);
  546. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)))
  547. end
  548. else
  549. do_error;
  550. end;
  551. pasbool16,
  552. bool16bit :
  553. begin
  554. if is_constboolnode(node) then
  555. begin
  556. testrange(def,tordconstnode(node).value,false,false);
  557. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)))
  558. end
  559. else
  560. do_error;
  561. end;
  562. pasbool32,
  563. bool32bit :
  564. begin
  565. if is_constboolnode(node) then
  566. begin
  567. testrange(def,tordconstnode(node).value,false,false);
  568. list.concat(Tai_const.Create_32bit(longint(tordconstnode(node).value.svalue)))
  569. end
  570. else
  571. do_error;
  572. end;
  573. pasbool64,
  574. bool64bit :
  575. begin
  576. if is_constboolnode(node) then
  577. begin
  578. testrange(def,tordconstnode(node).value,false,false);
  579. list.concat(Tai_const.Create_64bit(int64(tordconstnode(node).value.svalue)))
  580. end
  581. else
  582. do_error;
  583. end;
  584. uchar :
  585. begin
  586. if is_constwidecharnode(node) then
  587. inserttypeconv(node,cansichartype);
  588. if is_constcharnode(node) or
  589. ((m_delphi in current_settings.modeswitches) and
  590. is_constwidecharnode(node) and
  591. (tordconstnode(node).value <= 255)) then
  592. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)))
  593. else
  594. do_error;
  595. end;
  596. uwidechar :
  597. begin
  598. if is_constcharnode(node) then
  599. inserttypeconv(node,cwidechartype);
  600. if is_constwidecharnode(node) then
  601. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)))
  602. else
  603. do_error;
  604. end;
  605. s8bit,u8bit,
  606. u16bit,s16bit,
  607. s32bit,u32bit,
  608. s64bit,u64bit :
  609. begin
  610. if is_constintnode(node) then
  611. begin
  612. testrange(def,tordconstnode(node).value,false,false);
  613. case def.size of
  614. 1 :
  615. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)));
  616. 2 :
  617. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)));
  618. 4 :
  619. list.concat(Tai_const.Create_32bit(longint(tordconstnode(node).value.svalue)));
  620. 8 :
  621. list.concat(Tai_const.Create_64bit(tordconstnode(node).value.svalue));
  622. end;
  623. end
  624. else
  625. do_error;
  626. end;
  627. scurrency:
  628. begin
  629. if is_constintnode(node) then
  630. intvalue:=tordconstnode(node).value*10000
  631. { allow bootstrapping }
  632. else if is_constrealnode(node) then
  633. intvalue:=PInt64(@trealconstnode(node).value_currency)^
  634. else
  635. begin
  636. intvalue:=0;
  637. IncompatibleTypes(node.resultdef, def);
  638. end;
  639. list.concat(Tai_const.Create_64bit(intvalue));
  640. end;
  641. else
  642. internalerror(200611052);
  643. end;
  644. end;
  645. procedure tasmlisttypedconstbuilder.tc_emit_floatdef(def: tfloatdef; var node: tnode);
  646. var
  647. value : bestreal;
  648. begin
  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. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(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. case tvecnode(hp).left.resultdef.typ of
  824. stringdef :
  825. begin
  826. { this seems OK for shortstring and ansistrings PM }
  827. { it is wrong for widestrings !! }
  828. len:=1;
  829. base:=0;
  830. end;
  831. arraydef :
  832. begin
  833. if not is_packed_array(tvecnode(hp).left.resultdef) then
  834. begin
  835. len:=tarraydef(tvecnode(hp).left.resultdef).elesize;
  836. base:=tarraydef(tvecnode(hp).left.resultdef).lowrange;
  837. end
  838. else
  839. begin
  840. Message(parser_e_packed_dynamic_open_array);
  841. len:=1;
  842. base:=0;
  843. end;
  844. end
  845. else
  846. Message(parser_e_illegal_expression);
  847. end;
  848. if is_constintnode(tvecnode(hp).right) then
  849. begin
  850. {Prevent overflow.}
  851. v:=get_ordinal_value(tvecnode(hp).right)-base;
  852. if (v<int64(low(offset))) or (v>int64(high(offset))) then
  853. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(offset)),tostr(high(offset)));
  854. if high(offset)-offset div len>v then
  855. inc(offset,len*v.svalue)
  856. else
  857. message3(type_e_range_check_error_bounds,tostr(v),'0',tostr(high(offset)-offset div len))
  858. end
  859. else
  860. Message(parser_e_illegal_expression);
  861. end;
  862. subscriptn :
  863. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  864. typeconvn :
  865. begin
  866. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  867. Message(parser_e_illegal_expression);
  868. end;
  869. addrn :
  870. ;
  871. else
  872. Message(parser_e_illegal_expression);
  873. end;
  874. hp:=tunarynode(hp).left;
  875. end;
  876. srsym:=tloadnode(hp).symtableentry;
  877. case srsym.typ of
  878. procsym :
  879. begin
  880. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  881. if Tprocsym(srsym).ProcdefList.Count>1 then
  882. Message(parser_e_no_overloaded_procvars);
  883. if po_abstractmethod in pd.procoptions then
  884. Message(type_e_cant_take_address_of_abstract_method)
  885. else
  886. list.concat(Tai_const.Createname(pd.mangledname,offset));
  887. end;
  888. staticvarsym :
  889. list.concat(Tai_const.Createname(tstaticvarsym(srsym).mangledname,offset));
  890. labelsym :
  891. list.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,offset));
  892. constsym :
  893. if tconstsym(srsym).consttyp=constresourcestring then
  894. list.concat(Tai_const.Createname(make_mangledname('RESSTR',tconstsym(srsym).owner,tconstsym(srsym).name),AT_DATA,sizeof(pint)))
  895. else
  896. Message(type_e_variable_id_expected);
  897. else
  898. Message(type_e_variable_id_expected);
  899. end;
  900. end
  901. else
  902. Message(parser_e_illegal_expression);
  903. end
  904. else
  905. { allow typeof(Object type)}
  906. if (node.nodetype=inlinen) and
  907. (tinlinenode(node).inlinenumber=in_typeof_x) then
  908. begin
  909. if (tinlinenode(node).left.nodetype=typen) then
  910. begin
  911. list.concat(Tai_const.createname(
  912. tobjectdef(tinlinenode(node).left.resultdef).vmt_mangledname,AT_DATA,0));
  913. end
  914. else
  915. Message(parser_e_illegal_expression);
  916. end
  917. else
  918. Message(parser_e_illegal_expression);
  919. end;
  920. procedure tasmlisttypedconstbuilder.tc_emit_setdef(def: tsetdef; var node: tnode);
  921. type
  922. setbytes = array[0..31] of byte;
  923. Psetbytes = ^setbytes;
  924. var
  925. i: longint;
  926. begin
  927. if node.nodetype=setconstn then
  928. begin
  929. { be sure to convert to the correct result, else
  930. it can generate smallset data instead of normalset (PFV) }
  931. inserttypeconv(node,def);
  932. { we only allow const sets }
  933. if (node.nodetype<>setconstn) or
  934. assigned(tsetconstnode(node).left) then
  935. Message(parser_e_illegal_expression)
  936. else
  937. begin
  938. tsetconstnode(node).adjustforsetbase;
  939. { this writing is endian-dependant }
  940. if source_info.endian = target_info.endian then
  941. begin
  942. for i:=0 to node.resultdef.size-1 do
  943. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(node).value_set)^[i]));
  944. end
  945. else
  946. begin
  947. for i:=0 to node.resultdef.size-1 do
  948. list.concat(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(node).value_set)^[i])));
  949. end;
  950. end;
  951. end
  952. else
  953. Message(parser_e_illegal_expression);
  954. end;
  955. procedure tasmlisttypedconstbuilder.tc_emit_enumdef(def: tenumdef; var node: tnode);
  956. begin
  957. if node.nodetype=ordconstn then
  958. begin
  959. if equal_defs(node.resultdef,def) or
  960. is_subequal(node.resultdef,def) then
  961. begin
  962. testrange(def,tordconstnode(node).value,false,false);
  963. case longint(node.resultdef.size) of
  964. 1 : list.concat(Tai_const.Create_8bit(Byte(tordconstnode(node).value.svalue)));
  965. 2 : list.concat(Tai_const.Create_16bit(Word(tordconstnode(node).value.svalue)));
  966. 4 : list.concat(Tai_const.Create_32bit(Longint(tordconstnode(node).value.svalue)));
  967. end;
  968. end
  969. else
  970. IncompatibleTypes(node.resultdef,def);
  971. end
  972. else
  973. Message(parser_e_illegal_expression);
  974. end;
  975. { parse a single constant and add it to the packed const info }
  976. { represented by curval etc (see explanation of bitpackval for }
  977. { what the different parameters mean) }
  978. function tasmlisttypedconstbuilder.parse_single_packed_const(def: tdef; var bp: tbitpackedval): boolean;
  979. var
  980. node: tnode;
  981. begin
  982. result:=true;
  983. node:=comp_expr(true,false);
  984. if (node.nodetype <> ordconstn) or
  985. (not equal_defs(node.resultdef,def) and
  986. not is_subequal(node.resultdef,def)) then
  987. begin
  988. node.free;
  989. incompatibletypes(node.resultdef,def);
  990. consume_all_until(_SEMICOLON);
  991. result:=false;
  992. exit;
  993. end;
  994. if (Tordconstnode(node).value<qword(low(Aword))) or (Tordconstnode(node).value>qword(high(Aword))) then
  995. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(node).value),tostr(low(Aword)),tostr(high(Aword)))
  996. else
  997. bitpackval(Tordconstnode(node).value.uvalue,bp);
  998. if (bp.curbitoffset>=AIntBits) then
  999. flush_packed_value(list,bp);
  1000. node.free;
  1001. end;
  1002. procedure tasmlisttypedconstbuilder.parse_arraydef(def:tarraydef);
  1003. var
  1004. n : tnode;
  1005. i : longint;
  1006. len : asizeint;
  1007. ch : array[0..1] of char;
  1008. ca : pbyte;
  1009. int_const: tai_const;
  1010. char_size: integer;
  1011. oldoffset: asizeint;
  1012. begin
  1013. { dynamic array nil }
  1014. if is_dynamic_array(def) then
  1015. begin
  1016. { Only allow nil initialization }
  1017. consume(_NIL);
  1018. list.concat(Tai_const.Create_sym(nil));
  1019. end
  1020. { packed array constant }
  1021. else if is_packed_array(def) and
  1022. ((def.elepackedbitsize mod 8 <> 0) or
  1023. not ispowerof2(def.elepackedbitsize div 8,i)) then
  1024. begin
  1025. parse_packed_array_def(def);
  1026. end
  1027. { normal array const between brackets }
  1028. else if try_to_consume(_LKLAMMER) then
  1029. begin
  1030. oldoffset:=curoffset;
  1031. curoffset:=0;
  1032. for i:=def.lowrange to def.highrange-1 do
  1033. begin
  1034. read_typed_const_data(def.elementdef);
  1035. Inc(curoffset,def.elementdef.size);
  1036. if token=_RKLAMMER then
  1037. begin
  1038. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  1039. consume(_RKLAMMER);
  1040. exit;
  1041. end
  1042. else
  1043. consume(_COMMA);
  1044. end;
  1045. read_typed_const_data(def.elementdef);
  1046. consume(_RKLAMMER);
  1047. curoffset:=oldoffset;
  1048. end
  1049. { if array of char then we allow also a string }
  1050. else if is_anychar(def.elementdef) then
  1051. begin
  1052. char_size:=def.elementdef.size;
  1053. n:=comp_expr(true,false);
  1054. if n.nodetype=stringconstn then
  1055. begin
  1056. len:=tstringconstnode(n).len;
  1057. case char_size of
  1058. 1:
  1059. begin
  1060. if (tstringconstnode(n).cst_type in [cst_unicodestring,cst_widestring]) then
  1061. inserttypeconv(n,getansistringdef);
  1062. if n.nodetype<>stringconstn then
  1063. internalerror(2010033003);
  1064. ca:=pointer(tstringconstnode(n).value_str);
  1065. end;
  1066. 2:
  1067. begin
  1068. inserttypeconv(n,cunicodestringtype);
  1069. if n.nodetype<>stringconstn then
  1070. internalerror(2010033003);
  1071. ca:=pointer(pcompilerwidestring(tstringconstnode(n).value_str)^.data)
  1072. end;
  1073. else
  1074. internalerror(2010033005);
  1075. end;
  1076. { For tp7 the maximum lentgh can be 255 }
  1077. if (m_tp7 in current_settings.modeswitches) and
  1078. (len>255) then
  1079. len:=255;
  1080. end
  1081. else if is_constcharnode(n) then
  1082. begin
  1083. case char_size of
  1084. 1:
  1085. ch[0]:=chr(tordconstnode(n).value.uvalue and $ff);
  1086. 2:
  1087. begin
  1088. inserttypeconv(n,cwidechartype);
  1089. if not is_constwidecharnode(n) then
  1090. internalerror(2010033001);
  1091. widechar(ch):=widechar(tordconstnode(n).value.uvalue and $ffff);
  1092. end;
  1093. else
  1094. internalerror(2010033002);
  1095. end;
  1096. ca:=@ch;
  1097. len:=1;
  1098. end
  1099. else if is_constwidecharnode(n) and (current_settings.sourcecodepage<>CP_UTF8) then
  1100. begin
  1101. case char_size of
  1102. 1:
  1103. begin
  1104. inserttypeconv(n,cansichartype);
  1105. if not is_constcharnode(n) then
  1106. internalerror(2010033001);
  1107. ch[0]:=chr(tordconstnode(n).value.uvalue and $ff);
  1108. end;
  1109. 2:
  1110. widechar(ch):=widechar(tordconstnode(n).value.uvalue and $ffff);
  1111. else
  1112. internalerror(2010033002);
  1113. end;
  1114. ca:=@ch;
  1115. len:=1;
  1116. end
  1117. else
  1118. begin
  1119. Message(parser_e_illegal_expression);
  1120. len:=0;
  1121. end;
  1122. if len>(def.highrange-def.lowrange+1) then
  1123. Message(parser_e_string_larger_array);
  1124. for i:=0 to def.highrange-def.lowrange do
  1125. begin
  1126. if i<len then
  1127. begin
  1128. case char_size of
  1129. 1:
  1130. int_const:=Tai_const.Create_char(char_size,pbyte(ca)^);
  1131. 2:
  1132. int_const:=Tai_const.Create_char(char_size,pword(ca)^);
  1133. else
  1134. internalerror(2010033004);
  1135. end;
  1136. inc(ca, char_size);
  1137. end
  1138. else
  1139. {Fill the remaining positions with #0.}
  1140. int_const:=Tai_const.Create_char(char_size,0);
  1141. list.concat(int_const)
  1142. end;
  1143. n.free;
  1144. end
  1145. else
  1146. begin
  1147. { we want the ( }
  1148. consume(_LKLAMMER);
  1149. end;
  1150. end;
  1151. procedure tasmlisttypedconstbuilder.parse_procvardef(def:tprocvardef);
  1152. var
  1153. tmpn,n : tnode;
  1154. pd : tprocdef;
  1155. begin
  1156. { Procvars and pointers are no longer compatible. }
  1157. { under tp: =nil or =var under fpc: =nil or =@var }
  1158. if try_to_consume(_NIL) then
  1159. begin
  1160. list.concat(Tai_const.Create_sym(nil));
  1161. if not def.is_addressonly then
  1162. list.concat(Tai_const.Create_sym(nil));
  1163. exit;
  1164. end;
  1165. { you can't assign a value other than NIL to a typed constant }
  1166. { which is a "procedure of object", because this also requires }
  1167. { address of an object/class instance, which is not known at }
  1168. { compile time (JM) }
  1169. if (po_methodpointer in def.procoptions) then
  1170. Message(parser_e_no_procvarobj_const);
  1171. { parse the rest too, so we can continue with error checking }
  1172. getprocvardef:=def;
  1173. n:=comp_expr(true,false);
  1174. getprocvardef:=nil;
  1175. if codegenerror then
  1176. begin
  1177. n.free;
  1178. exit;
  1179. end;
  1180. { let type conversion check everything needed }
  1181. inserttypeconv(n,def);
  1182. if codegenerror then
  1183. begin
  1184. n.free;
  1185. exit;
  1186. end;
  1187. { remove typeconvs, that will normally insert a lea
  1188. instruction which is not necessary for us }
  1189. while n.nodetype=typeconvn do
  1190. begin
  1191. tmpn:=ttypeconvnode(n).left;
  1192. ttypeconvnode(n).left:=nil;
  1193. n.free;
  1194. n:=tmpn;
  1195. end;
  1196. { remove addrn which we also don't need here }
  1197. if n.nodetype=addrn then
  1198. begin
  1199. tmpn:=taddrnode(n).left;
  1200. taddrnode(n).left:=nil;
  1201. n.free;
  1202. n:=tmpn;
  1203. end;
  1204. { we now need to have a loadn with a procsym }
  1205. if (n.nodetype=loadn) and
  1206. (tloadnode(n).symtableentry.typ=procsym) then
  1207. begin
  1208. pd:=tloadnode(n).procdef;
  1209. list.concat(Tai_const.createname(pd.mangledname,0));
  1210. { nested procvar typed consts can only be initialised with nil
  1211. (checked above) or with a global procedure (checked here),
  1212. because in other cases we need a valid frame pointer }
  1213. if is_nested_pd(def) then
  1214. begin
  1215. if is_nested_pd(pd) then
  1216. Message(parser_e_no_procvarnested_const);
  1217. list.concat(Tai_const.Create_sym(nil));
  1218. end
  1219. end
  1220. else if n.nodetype=pointerconstn then
  1221. list.concat(Tai_const.Create_pint(tpointerconstnode(n).value))
  1222. else
  1223. Message(parser_e_illegal_expression);
  1224. n.free;
  1225. end;
  1226. procedure tasmlisttypedconstbuilder.parse_recorddef(def:trecorddef);
  1227. var
  1228. n : tnode;
  1229. symidx : longint;
  1230. recsym,
  1231. srsym : tsym;
  1232. hs : string;
  1233. sorg,s : TIDString;
  1234. tmpguid : tguid;
  1235. recoffset,
  1236. fillbytes : aint;
  1237. bp : tbitpackedval;
  1238. error,
  1239. is_packed: boolean;
  1240. startoffset: aint;
  1241. procedure handle_stringconstn;
  1242. var
  1243. i : longint;
  1244. begin
  1245. hs:=strpas(tstringconstnode(n).value_str);
  1246. if string2guid(hs,tmpguid) then
  1247. begin
  1248. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  1249. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  1250. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  1251. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1252. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  1253. end
  1254. else
  1255. Message(parser_e_improper_guid_syntax);
  1256. end;
  1257. var
  1258. i : longint;
  1259. SymList:TFPHashObjectList;
  1260. begin
  1261. { GUID }
  1262. if (def=rec_tguid) and (token=_ID) then
  1263. begin
  1264. n:=comp_expr(true,false);
  1265. if n.nodetype=stringconstn then
  1266. handle_stringconstn
  1267. else
  1268. begin
  1269. inserttypeconv(n,rec_tguid);
  1270. if n.nodetype=guidconstn then
  1271. begin
  1272. tmpguid:=tguidconstnode(n).value;
  1273. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  1274. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  1275. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  1276. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1277. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  1278. end
  1279. else
  1280. Message(parser_e_illegal_expression);
  1281. end;
  1282. n.free;
  1283. exit;
  1284. end;
  1285. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1286. begin
  1287. n:=comp_expr(true,false);
  1288. inserttypeconv(n,cshortstringtype);
  1289. if n.nodetype=stringconstn then
  1290. handle_stringconstn
  1291. else
  1292. Message(parser_e_illegal_expression);
  1293. n.free;
  1294. exit;
  1295. end;
  1296. { bitpacked record? }
  1297. is_packed:=is_packed_record_or_object(def);
  1298. if (is_packed) then
  1299. begin
  1300. { loadbitsize = 8, bitpacked records are always padded to }
  1301. { a multiple of a byte. packedbitsize will be set separately }
  1302. { for each field }
  1303. initbitpackval(bp,0);
  1304. bp.loadbitsize:=8;
  1305. end;
  1306. { normal record }
  1307. consume(_LKLAMMER);
  1308. recoffset:=0;
  1309. sorg:='';
  1310. symidx:=0;
  1311. symlist:=def.symtable.SymList;
  1312. srsym:=get_next_varsym(def,symlist,symidx);
  1313. recsym := nil;
  1314. startoffset:=curoffset;
  1315. while token<>_RKLAMMER do
  1316. begin
  1317. s:=pattern;
  1318. sorg:=orgpattern;
  1319. consume(_ID);
  1320. consume(_COLON);
  1321. error := false;
  1322. recsym := tsym(def.symtable.Find(s));
  1323. if not assigned(recsym) then
  1324. begin
  1325. Message1(sym_e_illegal_field,sorg);
  1326. error := true;
  1327. end;
  1328. if (not error) and
  1329. (not assigned(srsym) or
  1330. (s <> srsym.name)) then
  1331. { possible variant record (JM) }
  1332. begin
  1333. { All parts of a variant start at the same offset }
  1334. { Also allow jumping from one variant part to another, }
  1335. { as long as the offsets match }
  1336. if (assigned(srsym) and
  1337. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1338. { srsym is not assigned after parsing w2 in the }
  1339. { typed const in the next example: }
  1340. { type tr = record case byte of }
  1341. { 1: (l1,l2: dword); }
  1342. { 2: (w1,w2: word); }
  1343. { end; }
  1344. { const r: tr = (w1:1;w2:1;l2:5); }
  1345. (tfieldvarsym(recsym).fieldoffset = recoffset) then
  1346. begin
  1347. srsym:=recsym;
  1348. { symidx should contain the next symbol id to search }
  1349. symidx:=SymList.indexof(srsym)+1;
  1350. end
  1351. { going backwards isn't allowed in any mode }
  1352. else if (tfieldvarsym(recsym).fieldoffset<recoffset) then
  1353. begin
  1354. Message(parser_e_invalid_record_const);
  1355. error := true;
  1356. end
  1357. { Delphi allows you to skip fields }
  1358. else if (m_delphi in current_settings.modeswitches) then
  1359. begin
  1360. Message1(parser_w_skipped_fields_before,sorg);
  1361. srsym := recsym;
  1362. end
  1363. { FPC and TP don't }
  1364. else
  1365. begin
  1366. Message1(parser_e_skipped_fields_before,sorg);
  1367. error := true;
  1368. end;
  1369. end;
  1370. if error then
  1371. consume_all_until(_SEMICOLON)
  1372. else
  1373. begin
  1374. { if needed fill (alignment) }
  1375. if tfieldvarsym(srsym).fieldoffset>recoffset then
  1376. begin
  1377. if not(is_packed) then
  1378. fillbytes:=tfieldvarsym(srsym).fieldoffset-recoffset
  1379. else
  1380. begin
  1381. flush_packed_value(list,bp);
  1382. { curoffset is now aligned to the next byte }
  1383. recoffset:=align(recoffset,8);
  1384. { offsets are in bits in this case }
  1385. fillbytes:=(tfieldvarsym(srsym).fieldoffset-recoffset) div 8;
  1386. end;
  1387. for i:=1 to fillbytes do
  1388. list.concat(Tai_const.Create_8bit(0))
  1389. end;
  1390. { new position }
  1391. recoffset:=tfieldvarsym(srsym).fieldoffset;
  1392. if not(is_packed) then
  1393. inc(recoffset,tfieldvarsym(srsym).vardef.size)
  1394. else
  1395. inc(recoffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1396. { read the data }
  1397. if not(is_packed) or
  1398. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1399. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1400. begin
  1401. if is_packed then
  1402. begin
  1403. flush_packed_value(list,bp);
  1404. recoffset:=align(recoffset,8);
  1405. end;
  1406. curoffset:=startoffset+tfieldvarsym(srsym).fieldoffset;
  1407. read_typed_const_data(tfieldvarsym(srsym).vardef);
  1408. end
  1409. else
  1410. begin
  1411. bp.packedbitsize:=tfieldvarsym(srsym).vardef.packedbitsize;
  1412. parse_single_packed_const(tfieldvarsym(srsym).vardef,bp);
  1413. end;
  1414. { keep previous field for checking whether whole }
  1415. { record was initialized (JM) }
  1416. recsym := srsym;
  1417. { goto next field }
  1418. srsym:=get_next_varsym(def,SymList,symidx);
  1419. if token=_SEMICOLON then
  1420. consume(_SEMICOLON)
  1421. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1422. consume(_COMMA)
  1423. else
  1424. break;
  1425. end;
  1426. end;
  1427. curoffset:=startoffset;
  1428. { are there any fields left, but don't complain if there only
  1429. come other variant parts after the last initialized field }
  1430. if assigned(srsym) and
  1431. (
  1432. (recsym=nil) or
  1433. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1434. ) then
  1435. Message1(parser_w_skipped_fields_after,sorg);
  1436. if not(is_packed) then
  1437. fillbytes:=def.size-recoffset
  1438. else
  1439. begin
  1440. flush_packed_value(list,bp);
  1441. recoffset:=align(recoffset,8);
  1442. fillbytes:=def.size-(recoffset div 8);
  1443. end;
  1444. for i:=1 to fillbytes do
  1445. list.concat(Tai_const.Create_8bit(0));
  1446. consume(_RKLAMMER);
  1447. end;
  1448. procedure tasmlisttypedconstbuilder.parse_objectdef(def:tobjectdef);
  1449. var
  1450. n : tnode;
  1451. i : longint;
  1452. obj : tobjectdef;
  1453. srsym : tsym;
  1454. st : tsymtable;
  1455. objoffset : aint;
  1456. s,sorg : TIDString;
  1457. vmtwritten : boolean;
  1458. startoffset:aint;
  1459. begin
  1460. { no support for packed object }
  1461. if is_packed_record_or_object(def) then
  1462. begin
  1463. Message(type_e_no_const_packed_record);
  1464. exit;
  1465. end;
  1466. { only allow nil for implicit pointer object types }
  1467. if is_implicit_pointer_object_type(def) then
  1468. begin
  1469. n:=comp_expr(true,false);
  1470. if n.nodetype<>niln then
  1471. begin
  1472. Message(parser_e_type_const_not_possible);
  1473. consume_all_until(_SEMICOLON);
  1474. end
  1475. else
  1476. list.concat(Tai_const.Create_sym(nil));
  1477. n.free;
  1478. exit;
  1479. end;
  1480. { for objects we allow it only if it doesn't contain a vmt }
  1481. if (oo_has_vmt in def.objectoptions) and
  1482. (m_fpc in current_settings.modeswitches) then
  1483. begin
  1484. Message(parser_e_type_object_constants);
  1485. exit;
  1486. end;
  1487. consume(_LKLAMMER);
  1488. startoffset:=curoffset;
  1489. objoffset:=0;
  1490. vmtwritten:=false;
  1491. while token<>_RKLAMMER do
  1492. begin
  1493. s:=pattern;
  1494. sorg:=orgpattern;
  1495. consume(_ID);
  1496. consume(_COLON);
  1497. srsym:=nil;
  1498. obj:=tobjectdef(def);
  1499. st:=obj.symtable;
  1500. while (srsym=nil) and assigned(st) do
  1501. begin
  1502. srsym:=tsym(st.Find(s));
  1503. if assigned(obj) then
  1504. obj:=obj.childof;
  1505. if assigned(obj) then
  1506. st:=obj.symtable
  1507. else
  1508. st:=nil;
  1509. end;
  1510. if (srsym=nil) or
  1511. (srsym.typ<>fieldvarsym) then
  1512. begin
  1513. if (srsym=nil) then
  1514. Message1(sym_e_id_not_found,sorg)
  1515. else
  1516. Message1(sym_e_illegal_field,sorg);
  1517. consume_all_until(_RKLAMMER);
  1518. break;
  1519. end
  1520. else
  1521. with tfieldvarsym(srsym) do
  1522. begin
  1523. { check position }
  1524. if fieldoffset<objoffset then
  1525. message(parser_e_invalid_record_const);
  1526. { check in VMT needs to be added for TP mode }
  1527. if not(vmtwritten) and
  1528. not(m_fpc in current_settings.modeswitches) and
  1529. (oo_has_vmt in def.objectoptions) and
  1530. (def.vmt_offset<fieldoffset) then
  1531. begin
  1532. for i:=1 to def.vmt_offset-objoffset do
  1533. list.concat(tai_const.create_8bit(0));
  1534. list.concat(tai_const.createname(def.vmt_mangledname,AT_DATA,0));
  1535. { this is more general }
  1536. objoffset:=def.vmt_offset + sizeof(pint);
  1537. vmtwritten:=true;
  1538. end;
  1539. { if needed fill }
  1540. if fieldoffset>objoffset then
  1541. for i:=1 to fieldoffset-objoffset do
  1542. list.concat(Tai_const.Create_8bit(0));
  1543. { new position }
  1544. objoffset:=fieldoffset+vardef.size;
  1545. { read the data }
  1546. curoffset:=startoffset+fieldoffset;
  1547. read_typed_const_data(vardef);
  1548. if not try_to_consume(_SEMICOLON) then
  1549. break;
  1550. end;
  1551. end;
  1552. curoffset:=startoffset;
  1553. if not(m_fpc in current_settings.modeswitches) and
  1554. (oo_has_vmt in def.objectoptions) and
  1555. (def.vmt_offset>=objoffset) then
  1556. begin
  1557. for i:=1 to def.vmt_offset-objoffset do
  1558. list.concat(tai_const.create_8bit(0));
  1559. list.concat(tai_const.createname(def.vmt_mangledname,AT_DATA,0));
  1560. { this is more general }
  1561. objoffset:=def.vmt_offset + sizeof(pint);
  1562. end;
  1563. for i:=1 to def.size-objoffset do
  1564. list.concat(Tai_const.Create_8bit(0));
  1565. consume(_RKLAMMER);
  1566. end;
  1567. function tasmlisttypedconstbuilder.parse_into_asmlist: tasmlist;
  1568. begin
  1569. read_typed_const_data(tcsym.vardef);
  1570. result:=list;
  1571. end;
  1572. { tnodetreetypedconstbuilder }
  1573. procedure tnodetreetypedconstbuilder.parse_arraydef(def: tarraydef);
  1574. var
  1575. n : tnode;
  1576. i : longint;
  1577. orgbase: tnode;
  1578. begin
  1579. { dynamic array nil }
  1580. if is_dynamic_array(def) then
  1581. begin
  1582. { Only allow nil initialization }
  1583. consume(_NIL);
  1584. addstatement(statmnt,cassignmentnode.create_internal(basenode,cnilnode.create));
  1585. basenode:=nil;
  1586. end
  1587. { array const between brackets }
  1588. else if try_to_consume(_LKLAMMER) then
  1589. begin
  1590. orgbase:=basenode;
  1591. for i:=def.lowrange to def.highrange-1 do
  1592. begin
  1593. basenode:=cvecnode.create(orgbase.getcopy,ctypeconvnode.create_explicit(genintconstnode(i),tarraydef(def).rangedef));
  1594. read_typed_const_data(def.elementdef);
  1595. if token=_RKLAMMER then
  1596. begin
  1597. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  1598. consume(_RKLAMMER);
  1599. exit;
  1600. end
  1601. else
  1602. consume(_COMMA);
  1603. end;
  1604. basenode:=cvecnode.create(orgbase,ctypeconvnode.create_explicit(genintconstnode(def.highrange),tarraydef(def).rangedef));
  1605. read_typed_const_data(def.elementdef);
  1606. consume(_RKLAMMER);
  1607. end
  1608. { if array of char then we allow also a string }
  1609. else if is_anychar(def.elementdef) then
  1610. begin
  1611. n:=comp_expr(true,false);
  1612. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1613. basenode:=nil;
  1614. end
  1615. else
  1616. begin
  1617. { we want the ( }
  1618. consume(_LKLAMMER);
  1619. end;
  1620. end;
  1621. procedure tnodetreetypedconstbuilder.parse_procvardef(def: tprocvardef);
  1622. begin
  1623. addstatement(statmnt,cassignmentnode.create_internal(basenode,comp_expr(true,false)));
  1624. basenode:=nil;
  1625. end;
  1626. procedure tnodetreetypedconstbuilder.parse_recorddef(def: trecorddef);
  1627. var
  1628. n,n2 : tnode;
  1629. SymList:TFPHashObjectList;
  1630. orgbasenode : tnode;
  1631. symidx : longint;
  1632. recsym,
  1633. srsym : tsym;
  1634. sorg,s : TIDString;
  1635. recoffset : aint;
  1636. error,
  1637. is_packed: boolean;
  1638. procedure handle_stringconstn;
  1639. begin
  1640. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1641. basenode:=nil;
  1642. n:=nil;
  1643. end;
  1644. begin
  1645. { GUID }
  1646. if (def=rec_tguid) and (token=_ID) then
  1647. begin
  1648. n:=comp_expr(true,false);
  1649. if n.nodetype=stringconstn then
  1650. handle_stringconstn
  1651. else
  1652. begin
  1653. inserttypeconv(n,rec_tguid);
  1654. if n.nodetype=guidconstn then
  1655. begin
  1656. n2:=cstringconstnode.createstr(guid2string(tguidconstnode(n).value));
  1657. n.free;
  1658. n:=n2;
  1659. handle_stringconstn;
  1660. end
  1661. else
  1662. Message(parser_e_illegal_expression);
  1663. end;
  1664. n.free;
  1665. exit;
  1666. end;
  1667. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1668. begin
  1669. n:=comp_expr(true,false);
  1670. inserttypeconv(n,cshortstringtype);
  1671. if n.nodetype=stringconstn then
  1672. handle_stringconstn
  1673. else
  1674. Message(parser_e_illegal_expression);
  1675. n.free;
  1676. exit;
  1677. end;
  1678. { bitpacked record? }
  1679. is_packed:=is_packed_record_or_object(def);
  1680. { normal record }
  1681. consume(_LKLAMMER);
  1682. recoffset:=0;
  1683. sorg:='';
  1684. symidx:=0;
  1685. symlist:=def.symtable.SymList;
  1686. srsym:=get_next_varsym(def,symlist,symidx);
  1687. recsym := nil;
  1688. orgbasenode:=basenode;
  1689. basenode:=nil;
  1690. while token<>_RKLAMMER do
  1691. begin
  1692. s:=pattern;
  1693. sorg:=orgpattern;
  1694. consume(_ID);
  1695. consume(_COLON);
  1696. error := false;
  1697. recsym := tsym(def.symtable.Find(s));
  1698. if not assigned(recsym) then
  1699. begin
  1700. Message1(sym_e_illegal_field,sorg);
  1701. error := true;
  1702. end;
  1703. if (not error) and
  1704. (not assigned(srsym) or
  1705. (s <> srsym.name)) then
  1706. { possible variant record (JM) }
  1707. begin
  1708. { All parts of a variant start at the same offset }
  1709. { Also allow jumping from one variant part to another, }
  1710. { as long as the offsets match }
  1711. if (assigned(srsym) and
  1712. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1713. { srsym is not assigned after parsing w2 in the }
  1714. { typed const in the next example: }
  1715. { type tr = record case byte of }
  1716. { 1: (l1,l2: dword); }
  1717. { 2: (w1,w2: word); }
  1718. { end; }
  1719. { const r: tr = (w1:1;w2:1;l2:5); }
  1720. (tfieldvarsym(recsym).fieldoffset = recoffset) then
  1721. begin
  1722. srsym:=recsym;
  1723. { symidx should contain the next symbol id to search }
  1724. symidx:=SymList.indexof(srsym)+1;
  1725. end
  1726. { going backwards isn't allowed in any mode }
  1727. else if (tfieldvarsym(recsym).fieldoffset<recoffset) then
  1728. begin
  1729. Message(parser_e_invalid_record_const);
  1730. error := true;
  1731. end
  1732. { Delphi allows you to skip fields }
  1733. else if (m_delphi in current_settings.modeswitches) then
  1734. begin
  1735. Message1(parser_w_skipped_fields_before,sorg);
  1736. srsym := recsym;
  1737. end
  1738. { FPC and TP don't }
  1739. else
  1740. begin
  1741. Message1(parser_e_skipped_fields_before,sorg);
  1742. error := true;
  1743. end;
  1744. end;
  1745. if error then
  1746. consume_all_until(_SEMICOLON)
  1747. else
  1748. begin
  1749. { skipping fill bytes happens automatically, since we only
  1750. initialize the defined fields }
  1751. { new position }
  1752. recoffset:=tfieldvarsym(srsym).fieldoffset;
  1753. if not(is_packed) then
  1754. inc(recoffset,tfieldvarsym(srsym).vardef.size)
  1755. else
  1756. inc(recoffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1757. { read the data }
  1758. if is_packed and
  1759. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1760. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1761. recoffset:=align(recoffset,8);
  1762. basenode:=csubscriptnode.create(srsym,orgbasenode.getcopy);
  1763. read_typed_const_data(tfieldvarsym(srsym).vardef);
  1764. { keep previous field for checking whether whole }
  1765. { record was initialized (JM) }
  1766. recsym := srsym;
  1767. { goto next field }
  1768. srsym:=get_next_varsym(def,SymList,symidx);
  1769. if token=_SEMICOLON then
  1770. consume(_SEMICOLON)
  1771. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1772. consume(_COMMA)
  1773. else
  1774. break;
  1775. end;
  1776. end;
  1777. { are there any fields left, but don't complain if there only
  1778. come other variant parts after the last initialized field }
  1779. if assigned(srsym) and
  1780. (
  1781. (recsym=nil) or
  1782. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1783. ) then
  1784. Message1(parser_w_skipped_fields_after,sorg);
  1785. orgbasenode.free;
  1786. basenode:=nil;
  1787. consume(_RKLAMMER);
  1788. end;
  1789. procedure tnodetreetypedconstbuilder.parse_objectdef(def: tobjectdef);
  1790. var
  1791. n,
  1792. orgbasenode : tnode;
  1793. obj : tobjectdef;
  1794. srsym : tsym;
  1795. st : tsymtable;
  1796. objoffset : aint;
  1797. s,sorg : TIDString;
  1798. begin
  1799. { no support for packed object }
  1800. if is_packed_record_or_object(def) then
  1801. begin
  1802. Message(type_e_no_const_packed_record);
  1803. exit;
  1804. end;
  1805. { only allow nil for implicit pointer object types }
  1806. if is_implicit_pointer_object_type(def) then
  1807. begin
  1808. n:=comp_expr(true,false);
  1809. if n.nodetype<>niln then
  1810. begin
  1811. Message(parser_e_type_const_not_possible);
  1812. consume_all_until(_SEMICOLON);
  1813. end
  1814. else
  1815. begin
  1816. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1817. n:=nil;
  1818. basenode:=nil;
  1819. end;
  1820. n.free;
  1821. exit;
  1822. end;
  1823. { for objects we allow it only if it doesn't contain a vmt }
  1824. if (oo_has_vmt in def.objectoptions) and
  1825. (m_fpc in current_settings.modeswitches) then
  1826. begin
  1827. Message(parser_e_type_object_constants);
  1828. exit;
  1829. end;
  1830. consume(_LKLAMMER);
  1831. objoffset:=0;
  1832. orgbasenode:=basenode;
  1833. basenode:=nil;
  1834. while token<>_RKLAMMER do
  1835. begin
  1836. s:=pattern;
  1837. sorg:=orgpattern;
  1838. consume(_ID);
  1839. consume(_COLON);
  1840. srsym:=nil;
  1841. obj:=tobjectdef(def);
  1842. st:=obj.symtable;
  1843. while (srsym=nil) and assigned(st) do
  1844. begin
  1845. srsym:=tsym(st.Find(s));
  1846. if assigned(obj) then
  1847. obj:=obj.childof;
  1848. if assigned(obj) then
  1849. st:=obj.symtable
  1850. else
  1851. st:=nil;
  1852. end;
  1853. if (srsym=nil) or
  1854. (srsym.typ<>fieldvarsym) then
  1855. begin
  1856. if (srsym=nil) then
  1857. Message1(sym_e_id_not_found,sorg)
  1858. else
  1859. Message1(sym_e_illegal_field,sorg);
  1860. consume_all_until(_RKLAMMER);
  1861. break;
  1862. end
  1863. else
  1864. with tfieldvarsym(srsym) do
  1865. begin
  1866. { check position }
  1867. if fieldoffset<objoffset then
  1868. message(parser_e_invalid_record_const);
  1869. { new position }
  1870. objoffset:=fieldoffset+vardef.size;
  1871. { read the data }
  1872. basenode:=csubscriptnode.create(srsym,orgbasenode.getcopy);
  1873. read_typed_const_data(vardef);
  1874. if not try_to_consume(_SEMICOLON) then
  1875. break;
  1876. end;
  1877. end;
  1878. consume(_RKLAMMER);
  1879. end;
  1880. procedure tnodetreetypedconstbuilder.tc_emit_orddef(def: torddef; var node: tnode);
  1881. begin
  1882. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1883. basenode:=nil;
  1884. node:=nil;
  1885. end;
  1886. procedure tnodetreetypedconstbuilder.tc_emit_floatdef(def: tfloatdef; var node: tnode);
  1887. begin
  1888. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1889. basenode:=nil;
  1890. node:=nil;
  1891. end;
  1892. procedure tnodetreetypedconstbuilder.tc_emit_classrefdef(def: tclassrefdef; var node: tnode);
  1893. begin
  1894. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1895. basenode:=nil;
  1896. node:=nil;
  1897. end;
  1898. procedure tnodetreetypedconstbuilder.tc_emit_pointerdef(def: tpointerdef; var node: tnode);
  1899. begin
  1900. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1901. basenode:=nil;
  1902. node:=nil;
  1903. end;
  1904. procedure tnodetreetypedconstbuilder.tc_emit_setdef(def: tsetdef; var node: tnode);
  1905. begin
  1906. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1907. basenode:=nil;
  1908. node:=nil;
  1909. end;
  1910. procedure tnodetreetypedconstbuilder.tc_emit_enumdef(def: tenumdef; var node: tnode);
  1911. begin
  1912. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1913. basenode:=nil;
  1914. node:=nil;
  1915. end;
  1916. procedure tnodetreetypedconstbuilder.tc_emit_stringdef(def: tstringdef; var node: tnode);
  1917. begin
  1918. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1919. basenode:=nil;
  1920. node:=nil;
  1921. end;
  1922. constructor tnodetreetypedconstbuilder.create(sym: tstaticvarsym; previnit: tnode);
  1923. begin
  1924. inherited create(sym);
  1925. basenode:=cloadnode.create(sym,sym.owner);
  1926. resultblock:=internalstatements(statmnt);
  1927. if assigned(previnit) then
  1928. addstatement(statmnt,previnit);
  1929. end;
  1930. destructor tnodetreetypedconstbuilder.destroy;
  1931. begin
  1932. freeandnil(basenode);
  1933. freeandnil(resultblock);
  1934. inherited destroy;
  1935. end;
  1936. function tnodetreetypedconstbuilder.parse_into_nodetree: tnode;
  1937. begin
  1938. read_typed_const_data(tcsym.vardef);
  1939. result:=self.resultblock;
  1940. self.resultblock:=nil;
  1941. end;
  1942. begin
  1943. { default to asmlist version, best for most targets }
  1944. ctypedconstbuilder:=tasmlisttypedconstbuilder;
  1945. end.