ngtcon.pas 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  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)))
  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. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)))
  545. else
  546. do_error;
  547. end;
  548. pasbool16,
  549. bool16bit :
  550. begin
  551. if is_constboolnode(node) then
  552. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)))
  553. else
  554. do_error;
  555. end;
  556. pasbool32,
  557. bool32bit :
  558. begin
  559. if is_constboolnode(node) then
  560. list.concat(Tai_const.Create_32bit(longint(tordconstnode(node).value.svalue)))
  561. else
  562. do_error;
  563. end;
  564. pasbool64,
  565. bool64bit :
  566. begin
  567. if is_constboolnode(node) then
  568. list.concat(Tai_const.Create_64bit(int64(tordconstnode(node).value.svalue)))
  569. else
  570. do_error;
  571. end;
  572. uchar :
  573. begin
  574. if is_constwidecharnode(node) then
  575. inserttypeconv(node,cansichartype);
  576. if is_constcharnode(node) or
  577. ((m_delphi in current_settings.modeswitches) and
  578. is_constwidecharnode(node) and
  579. (tordconstnode(node).value <= 255)) then
  580. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)))
  581. else
  582. do_error;
  583. end;
  584. uwidechar :
  585. begin
  586. if is_constcharnode(node) then
  587. inserttypeconv(node,cwidechartype);
  588. if is_constwidecharnode(node) then
  589. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)))
  590. else
  591. do_error;
  592. end;
  593. s8bit,u8bit,
  594. u16bit,s16bit,
  595. s32bit,u32bit,
  596. s64bit,u64bit :
  597. begin
  598. if is_constintnode(node) then
  599. begin
  600. testrange(def,tordconstnode(node).value,false,false);
  601. case def.size of
  602. 1 :
  603. list.concat(Tai_const.Create_8bit(byte(tordconstnode(node).value.svalue)));
  604. 2 :
  605. list.concat(Tai_const.Create_16bit(word(tordconstnode(node).value.svalue)));
  606. 4 :
  607. list.concat(Tai_const.Create_32bit(longint(tordconstnode(node).value.svalue)));
  608. 8 :
  609. list.concat(Tai_const.Create_64bit(tordconstnode(node).value.svalue));
  610. end;
  611. end
  612. else
  613. do_error;
  614. end;
  615. scurrency:
  616. begin
  617. if is_constintnode(node) then
  618. intvalue:=tordconstnode(node).value*10000
  619. { allow bootstrapping }
  620. else if is_constrealnode(node) then
  621. intvalue:=PInt64(@trealconstnode(node).value_currency)^
  622. else
  623. begin
  624. intvalue:=0;
  625. IncompatibleTypes(node.resultdef, def);
  626. end;
  627. list.concat(Tai_const.Create_64bit(intvalue));
  628. end;
  629. else
  630. internalerror(200611052);
  631. end;
  632. end;
  633. procedure tasmlisttypedconstbuilder.tc_emit_floatdef(def: tfloatdef; var node: tnode);
  634. var
  635. value : bestreal;
  636. begin
  637. if is_constrealnode(node) then
  638. value:=trealconstnode(node).value_real
  639. else if is_constintnode(node) then
  640. value:=tordconstnode(node).value
  641. else if is_constnode(node) then
  642. IncompatibleTypes(node.resultdef, def)
  643. else
  644. Message(parser_e_illegal_expression);
  645. case def.floattype of
  646. s32real :
  647. list.concat(Tai_real_32bit.Create(ts32real(value)));
  648. s64real :
  649. {$ifdef ARM}
  650. if is_double_hilo_swapped then
  651. list.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  652. else
  653. {$endif ARM}
  654. list.concat(Tai_real_64bit.Create(ts64real(value)));
  655. s80real :
  656. list.concat(Tai_real_80bit.Create(value,s80floattype.size));
  657. sc80real :
  658. list.concat(Tai_real_80bit.Create(value,sc80floattype.size));
  659. s64comp :
  660. { the round is necessary for native compilers where comp isn't a float }
  661. list.concat(Tai_comp_64bit.Create(round(value)));
  662. s64currency:
  663. list.concat(Tai_comp_64bit.Create(round(value*10000)));
  664. s128real:
  665. list.concat(Tai_real_128bit.Create(value));
  666. else
  667. internalerror(200611053);
  668. end;
  669. end;
  670. procedure tasmlisttypedconstbuilder.tc_emit_classrefdef(def: tclassrefdef; var node: tnode);
  671. begin
  672. case node.nodetype of
  673. loadvmtaddrn:
  674. begin
  675. if not Tobjectdef(tclassrefdef(node.resultdef).pointeddef).is_related(tobjectdef(def.pointeddef)) then
  676. IncompatibleTypes(node.resultdef, def);
  677. list.concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol(Tobjectdef(tclassrefdef(node.resultdef).pointeddef).vmt_mangledname)));
  678. end;
  679. niln:
  680. list.concat(Tai_const.Create_sym(nil));
  681. else if is_constnode(node) then
  682. IncompatibleTypes(node.resultdef, def)
  683. else
  684. Message(parser_e_illegal_expression);
  685. end;
  686. end;
  687. procedure tasmlisttypedconstbuilder.tc_emit_pointerdef(def: tpointerdef; var node: tnode);
  688. var
  689. hp : tnode;
  690. srsym : tsym;
  691. pd : tprocdef;
  692. ca : pchar;
  693. pw : pcompilerwidestring;
  694. i,len : longint;
  695. base,
  696. offset : aint;
  697. v : Tconstexprint;
  698. ll : tasmlabel;
  699. varalign : shortint;
  700. begin
  701. { remove equal typecasts for pointer/nil addresses }
  702. if (node.nodetype=typeconvn) then
  703. with Ttypeconvnode(node) do
  704. if (left.nodetype in [addrn,niln]) and equal_defs(def,node.resultdef) then
  705. begin
  706. hp:=left;
  707. left:=nil;
  708. node.free;
  709. node:=hp;
  710. end;
  711. { allows horrible ofs(typeof(TButton)^) code !! }
  712. if (node.nodetype=addrn) then
  713. with Taddrnode(node) do
  714. if left.nodetype=derefn then
  715. begin
  716. hp:=tderefnode(left).left;
  717. tderefnode(left).left:=nil;
  718. node.free;
  719. node:=hp;
  720. end;
  721. { const pointer ? }
  722. if (node.nodetype = pointerconstn) then
  723. begin
  724. {$if sizeof(TConstPtrUInt)=8}
  725. list.concat(Tai_const.Create_64bit(int64(tpointerconstnode(node).value)));
  726. {$else}
  727. {$if sizeof(TConstPtrUInt)=4}
  728. list.concat(Tai_const.Create_32bit(longint(tpointerconstnode(node).value)));
  729. {$else}
  730. internalerror(200404122);
  731. {$endif} {$endif}
  732. end
  733. { nil pointer ? }
  734. else if node.nodetype=niln then
  735. list.concat(Tai_const.Create_sym(nil))
  736. { maybe pchar ? }
  737. else
  738. if is_char(def.pointeddef) and
  739. (node.nodetype<>addrn) then
  740. begin
  741. current_asmdata.getdatalabel(ll);
  742. list.concat(Tai_const.Create_sym(ll));
  743. if node.nodetype=stringconstn then
  744. varalign:=size_2_align(tstringconstnode(node).len)
  745. else
  746. varalign:=0;
  747. varalign:=const_align(varalign);
  748. new_section(current_asmdata.asmlists[al_const], sec_rodata, ll.name, varalign);
  749. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  750. if node.nodetype=stringconstn then
  751. begin
  752. len:=tstringconstnode(node).len;
  753. { For tp7 the maximum lentgh can be 255 }
  754. if (m_tp7 in current_settings.modeswitches) and
  755. (len>255) then
  756. len:=255;
  757. getmem(ca,len+2);
  758. move(tstringconstnode(node).value_str^,ca^,len+1);
  759. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,len+1));
  760. end
  761. else
  762. if is_constcharnode(node) then
  763. current_asmdata.asmlists[al_const].concat(Tai_string.Create(char(byte(tordconstnode(node).value.svalue))+#0))
  764. else
  765. IncompatibleTypes(node.resultdef, def);
  766. end
  767. { maybe pwidechar ? }
  768. else
  769. if is_widechar(def.pointeddef) and
  770. (node.nodetype<>addrn) then
  771. begin
  772. current_asmdata.getdatalabel(ll);
  773. list.concat(Tai_const.Create_sym(ll));
  774. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(sizeof(pint))));
  775. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(ll));
  776. if (node.nodetype in [stringconstn,ordconstn]) then
  777. begin
  778. { convert to unicodestring stringconstn }
  779. inserttypeconv(node,cunicodestringtype);
  780. if (node.nodetype=stringconstn) and
  781. (tstringconstnode(node).cst_type in [cst_widestring,cst_unicodestring]) then
  782. begin
  783. pw:=pcompilerwidestring(tstringconstnode(node).value_str);
  784. for i:=0 to tstringconstnode(node).len-1 do
  785. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(pw^.data[i]));
  786. { ending #0 }
  787. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(0))
  788. end;
  789. end
  790. else
  791. IncompatibleTypes(node.resultdef, def);
  792. end
  793. else
  794. if (node.nodetype=addrn) or
  795. is_proc2procvar_load(node,pd) then
  796. begin
  797. { insert typeconv }
  798. inserttypeconv(node,def);
  799. hp:=node;
  800. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  801. hp:=tunarynode(hp).left;
  802. if (hp.nodetype=loadn) then
  803. begin
  804. hp:=node;
  805. offset:=0;
  806. while assigned(hp) and (hp.nodetype<>loadn) do
  807. begin
  808. case hp.nodetype of
  809. vecn :
  810. begin
  811. case tvecnode(hp).left.resultdef.typ of
  812. stringdef :
  813. begin
  814. { this seems OK for shortstring and ansistrings PM }
  815. { it is wrong for widestrings !! }
  816. len:=1;
  817. base:=0;
  818. end;
  819. arraydef :
  820. begin
  821. if not is_packed_array(tvecnode(hp).left.resultdef) then
  822. begin
  823. len:=tarraydef(tvecnode(hp).left.resultdef).elesize;
  824. base:=tarraydef(tvecnode(hp).left.resultdef).lowrange;
  825. end
  826. else
  827. begin
  828. Message(parser_e_packed_dynamic_open_array);
  829. len:=1;
  830. base:=0;
  831. end;
  832. end
  833. else
  834. Message(parser_e_illegal_expression);
  835. end;
  836. if is_constintnode(tvecnode(hp).right) then
  837. begin
  838. {Prevent overflow.}
  839. v:=get_ordinal_value(tvecnode(hp).right)-base;
  840. if (v<int64(low(offset))) or (v>int64(high(offset))) then
  841. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(offset)),tostr(high(offset)));
  842. if high(offset)-offset div len>v then
  843. inc(offset,len*v.svalue)
  844. else
  845. message3(type_e_range_check_error_bounds,tostr(v),'0',tostr(high(offset)-offset div len))
  846. end
  847. else
  848. Message(parser_e_illegal_expression);
  849. end;
  850. subscriptn :
  851. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  852. typeconvn :
  853. begin
  854. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  855. Message(parser_e_illegal_expression);
  856. end;
  857. addrn :
  858. ;
  859. else
  860. Message(parser_e_illegal_expression);
  861. end;
  862. hp:=tunarynode(hp).left;
  863. end;
  864. srsym:=tloadnode(hp).symtableentry;
  865. case srsym.typ of
  866. procsym :
  867. begin
  868. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  869. if Tprocsym(srsym).ProcdefList.Count>1 then
  870. Message(parser_e_no_overloaded_procvars);
  871. if po_abstractmethod in pd.procoptions then
  872. Message(type_e_cant_take_address_of_abstract_method)
  873. else
  874. list.concat(Tai_const.Createname(pd.mangledname,offset));
  875. end;
  876. staticvarsym :
  877. list.concat(Tai_const.Createname(tstaticvarsym(srsym).mangledname,offset));
  878. labelsym :
  879. list.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,offset));
  880. constsym :
  881. if tconstsym(srsym).consttyp=constresourcestring then
  882. list.concat(Tai_const.Createname(make_mangledname('RESSTR',tconstsym(srsym).owner,tconstsym(srsym).name),sizeof(pint)))
  883. else
  884. Message(type_e_variable_id_expected);
  885. else
  886. Message(type_e_variable_id_expected);
  887. end;
  888. end
  889. else
  890. Message(parser_e_illegal_expression);
  891. end
  892. else
  893. { allow typeof(Object type)}
  894. if (node.nodetype=inlinen) and
  895. (tinlinenode(node).inlinenumber=in_typeof_x) then
  896. begin
  897. if (tinlinenode(node).left.nodetype=typen) then
  898. begin
  899. list.concat(Tai_const.createname(
  900. tobjectdef(tinlinenode(node).left.resultdef).vmt_mangledname,0));
  901. end
  902. else
  903. Message(parser_e_illegal_expression);
  904. end
  905. else
  906. Message(parser_e_illegal_expression);
  907. end;
  908. procedure tasmlisttypedconstbuilder.tc_emit_setdef(def: tsetdef; var node: tnode);
  909. type
  910. setbytes = array[0..31] of byte;
  911. Psetbytes = ^setbytes;
  912. var
  913. i: longint;
  914. begin
  915. if node.nodetype=setconstn then
  916. begin
  917. { be sure to convert to the correct result, else
  918. it can generate smallset data instead of normalset (PFV) }
  919. inserttypeconv(node,def);
  920. { we only allow const sets }
  921. if (node.nodetype<>setconstn) or
  922. assigned(tsetconstnode(node).left) then
  923. Message(parser_e_illegal_expression)
  924. else
  925. begin
  926. tsetconstnode(node).adjustforsetbase;
  927. { this writing is endian-dependant }
  928. if source_info.endian = target_info.endian then
  929. begin
  930. for i:=0 to node.resultdef.size-1 do
  931. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(node).value_set)^[i]));
  932. end
  933. else
  934. begin
  935. for i:=0 to node.resultdef.size-1 do
  936. list.concat(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(node).value_set)^[i])));
  937. end;
  938. end;
  939. end
  940. else
  941. Message(parser_e_illegal_expression);
  942. end;
  943. procedure tasmlisttypedconstbuilder.tc_emit_enumdef(def: tenumdef; var node: tnode);
  944. begin
  945. if node.nodetype=ordconstn then
  946. begin
  947. if equal_defs(node.resultdef,def) or
  948. is_subequal(node.resultdef,def) then
  949. begin
  950. case longint(node.resultdef.size) of
  951. 1 : list.concat(Tai_const.Create_8bit(Byte(tordconstnode(node).value.svalue)));
  952. 2 : list.concat(Tai_const.Create_16bit(Word(tordconstnode(node).value.svalue)));
  953. 4 : list.concat(Tai_const.Create_32bit(Longint(tordconstnode(node).value.svalue)));
  954. end;
  955. end
  956. else
  957. IncompatibleTypes(node.resultdef,def);
  958. end
  959. else
  960. Message(parser_e_illegal_expression);
  961. end;
  962. { parse a single constant and add it to the packed const info }
  963. { represented by curval etc (see explanation of bitpackval for }
  964. { what the different parameters mean) }
  965. function tasmlisttypedconstbuilder.parse_single_packed_const(def: tdef; var bp: tbitpackedval): boolean;
  966. var
  967. node: tnode;
  968. begin
  969. result:=true;
  970. node:=comp_expr(true,false);
  971. if (node.nodetype <> ordconstn) or
  972. (not equal_defs(node.resultdef,def) and
  973. not is_subequal(node.resultdef,def)) then
  974. begin
  975. node.free;
  976. incompatibletypes(node.resultdef,def);
  977. consume_all_until(_SEMICOLON);
  978. result:=false;
  979. exit;
  980. end;
  981. if (Tordconstnode(node).value<qword(low(Aword))) or (Tordconstnode(node).value>qword(high(Aword))) then
  982. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(node).value),tostr(low(Aword)),tostr(high(Aword)))
  983. else
  984. bitpackval(Tordconstnode(node).value.uvalue,bp);
  985. if (bp.curbitoffset>=AIntBits) then
  986. flush_packed_value(list,bp);
  987. node.free;
  988. end;
  989. procedure tasmlisttypedconstbuilder.parse_arraydef(def:tarraydef);
  990. var
  991. n : tnode;
  992. i : longint;
  993. len : asizeint;
  994. ch : array[0..1] of char;
  995. ca : pbyte;
  996. int_const: tai_const;
  997. char_size: integer;
  998. oldoffset: asizeint;
  999. begin
  1000. { dynamic array nil }
  1001. if is_dynamic_array(def) then
  1002. begin
  1003. { Only allow nil initialization }
  1004. consume(_NIL);
  1005. list.concat(Tai_const.Create_sym(nil));
  1006. end
  1007. { packed array constant }
  1008. else if is_packed_array(def) and
  1009. ((def.elepackedbitsize mod 8 <> 0) or
  1010. not ispowerof2(def.elepackedbitsize div 8,i)) then
  1011. begin
  1012. parse_packed_array_def(def);
  1013. end
  1014. { normal array const between brackets }
  1015. else if try_to_consume(_LKLAMMER) then
  1016. begin
  1017. oldoffset:=curoffset;
  1018. curoffset:=0;
  1019. for i:=def.lowrange to def.highrange-1 do
  1020. begin
  1021. read_typed_const_data(def.elementdef);
  1022. Inc(curoffset,def.elementdef.size);
  1023. if token=_RKLAMMER then
  1024. begin
  1025. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  1026. consume(_RKLAMMER);
  1027. exit;
  1028. end
  1029. else
  1030. consume(_COMMA);
  1031. end;
  1032. read_typed_const_data(def.elementdef);
  1033. consume(_RKLAMMER);
  1034. curoffset:=oldoffset;
  1035. end
  1036. { if array of char then we allow also a string }
  1037. else if is_anychar(def.elementdef) then
  1038. begin
  1039. char_size:=def.elementdef.size;
  1040. n:=comp_expr(true,false);
  1041. if n.nodetype=stringconstn then
  1042. begin
  1043. len:=tstringconstnode(n).len;
  1044. case char_size of
  1045. 1:
  1046. begin
  1047. if (tstringconstnode(n).cst_type in [cst_unicodestring,cst_widestring]) then
  1048. inserttypeconv(n,getansistringdef);
  1049. if n.nodetype<>stringconstn then
  1050. internalerror(2010033003);
  1051. ca:=pointer(tstringconstnode(n).value_str);
  1052. end;
  1053. 2:
  1054. begin
  1055. inserttypeconv(n,cunicodestringtype);
  1056. if n.nodetype<>stringconstn then
  1057. internalerror(2010033003);
  1058. ca:=pointer(pcompilerwidestring(tstringconstnode(n).value_str)^.data)
  1059. end;
  1060. else
  1061. internalerror(2010033005);
  1062. end;
  1063. { For tp7 the maximum lentgh can be 255 }
  1064. if (m_tp7 in current_settings.modeswitches) and
  1065. (len>255) then
  1066. len:=255;
  1067. end
  1068. else if is_constcharnode(n) then
  1069. begin
  1070. case char_size of
  1071. 1:
  1072. ch[0]:=chr(tordconstnode(n).value.uvalue and $ff);
  1073. 2:
  1074. begin
  1075. inserttypeconv(n,cwidechartype);
  1076. if not is_constwidecharnode(n) then
  1077. internalerror(2010033001);
  1078. widechar(ch):=widechar(tordconstnode(n).value.uvalue and $ffff);
  1079. end;
  1080. else
  1081. internalerror(2010033002);
  1082. end;
  1083. ca:=@ch;
  1084. len:=1;
  1085. end
  1086. else if is_constwidecharnode(n) and (current_settings.sourcecodepage<>CP_UTF8) then
  1087. begin
  1088. case char_size of
  1089. 1:
  1090. begin
  1091. inserttypeconv(n,cansichartype);
  1092. if not is_constcharnode(n) then
  1093. internalerror(2010033001);
  1094. ch[0]:=chr(tordconstnode(n).value.uvalue and $ff);
  1095. end;
  1096. 2:
  1097. widechar(ch):=widechar(tordconstnode(n).value.uvalue and $ffff);
  1098. else
  1099. internalerror(2010033002);
  1100. end;
  1101. ca:=@ch;
  1102. len:=1;
  1103. end
  1104. else
  1105. begin
  1106. Message(parser_e_illegal_expression);
  1107. len:=0;
  1108. end;
  1109. if len>(def.highrange-def.lowrange+1) then
  1110. Message(parser_e_string_larger_array);
  1111. for i:=0 to def.highrange-def.lowrange do
  1112. begin
  1113. if i<len then
  1114. begin
  1115. case char_size of
  1116. 1:
  1117. int_const:=Tai_const.Create_char(char_size,pbyte(ca)^);
  1118. 2:
  1119. int_const:=Tai_const.Create_char(char_size,pword(ca)^);
  1120. else
  1121. internalerror(2010033004);
  1122. end;
  1123. inc(ca, char_size);
  1124. end
  1125. else
  1126. {Fill the remaining positions with #0.}
  1127. int_const:=Tai_const.Create_char(char_size,0);
  1128. list.concat(int_const)
  1129. end;
  1130. n.free;
  1131. end
  1132. else
  1133. begin
  1134. { we want the ( }
  1135. consume(_LKLAMMER);
  1136. end;
  1137. end;
  1138. procedure tasmlisttypedconstbuilder.parse_procvardef(def:tprocvardef);
  1139. var
  1140. tmpn,n : tnode;
  1141. pd : tprocdef;
  1142. begin
  1143. { Procvars and pointers are no longer compatible. }
  1144. { under tp: =nil or =var under fpc: =nil or =@var }
  1145. if try_to_consume(_NIL) then
  1146. begin
  1147. list.concat(Tai_const.Create_sym(nil));
  1148. if not def.is_addressonly then
  1149. list.concat(Tai_const.Create_sym(nil));
  1150. exit;
  1151. end;
  1152. { you can't assign a value other than NIL to a typed constant }
  1153. { which is a "procedure of object", because this also requires }
  1154. { address of an object/class instance, which is not known at }
  1155. { compile time (JM) }
  1156. if (po_methodpointer in def.procoptions) then
  1157. Message(parser_e_no_procvarobj_const);
  1158. { parse the rest too, so we can continue with error checking }
  1159. getprocvardef:=def;
  1160. n:=comp_expr(true,false);
  1161. getprocvardef:=nil;
  1162. if codegenerror then
  1163. begin
  1164. n.free;
  1165. exit;
  1166. end;
  1167. { let type conversion check everything needed }
  1168. inserttypeconv(n,def);
  1169. if codegenerror then
  1170. begin
  1171. n.free;
  1172. exit;
  1173. end;
  1174. { remove typeconvs, that will normally insert a lea
  1175. instruction which is not necessary for us }
  1176. while n.nodetype=typeconvn do
  1177. begin
  1178. tmpn:=ttypeconvnode(n).left;
  1179. ttypeconvnode(n).left:=nil;
  1180. n.free;
  1181. n:=tmpn;
  1182. end;
  1183. { remove addrn which we also don't need here }
  1184. if n.nodetype=addrn then
  1185. begin
  1186. tmpn:=taddrnode(n).left;
  1187. taddrnode(n).left:=nil;
  1188. n.free;
  1189. n:=tmpn;
  1190. end;
  1191. { we now need to have a loadn with a procsym }
  1192. if (n.nodetype=loadn) and
  1193. (tloadnode(n).symtableentry.typ=procsym) then
  1194. begin
  1195. pd:=tloadnode(n).procdef;
  1196. list.concat(Tai_const.createname(pd.mangledname,0));
  1197. { nested procvar typed consts can only be initialised with nil
  1198. (checked above) or with a global procedure (checked here),
  1199. because in other cases we need a valid frame pointer }
  1200. if is_nested_pd(def) then
  1201. begin
  1202. if is_nested_pd(pd) then
  1203. Message(parser_e_no_procvarnested_const);
  1204. list.concat(Tai_const.Create_sym(nil));
  1205. end
  1206. end
  1207. else
  1208. Message(parser_e_illegal_expression);
  1209. n.free;
  1210. end;
  1211. procedure tasmlisttypedconstbuilder.parse_recorddef(def:trecorddef);
  1212. var
  1213. n : tnode;
  1214. symidx : longint;
  1215. recsym,
  1216. srsym : tsym;
  1217. hs : string;
  1218. sorg,s : TIDString;
  1219. tmpguid : tguid;
  1220. recoffset,
  1221. fillbytes : aint;
  1222. bp : tbitpackedval;
  1223. error,
  1224. is_packed: boolean;
  1225. startoffset: aint;
  1226. procedure handle_stringconstn;
  1227. var
  1228. i : longint;
  1229. begin
  1230. hs:=strpas(tstringconstnode(n).value_str);
  1231. if string2guid(hs,tmpguid) then
  1232. begin
  1233. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  1234. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  1235. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  1236. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1237. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  1238. end
  1239. else
  1240. Message(parser_e_improper_guid_syntax);
  1241. end;
  1242. var
  1243. i : longint;
  1244. SymList:TFPHashObjectList;
  1245. begin
  1246. { GUID }
  1247. if (def=rec_tguid) and (token=_ID) then
  1248. begin
  1249. n:=comp_expr(true,false);
  1250. if n.nodetype=stringconstn then
  1251. handle_stringconstn
  1252. else
  1253. begin
  1254. inserttypeconv(n,rec_tguid);
  1255. if n.nodetype=guidconstn then
  1256. begin
  1257. tmpguid:=tguidconstnode(n).value;
  1258. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  1259. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  1260. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  1261. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1262. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  1263. end
  1264. else
  1265. Message(parser_e_illegal_expression);
  1266. end;
  1267. n.free;
  1268. exit;
  1269. end;
  1270. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1271. begin
  1272. n:=comp_expr(true,false);
  1273. inserttypeconv(n,cshortstringtype);
  1274. if n.nodetype=stringconstn then
  1275. handle_stringconstn
  1276. else
  1277. Message(parser_e_illegal_expression);
  1278. n.free;
  1279. exit;
  1280. end;
  1281. { bitpacked record? }
  1282. is_packed:=is_packed_record_or_object(def);
  1283. if (is_packed) then
  1284. begin
  1285. { loadbitsize = 8, bitpacked records are always padded to }
  1286. { a multiple of a byte. packedbitsize will be set separately }
  1287. { for each field }
  1288. initbitpackval(bp,0);
  1289. bp.loadbitsize:=8;
  1290. end;
  1291. { normal record }
  1292. consume(_LKLAMMER);
  1293. recoffset:=0;
  1294. sorg:='';
  1295. symidx:=0;
  1296. symlist:=def.symtable.SymList;
  1297. srsym:=get_next_varsym(def,symlist,symidx);
  1298. recsym := nil;
  1299. startoffset:=curoffset;
  1300. while token<>_RKLAMMER do
  1301. begin
  1302. s:=pattern;
  1303. sorg:=orgpattern;
  1304. consume(_ID);
  1305. consume(_COLON);
  1306. error := false;
  1307. recsym := tsym(def.symtable.Find(s));
  1308. if not assigned(recsym) then
  1309. begin
  1310. Message1(sym_e_illegal_field,sorg);
  1311. error := true;
  1312. end;
  1313. if (not error) and
  1314. (not assigned(srsym) or
  1315. (s <> srsym.name)) then
  1316. { possible variant record (JM) }
  1317. begin
  1318. { All parts of a variant start at the same offset }
  1319. { Also allow jumping from one variant part to another, }
  1320. { as long as the offsets match }
  1321. if (assigned(srsym) and
  1322. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1323. { srsym is not assigned after parsing w2 in the }
  1324. { typed const in the next example: }
  1325. { type tr = record case byte of }
  1326. { 1: (l1,l2: dword); }
  1327. { 2: (w1,w2: word); }
  1328. { end; }
  1329. { const r: tr = (w1:1;w2:1;l2:5); }
  1330. (tfieldvarsym(recsym).fieldoffset = recoffset) then
  1331. begin
  1332. srsym:=recsym;
  1333. { symidx should contain the next symbol id to search }
  1334. symidx:=SymList.indexof(srsym)+1;
  1335. end
  1336. { going backwards isn't allowed in any mode }
  1337. else if (tfieldvarsym(recsym).fieldoffset<recoffset) then
  1338. begin
  1339. Message(parser_e_invalid_record_const);
  1340. error := true;
  1341. end
  1342. { Delphi allows you to skip fields }
  1343. else if (m_delphi in current_settings.modeswitches) then
  1344. begin
  1345. Message1(parser_w_skipped_fields_before,sorg);
  1346. srsym := recsym;
  1347. end
  1348. { FPC and TP don't }
  1349. else
  1350. begin
  1351. Message1(parser_e_skipped_fields_before,sorg);
  1352. error := true;
  1353. end;
  1354. end;
  1355. if error then
  1356. consume_all_until(_SEMICOLON)
  1357. else
  1358. begin
  1359. { if needed fill (alignment) }
  1360. if tfieldvarsym(srsym).fieldoffset>recoffset then
  1361. begin
  1362. if not(is_packed) then
  1363. fillbytes:=tfieldvarsym(srsym).fieldoffset-recoffset
  1364. else
  1365. begin
  1366. flush_packed_value(list,bp);
  1367. { curoffset is now aligned to the next byte }
  1368. recoffset:=align(recoffset,8);
  1369. { offsets are in bits in this case }
  1370. fillbytes:=(tfieldvarsym(srsym).fieldoffset-recoffset) div 8;
  1371. end;
  1372. for i:=1 to fillbytes do
  1373. list.concat(Tai_const.Create_8bit(0))
  1374. end;
  1375. { new position }
  1376. recoffset:=tfieldvarsym(srsym).fieldoffset;
  1377. if not(is_packed) then
  1378. inc(recoffset,tfieldvarsym(srsym).vardef.size)
  1379. else
  1380. inc(recoffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1381. { read the data }
  1382. if not(is_packed) or
  1383. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1384. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1385. begin
  1386. if is_packed then
  1387. begin
  1388. flush_packed_value(list,bp);
  1389. recoffset:=align(recoffset,8);
  1390. end;
  1391. curoffset:=startoffset+tfieldvarsym(srsym).fieldoffset;
  1392. read_typed_const_data(tfieldvarsym(srsym).vardef);
  1393. end
  1394. else
  1395. begin
  1396. bp.packedbitsize:=tfieldvarsym(srsym).vardef.packedbitsize;
  1397. parse_single_packed_const(tfieldvarsym(srsym).vardef,bp);
  1398. end;
  1399. { keep previous field for checking whether whole }
  1400. { record was initialized (JM) }
  1401. recsym := srsym;
  1402. { goto next field }
  1403. srsym:=get_next_varsym(def,SymList,symidx);
  1404. if token=_SEMICOLON then
  1405. consume(_SEMICOLON)
  1406. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1407. consume(_COMMA)
  1408. else
  1409. break;
  1410. end;
  1411. end;
  1412. curoffset:=startoffset;
  1413. { are there any fields left, but don't complain if there only
  1414. come other variant parts after the last initialized field }
  1415. if assigned(srsym) and
  1416. (
  1417. (recsym=nil) or
  1418. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1419. ) then
  1420. Message1(parser_w_skipped_fields_after,sorg);
  1421. if not(is_packed) then
  1422. fillbytes:=def.size-recoffset
  1423. else
  1424. begin
  1425. flush_packed_value(list,bp);
  1426. recoffset:=align(recoffset,8);
  1427. fillbytes:=def.size-(recoffset div 8);
  1428. end;
  1429. for i:=1 to fillbytes do
  1430. list.concat(Tai_const.Create_8bit(0));
  1431. consume(_RKLAMMER);
  1432. end;
  1433. procedure tasmlisttypedconstbuilder.parse_objectdef(def:tobjectdef);
  1434. var
  1435. n : tnode;
  1436. i : longint;
  1437. obj : tobjectdef;
  1438. srsym : tsym;
  1439. st : tsymtable;
  1440. objoffset : aint;
  1441. s,sorg : TIDString;
  1442. vmtwritten : boolean;
  1443. startoffset:aint;
  1444. begin
  1445. { no support for packed object }
  1446. if is_packed_record_or_object(def) then
  1447. begin
  1448. Message(type_e_no_const_packed_record);
  1449. exit;
  1450. end;
  1451. { only allow nil for implicit pointer object types }
  1452. if is_implicit_pointer_object_type(def) then
  1453. begin
  1454. n:=comp_expr(true,false);
  1455. if n.nodetype<>niln then
  1456. begin
  1457. Message(parser_e_type_const_not_possible);
  1458. consume_all_until(_SEMICOLON);
  1459. end
  1460. else
  1461. list.concat(Tai_const.Create_sym(nil));
  1462. n.free;
  1463. exit;
  1464. end;
  1465. { for objects we allow it only if it doesn't contain a vmt }
  1466. if (oo_has_vmt in def.objectoptions) and
  1467. (m_fpc in current_settings.modeswitches) then
  1468. begin
  1469. Message(parser_e_type_object_constants);
  1470. exit;
  1471. end;
  1472. consume(_LKLAMMER);
  1473. startoffset:=curoffset;
  1474. objoffset:=0;
  1475. vmtwritten:=false;
  1476. while token<>_RKLAMMER do
  1477. begin
  1478. s:=pattern;
  1479. sorg:=orgpattern;
  1480. consume(_ID);
  1481. consume(_COLON);
  1482. srsym:=nil;
  1483. obj:=tobjectdef(def);
  1484. st:=obj.symtable;
  1485. while (srsym=nil) and assigned(st) do
  1486. begin
  1487. srsym:=tsym(st.Find(s));
  1488. if assigned(obj) then
  1489. obj:=obj.childof;
  1490. if assigned(obj) then
  1491. st:=obj.symtable
  1492. else
  1493. st:=nil;
  1494. end;
  1495. if (srsym=nil) or
  1496. (srsym.typ<>fieldvarsym) then
  1497. begin
  1498. if (srsym=nil) then
  1499. Message1(sym_e_id_not_found,sorg)
  1500. else
  1501. Message1(sym_e_illegal_field,sorg);
  1502. consume_all_until(_RKLAMMER);
  1503. break;
  1504. end
  1505. else
  1506. with tfieldvarsym(srsym) do
  1507. begin
  1508. { check position }
  1509. if fieldoffset<objoffset then
  1510. message(parser_e_invalid_record_const);
  1511. { check in VMT needs to be added for TP mode }
  1512. if not(vmtwritten) and
  1513. not(m_fpc in current_settings.modeswitches) and
  1514. (oo_has_vmt in def.objectoptions) and
  1515. (def.vmt_offset<fieldoffset) then
  1516. begin
  1517. for i:=1 to def.vmt_offset-objoffset do
  1518. list.concat(tai_const.create_8bit(0));
  1519. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1520. { this is more general }
  1521. objoffset:=def.vmt_offset + sizeof(pint);
  1522. vmtwritten:=true;
  1523. end;
  1524. { if needed fill }
  1525. if fieldoffset>objoffset then
  1526. for i:=1 to fieldoffset-objoffset do
  1527. list.concat(Tai_const.Create_8bit(0));
  1528. { new position }
  1529. objoffset:=fieldoffset+vardef.size;
  1530. { read the data }
  1531. curoffset:=startoffset+fieldoffset;
  1532. read_typed_const_data(vardef);
  1533. if not try_to_consume(_SEMICOLON) then
  1534. break;
  1535. end;
  1536. end;
  1537. curoffset:=startoffset;
  1538. if not(m_fpc in current_settings.modeswitches) and
  1539. (oo_has_vmt in def.objectoptions) and
  1540. (def.vmt_offset>=objoffset) then
  1541. begin
  1542. for i:=1 to def.vmt_offset-objoffset do
  1543. list.concat(tai_const.create_8bit(0));
  1544. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1545. { this is more general }
  1546. objoffset:=def.vmt_offset + sizeof(pint);
  1547. end;
  1548. for i:=1 to def.size-objoffset do
  1549. list.concat(Tai_const.Create_8bit(0));
  1550. consume(_RKLAMMER);
  1551. end;
  1552. function tasmlisttypedconstbuilder.parse_into_asmlist: tasmlist;
  1553. begin
  1554. read_typed_const_data(tcsym.vardef);
  1555. result:=list;
  1556. end;
  1557. { tnodetreetypedconstbuilder }
  1558. procedure tnodetreetypedconstbuilder.parse_arraydef(def: tarraydef);
  1559. var
  1560. n : tnode;
  1561. i : longint;
  1562. orgbase: tnode;
  1563. begin
  1564. { dynamic array nil }
  1565. if is_dynamic_array(def) then
  1566. begin
  1567. { Only allow nil initialization }
  1568. consume(_NIL);
  1569. addstatement(statmnt,cassignmentnode.create_internal(basenode,cnilnode.create));
  1570. basenode:=nil;
  1571. end
  1572. { array const between brackets }
  1573. else if try_to_consume(_LKLAMMER) then
  1574. begin
  1575. orgbase:=basenode;
  1576. for i:=def.lowrange to def.highrange-1 do
  1577. begin
  1578. basenode:=cvecnode.create(orgbase.getcopy,ctypeconvnode.create_explicit(genintconstnode(i),tarraydef(def).rangedef));
  1579. read_typed_const_data(def.elementdef);
  1580. if token=_RKLAMMER then
  1581. begin
  1582. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  1583. consume(_RKLAMMER);
  1584. exit;
  1585. end
  1586. else
  1587. consume(_COMMA);
  1588. end;
  1589. basenode:=cvecnode.create(orgbase,ctypeconvnode.create_explicit(genintconstnode(def.highrange),tarraydef(def).rangedef));
  1590. read_typed_const_data(def.elementdef);
  1591. consume(_RKLAMMER);
  1592. end
  1593. { if array of char then we allow also a string }
  1594. else if is_anychar(def.elementdef) then
  1595. begin
  1596. n:=comp_expr(true,false);
  1597. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1598. basenode:=nil;
  1599. end
  1600. else
  1601. begin
  1602. { we want the ( }
  1603. consume(_LKLAMMER);
  1604. end;
  1605. end;
  1606. procedure tnodetreetypedconstbuilder.parse_procvardef(def: tprocvardef);
  1607. begin
  1608. addstatement(statmnt,cassignmentnode.create_internal(basenode,comp_expr(true,false)));
  1609. basenode:=nil;
  1610. end;
  1611. procedure tnodetreetypedconstbuilder.parse_recorddef(def: trecorddef);
  1612. var
  1613. n,n2 : tnode;
  1614. SymList:TFPHashObjectList;
  1615. orgbasenode : tnode;
  1616. symidx : longint;
  1617. recsym,
  1618. srsym : tsym;
  1619. sorg,s : TIDString;
  1620. recoffset : aint;
  1621. error,
  1622. is_packed: boolean;
  1623. procedure handle_stringconstn;
  1624. begin
  1625. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1626. basenode:=nil;
  1627. n:=nil;
  1628. end;
  1629. begin
  1630. { GUID }
  1631. if (def=rec_tguid) and (token=_ID) then
  1632. begin
  1633. n:=comp_expr(true,false);
  1634. if n.nodetype=stringconstn then
  1635. handle_stringconstn
  1636. else
  1637. begin
  1638. inserttypeconv(n,rec_tguid);
  1639. if n.nodetype=guidconstn then
  1640. begin
  1641. n2:=cstringconstnode.createstr(guid2string(tguidconstnode(n).value));
  1642. n.free;
  1643. n:=n2;
  1644. handle_stringconstn;
  1645. end
  1646. else
  1647. Message(parser_e_illegal_expression);
  1648. end;
  1649. n.free;
  1650. exit;
  1651. end;
  1652. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1653. begin
  1654. n:=comp_expr(true,false);
  1655. inserttypeconv(n,cshortstringtype);
  1656. if n.nodetype=stringconstn then
  1657. handle_stringconstn
  1658. else
  1659. Message(parser_e_illegal_expression);
  1660. n.free;
  1661. exit;
  1662. end;
  1663. { bitpacked record? }
  1664. is_packed:=is_packed_record_or_object(def);
  1665. { normal record }
  1666. consume(_LKLAMMER);
  1667. recoffset:=0;
  1668. sorg:='';
  1669. symidx:=0;
  1670. symlist:=def.symtable.SymList;
  1671. srsym:=get_next_varsym(def,symlist,symidx);
  1672. recsym := nil;
  1673. orgbasenode:=basenode;
  1674. basenode:=nil;
  1675. while token<>_RKLAMMER do
  1676. begin
  1677. s:=pattern;
  1678. sorg:=orgpattern;
  1679. consume(_ID);
  1680. consume(_COLON);
  1681. error := false;
  1682. recsym := tsym(def.symtable.Find(s));
  1683. if not assigned(recsym) then
  1684. begin
  1685. Message1(sym_e_illegal_field,sorg);
  1686. error := true;
  1687. end;
  1688. if (not error) and
  1689. (not assigned(srsym) or
  1690. (s <> srsym.name)) then
  1691. { possible variant record (JM) }
  1692. begin
  1693. { All parts of a variant start at the same offset }
  1694. { Also allow jumping from one variant part to another, }
  1695. { as long as the offsets match }
  1696. if (assigned(srsym) and
  1697. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1698. { srsym is not assigned after parsing w2 in the }
  1699. { typed const in the next example: }
  1700. { type tr = record case byte of }
  1701. { 1: (l1,l2: dword); }
  1702. { 2: (w1,w2: word); }
  1703. { end; }
  1704. { const r: tr = (w1:1;w2:1;l2:5); }
  1705. (tfieldvarsym(recsym).fieldoffset = recoffset) then
  1706. begin
  1707. srsym:=recsym;
  1708. { symidx should contain the next symbol id to search }
  1709. symidx:=SymList.indexof(srsym)+1;
  1710. end
  1711. { going backwards isn't allowed in any mode }
  1712. else if (tfieldvarsym(recsym).fieldoffset<recoffset) then
  1713. begin
  1714. Message(parser_e_invalid_record_const);
  1715. error := true;
  1716. end
  1717. { Delphi allows you to skip fields }
  1718. else if (m_delphi in current_settings.modeswitches) then
  1719. begin
  1720. Message1(parser_w_skipped_fields_before,sorg);
  1721. srsym := recsym;
  1722. end
  1723. { FPC and TP don't }
  1724. else
  1725. begin
  1726. Message1(parser_e_skipped_fields_before,sorg);
  1727. error := true;
  1728. end;
  1729. end;
  1730. if error then
  1731. consume_all_until(_SEMICOLON)
  1732. else
  1733. begin
  1734. { skipping fill bytes happens automatically, since we only
  1735. initialize the defined fields }
  1736. { new position }
  1737. recoffset:=tfieldvarsym(srsym).fieldoffset;
  1738. if not(is_packed) then
  1739. inc(recoffset,tfieldvarsym(srsym).vardef.size)
  1740. else
  1741. inc(recoffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1742. { read the data }
  1743. if is_packed and
  1744. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1745. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1746. recoffset:=align(recoffset,8);
  1747. basenode:=csubscriptnode.create(srsym,orgbasenode.getcopy);
  1748. read_typed_const_data(tfieldvarsym(srsym).vardef);
  1749. { keep previous field for checking whether whole }
  1750. { record was initialized (JM) }
  1751. recsym := srsym;
  1752. { goto next field }
  1753. srsym:=get_next_varsym(def,SymList,symidx);
  1754. if token=_SEMICOLON then
  1755. consume(_SEMICOLON)
  1756. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1757. consume(_COMMA)
  1758. else
  1759. break;
  1760. end;
  1761. end;
  1762. { are there any fields left, but don't complain if there only
  1763. come other variant parts after the last initialized field }
  1764. if assigned(srsym) and
  1765. (
  1766. (recsym=nil) or
  1767. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1768. ) then
  1769. Message1(parser_w_skipped_fields_after,sorg);
  1770. orgbasenode.free;
  1771. basenode:=nil;
  1772. consume(_RKLAMMER);
  1773. end;
  1774. procedure tnodetreetypedconstbuilder.parse_objectdef(def: tobjectdef);
  1775. var
  1776. n,
  1777. orgbasenode : tnode;
  1778. obj : tobjectdef;
  1779. srsym : tsym;
  1780. st : tsymtable;
  1781. objoffset : aint;
  1782. s,sorg : TIDString;
  1783. begin
  1784. { no support for packed object }
  1785. if is_packed_record_or_object(def) then
  1786. begin
  1787. Message(type_e_no_const_packed_record);
  1788. exit;
  1789. end;
  1790. { only allow nil for implicit pointer object types }
  1791. if is_implicit_pointer_object_type(def) then
  1792. begin
  1793. n:=comp_expr(true,false);
  1794. if n.nodetype<>niln then
  1795. begin
  1796. Message(parser_e_type_const_not_possible);
  1797. consume_all_until(_SEMICOLON);
  1798. end
  1799. else
  1800. begin
  1801. addstatement(statmnt,cassignmentnode.create_internal(basenode,n));
  1802. n:=nil;
  1803. basenode:=nil;
  1804. end;
  1805. n.free;
  1806. exit;
  1807. end;
  1808. { for objects we allow it only if it doesn't contain a vmt }
  1809. if (oo_has_vmt in def.objectoptions) and
  1810. (m_fpc in current_settings.modeswitches) then
  1811. begin
  1812. Message(parser_e_type_object_constants);
  1813. exit;
  1814. end;
  1815. consume(_LKLAMMER);
  1816. objoffset:=0;
  1817. orgbasenode:=basenode;
  1818. basenode:=nil;
  1819. while token<>_RKLAMMER do
  1820. begin
  1821. s:=pattern;
  1822. sorg:=orgpattern;
  1823. consume(_ID);
  1824. consume(_COLON);
  1825. srsym:=nil;
  1826. obj:=tobjectdef(def);
  1827. st:=obj.symtable;
  1828. while (srsym=nil) and assigned(st) do
  1829. begin
  1830. srsym:=tsym(st.Find(s));
  1831. if assigned(obj) then
  1832. obj:=obj.childof;
  1833. if assigned(obj) then
  1834. st:=obj.symtable
  1835. else
  1836. st:=nil;
  1837. end;
  1838. if (srsym=nil) or
  1839. (srsym.typ<>fieldvarsym) then
  1840. begin
  1841. if (srsym=nil) then
  1842. Message1(sym_e_id_not_found,sorg)
  1843. else
  1844. Message1(sym_e_illegal_field,sorg);
  1845. consume_all_until(_RKLAMMER);
  1846. break;
  1847. end
  1848. else
  1849. with tfieldvarsym(srsym) do
  1850. begin
  1851. { check position }
  1852. if fieldoffset<objoffset then
  1853. message(parser_e_invalid_record_const);
  1854. { new position }
  1855. objoffset:=fieldoffset+vardef.size;
  1856. { read the data }
  1857. basenode:=csubscriptnode.create(srsym,orgbasenode.getcopy);
  1858. read_typed_const_data(vardef);
  1859. if not try_to_consume(_SEMICOLON) then
  1860. break;
  1861. end;
  1862. end;
  1863. consume(_RKLAMMER);
  1864. end;
  1865. procedure tnodetreetypedconstbuilder.tc_emit_orddef(def: torddef; var node: tnode);
  1866. begin
  1867. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1868. basenode:=nil;
  1869. node:=nil;
  1870. end;
  1871. procedure tnodetreetypedconstbuilder.tc_emit_floatdef(def: tfloatdef; var node: tnode);
  1872. begin
  1873. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1874. basenode:=nil;
  1875. node:=nil;
  1876. end;
  1877. procedure tnodetreetypedconstbuilder.tc_emit_classrefdef(def: tclassrefdef; 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_pointerdef(def: tpointerdef; 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_setdef(def: tsetdef; 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_enumdef(def: tenumdef; 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_stringdef(def: tstringdef; var node: tnode);
  1902. begin
  1903. addstatement(statmnt,cassignmentnode.create_internal(basenode,node));
  1904. basenode:=nil;
  1905. node:=nil;
  1906. end;
  1907. constructor tnodetreetypedconstbuilder.create(sym: tstaticvarsym; previnit: tnode);
  1908. begin
  1909. inherited create(sym);
  1910. basenode:=cloadnode.create(sym,sym.owner);
  1911. resultblock:=internalstatements(statmnt);
  1912. if assigned(previnit) then
  1913. addstatement(statmnt,previnit);
  1914. end;
  1915. destructor tnodetreetypedconstbuilder.destroy;
  1916. begin
  1917. freeandnil(basenode);
  1918. freeandnil(resultblock);
  1919. inherited destroy;
  1920. end;
  1921. function tnodetreetypedconstbuilder.parse_into_nodetree: tnode;
  1922. begin
  1923. read_typed_const_data(tcsym.vardef);
  1924. result:=self.resultblock;
  1925. self.resultblock:=nil;
  1926. end;
  1927. begin
  1928. { default to asmlist version, best for most targets }
  1929. ctypedconstbuilder:=tasmlisttypedconstbuilder;
  1930. end.