ngtcon.pas 76 KB

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