ngtcon.pas 76 KB

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