ngtcon.pas 76 KB

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