ptconst.pas 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Reads typed constants
  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 ptconst;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses symtype,symsym,aasmdata;
  21. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym;in_structure:boolean);
  22. implementation
  23. uses
  24. SysUtils,
  25. globtype,systems,tokens,verbose,constexp,
  26. cutils,globals,widestr,scanner,
  27. symconst,symbase,symdef,symtable,
  28. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  29. { pass 1 }
  30. node,htypechk,procinfo,
  31. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  32. { parser specific stuff }
  33. pbase,pexpr,pdecvar,
  34. { codegen }
  35. cpuinfo,cgbase,dbgbase,
  36. wpobase,asmutils
  37. ;
  38. {$maxfpuregisters 0}
  39. {*****************************************************************************
  40. Bitpacked value helpers
  41. *****************************************************************************}
  42. type
  43. tbitpackedval = record
  44. curval, nextval: aword;
  45. curbitoffset: smallint;
  46. loadbitsize,packedbitsize: byte;
  47. end;
  48. procedure initbitpackval(out bp: tbitpackedval; packedbitsize: byte);
  49. begin
  50. bp.curval:=0;
  51. bp.nextval:=0;
  52. bp.curbitoffset:=0;
  53. bp.packedbitsize:=packedbitsize;
  54. bp.loadbitsize:=packedbitsloadsize(bp.packedbitsize)*8;
  55. end;
  56. {$ifopt r+}
  57. {$define rangeon}
  58. {$r-}
  59. {$endif}
  60. {$ifopt q+}
  61. {$define overflowon}
  62. {$q-}
  63. {$endif}
  64. { (values between quotes below refer to fields of bp; fields not }
  65. { mentioned are unused by this routine) }
  66. { bitpacks "value" as bitpacked value of bitsize "packedbitsize" into }
  67. { "curval", which has already been filled up to "curbitoffset", and }
  68. { stores the spillover if any into "nextval". It also updates }
  69. { curbitoffset to reflect how many bits of currval are now used (can be }
  70. { > AIntBits in case of spillover) }
  71. procedure bitpackval(value: aword; var bp: tbitpackedval);
  72. var
  73. shiftcount: longint;
  74. begin
  75. if (target_info.endian=endian_big) then
  76. begin
  77. { bitpacked format: left-aligned (i.e., "big endian bitness") }
  78. bp.curval:=bp.curval or ((value shl (AIntBits-bp.packedbitsize)) shr bp.curbitoffset);
  79. shiftcount:=((AIntBits-bp.packedbitsize)-bp.curbitoffset);
  80. { carry-over to the next element? }
  81. if (shiftcount<0) then
  82. bp.nextval:=(value and ((aword(1) shl (-shiftcount))-1)) shl
  83. (AIntBits+shiftcount)
  84. end
  85. else
  86. begin
  87. { bitpacked format: right aligned (i.e., "little endian bitness") }
  88. bp.curval:=bp.curval or (value shl bp.curbitoffset);
  89. { carry-over to the next element? }
  90. if (bp.curbitoffset+bp.packedbitsize>AIntBits) then
  91. bp.nextval:=value shr (AIntBits-bp.curbitoffset)
  92. end;
  93. inc(bp.curbitoffset,bp.packedbitsize);
  94. end;
  95. {$ifdef rangeon}
  96. {$r+}
  97. {$undef rangeon}
  98. {$endif}
  99. {$ifdef overflowon}
  100. {$q+}
  101. {$undef overflowon}
  102. {$endif}
  103. procedure flush_packed_value(list: tasmlist; var bp: tbitpackedval);
  104. var
  105. bitstowrite: longint;
  106. writeval : byte;
  107. begin
  108. if (bp.curbitoffset < AIntBits) then
  109. begin
  110. { forced flush -> write multiple of loadsize }
  111. bitstowrite:=align(bp.curbitoffset,bp.loadbitsize);
  112. bp.curbitoffset:=0;
  113. end
  114. else
  115. begin
  116. bitstowrite:=AIntBits;
  117. dec(bp.curbitoffset,AIntBits);
  118. end;
  119. while (bitstowrite>=8) do
  120. begin
  121. if (target_info.endian=endian_little) then
  122. begin
  123. { write lowest byte }
  124. writeval:=byte(bp.curval);
  125. bp.curval:=bp.curval shr 8;
  126. end
  127. else
  128. begin
  129. { write highest byte }
  130. writeval:=bp.curval shr (AIntBits-8);
  131. bp.curval:=(bp.curval and (not($ff shl (AIntBits-8)))) shl 8;
  132. end;
  133. list.concat(tai_const.create_8bit(writeval));
  134. dec(bitstowrite,8);
  135. end;
  136. bp.curval:=bp.nextval;
  137. bp.nextval:=0;
  138. end;
  139. {*****************************************************************************
  140. read typed const
  141. *****************************************************************************}
  142. type
  143. { context used for parsing complex types (arrays/records/objects) }
  144. threc = record
  145. list : tasmlist;
  146. origsym: tstaticvarsym;
  147. offset: asizeint;
  148. origblock: tblock_type;
  149. end;
  150. { this procedure reads typed constants }
  151. procedure read_typed_const_data(var hr:threc;def:tdef); forward;
  152. procedure parse_orddef(list:tasmlist;def:torddef);
  153. var
  154. n : tnode;
  155. intvalue : tconstexprint;
  156. procedure do_error;
  157. begin
  158. if is_constnode(n) then
  159. IncompatibleTypes(n.resultdef, def)
  160. else
  161. Message(parser_e_illegal_expression);
  162. end;
  163. begin
  164. n:=comp_expr(true,false);
  165. { for C-style booleans, true=-1 and false=0) }
  166. if is_cbool(def) then
  167. inserttypeconv(n,def);
  168. case def.ordtype of
  169. pasbool8,
  170. bool8bit :
  171. begin
  172. if is_constboolnode(n) then
  173. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value.svalue)))
  174. else
  175. do_error;
  176. end;
  177. pasbool16,
  178. bool16bit :
  179. begin
  180. if is_constboolnode(n) then
  181. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value.svalue)))
  182. else
  183. do_error;
  184. end;
  185. pasbool32,
  186. bool32bit :
  187. begin
  188. if is_constboolnode(n) then
  189. list.concat(Tai_const.Create_32bit(longint(tordconstnode(n).value.svalue)))
  190. else
  191. do_error;
  192. end;
  193. pasbool64,
  194. bool64bit :
  195. begin
  196. if is_constboolnode(n) then
  197. list.concat(Tai_const.Create_64bit(int64(tordconstnode(n).value.svalue)))
  198. else
  199. do_error;
  200. end;
  201. uchar :
  202. begin
  203. if is_constcharnode(n) or
  204. ((m_delphi in current_settings.modeswitches) and
  205. is_constwidecharnode(n) and
  206. (tordconstnode(n).value <= 255)) then
  207. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value.svalue)))
  208. else
  209. do_error;
  210. end;
  211. uwidechar :
  212. begin
  213. if is_constcharnode(n) then
  214. inserttypeconv(n,cwidechartype);
  215. if is_constwidecharnode(n) then
  216. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value.svalue)))
  217. else
  218. do_error;
  219. end;
  220. s8bit,u8bit,
  221. u16bit,s16bit,
  222. s32bit,u32bit,
  223. s64bit,u64bit :
  224. begin
  225. if is_constintnode(n) then
  226. begin
  227. testrange(def,tordconstnode(n).value,false,false);
  228. case def.size of
  229. 1 :
  230. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value.svalue)));
  231. 2 :
  232. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value.svalue)));
  233. 4 :
  234. list.concat(Tai_const.Create_32bit(longint(tordconstnode(n).value.svalue)));
  235. 8 :
  236. list.concat(Tai_const.Create_64bit(tordconstnode(n).value.svalue));
  237. end;
  238. end
  239. else
  240. do_error;
  241. end;
  242. scurrency:
  243. begin
  244. if is_constintnode(n) then
  245. intvalue := tordconstnode(n).value
  246. { allow bootstrapping }
  247. else if is_constrealnode(n) then
  248. intvalue:=PInt64(@trealconstnode(n).value_currency)^
  249. else
  250. begin
  251. intvalue:=0;
  252. IncompatibleTypes(n.resultdef, def);
  253. end;
  254. list.concat(Tai_const.Create_64bit(intvalue));
  255. end;
  256. else
  257. internalerror(200611052);
  258. end;
  259. n.free;
  260. end;
  261. procedure parse_floatdef(list:tasmlist;def:tfloatdef);
  262. var
  263. n : tnode;
  264. value : bestreal;
  265. begin
  266. n:=comp_expr(true,false);
  267. if is_constrealnode(n) then
  268. value:=trealconstnode(n).value_real
  269. else if is_constintnode(n) then
  270. value:=tordconstnode(n).value
  271. else if is_constnode(n) then
  272. IncompatibleTypes(n.resultdef, def)
  273. else
  274. Message(parser_e_illegal_expression);
  275. case def.floattype of
  276. s32real :
  277. list.concat(Tai_real_32bit.Create(ts32real(value)));
  278. s64real :
  279. {$ifdef ARM}
  280. if is_double_hilo_swapped then
  281. list.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  282. else
  283. {$endif ARM}
  284. list.concat(Tai_real_64bit.Create(ts64real(value)));
  285. s80real :
  286. list.concat(Tai_real_80bit.Create(value,s80floattype.size));
  287. sc80real :
  288. list.concat(Tai_real_80bit.Create(value,sc80floattype.size));
  289. s64comp :
  290. { the round is necessary for native compilers where comp isn't a float }
  291. list.concat(Tai_comp_64bit.Create(round(value)));
  292. s64currency:
  293. list.concat(Tai_comp_64bit.Create(round(value*10000)));
  294. s128real:
  295. list.concat(Tai_real_128bit.Create(value));
  296. else
  297. internalerror(200611053);
  298. end;
  299. n.free;
  300. end;
  301. procedure parse_classrefdef(list:tasmlist;def:tclassrefdef);
  302. var
  303. n : tnode;
  304. begin
  305. n:=comp_expr(true,false);
  306. case n.nodetype of
  307. loadvmtaddrn:
  308. begin
  309. if not Tobjectdef(tclassrefdef(n.resultdef).pointeddef).is_related(tobjectdef(def.pointeddef)) then
  310. IncompatibleTypes(n.resultdef, def);
  311. list.concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol(Tobjectdef(tclassrefdef(n.resultdef).pointeddef).vmt_mangledname)));
  312. { update wpo info }
  313. if not assigned(current_procinfo) or
  314. (po_inline in current_procinfo.procdef.procoptions) or
  315. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname) then
  316. tobjectdef(tclassrefdef(n.resultdef).pointeddef).register_maybe_created_object_type;
  317. end;
  318. niln:
  319. list.concat(Tai_const.Create_sym(nil));
  320. else if is_constnode(n) then
  321. IncompatibleTypes(n.resultdef, def)
  322. else
  323. Message(parser_e_illegal_expression);
  324. end;
  325. n.free;
  326. end;
  327. procedure parse_pointerdef(list:tasmlist;def:tpointerdef);
  328. var
  329. hp,p : tnode;
  330. srsym : tsym;
  331. pd : tprocdef;
  332. ca : pchar;
  333. pw : pcompilerwidestring;
  334. i,len : longint;
  335. base,
  336. offset : aint;
  337. v : Tconstexprint;
  338. ll : tasmlabel;
  339. varalign : shortint;
  340. begin
  341. p:=comp_expr(true,false);
  342. { remove equal typecasts for pointer/nil addresses }
  343. if (p.nodetype=typeconvn) then
  344. with Ttypeconvnode(p) do
  345. if (left.nodetype in [addrn,niln]) and equal_defs(def,p.resultdef) then
  346. begin
  347. hp:=left;
  348. left:=nil;
  349. p.free;
  350. p:=hp;
  351. end;
  352. { allows horrible ofs(typeof(TButton)^) code !! }
  353. if (p.nodetype=addrn) then
  354. with Taddrnode(p) do
  355. if left.nodetype=derefn then
  356. begin
  357. hp:=tderefnode(left).left;
  358. tderefnode(left).left:=nil;
  359. p.free;
  360. p:=hp;
  361. end;
  362. { const pointer ? }
  363. if (p.nodetype = pointerconstn) then
  364. begin
  365. {$if sizeof(TConstPtrUInt)=8}
  366. list.concat(Tai_const.Create_64bit(int64(tpointerconstnode(p).value)));
  367. {$else}
  368. {$if sizeof(TConstPtrUInt)=4}
  369. list.concat(Tai_const.Create_32bit(longint(tpointerconstnode(p).value)));
  370. {$else}
  371. internalerror(200404122);
  372. {$endif} {$endif}
  373. end
  374. { nil pointer ? }
  375. else if p.nodetype=niln then
  376. list.concat(Tai_const.Create_sym(nil))
  377. { maybe pchar ? }
  378. else
  379. if is_char(def.pointeddef) and
  380. (p.nodetype<>addrn) then
  381. begin
  382. current_asmdata.getdatalabel(ll);
  383. list.concat(Tai_const.Create_sym(ll));
  384. if p.nodetype=stringconstn then
  385. varalign:=size_2_align(tstringconstnode(p).len)
  386. else
  387. varalign:=0;
  388. varalign:=const_align(varalign);
  389. new_section(current_asmdata.asmlists[al_const], sec_rodata, ll.name, varalign);
  390. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  391. if p.nodetype=stringconstn then
  392. begin
  393. len:=tstringconstnode(p).len;
  394. { For tp7 the maximum lentgh can be 255 }
  395. if (m_tp7 in current_settings.modeswitches) and
  396. (len>255) then
  397. len:=255;
  398. getmem(ca,len+2);
  399. move(tstringconstnode(p).value_str^,ca^,len+1);
  400. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,len+1));
  401. end
  402. else
  403. if is_constcharnode(p) then
  404. current_asmdata.asmlists[al_const].concat(Tai_string.Create(char(byte(tordconstnode(p).value.svalue))+#0))
  405. else
  406. IncompatibleTypes(p.resultdef, def);
  407. end
  408. { maybe pwidechar ? }
  409. else
  410. if is_widechar(def.pointeddef) and
  411. (p.nodetype<>addrn) then
  412. begin
  413. current_asmdata.getdatalabel(ll);
  414. list.concat(Tai_const.Create_sym(ll));
  415. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(sizeof(pint))));
  416. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(ll));
  417. if (p.nodetype in [stringconstn,ordconstn]) then
  418. begin
  419. { convert to widestring stringconstn }
  420. inserttypeconv(p,cwidestringtype);
  421. if (p.nodetype=stringconstn) and
  422. (tstringconstnode(p).cst_type in [cst_widestring,cst_unicodestring]) then
  423. begin
  424. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  425. for i:=0 to tstringconstnode(p).len-1 do
  426. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(pw^.data[i]));
  427. { ending #0 }
  428. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(0))
  429. end;
  430. end
  431. else
  432. IncompatibleTypes(p.resultdef, def);
  433. end
  434. else
  435. if (p.nodetype=addrn) or
  436. is_proc2procvar_load(p,pd) then
  437. begin
  438. { insert typeconv }
  439. inserttypeconv(p,def);
  440. hp:=p;
  441. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  442. hp:=tunarynode(hp).left;
  443. if (hp.nodetype=loadn) then
  444. begin
  445. hp:=p;
  446. offset:=0;
  447. while assigned(hp) and (hp.nodetype<>loadn) do
  448. begin
  449. case hp.nodetype of
  450. vecn :
  451. begin
  452. case tvecnode(hp).left.resultdef.typ of
  453. stringdef :
  454. begin
  455. { this seems OK for shortstring and ansistrings PM }
  456. { it is wrong for widestrings !! }
  457. len:=1;
  458. base:=0;
  459. end;
  460. arraydef :
  461. begin
  462. if not is_packed_array(tvecnode(hp).left.resultdef) then
  463. begin
  464. len:=tarraydef(tvecnode(hp).left.resultdef).elesize;
  465. base:=tarraydef(tvecnode(hp).left.resultdef).lowrange;
  466. end
  467. else
  468. begin
  469. Message(parser_e_packed_dynamic_open_array);
  470. len:=1;
  471. base:=0;
  472. end;
  473. end
  474. else
  475. Message(parser_e_illegal_expression);
  476. end;
  477. if is_constintnode(tvecnode(hp).right) then
  478. begin
  479. {Prevent overflow.}
  480. v:=get_ordinal_value(tvecnode(hp).right)-base;
  481. if (v<int64(low(offset))) or (v>int64(high(offset))) then
  482. message(parser_e_range_check_error);
  483. if high(offset)-offset div len>v then
  484. inc(offset,len*v.svalue)
  485. else
  486. message(parser_e_range_check_error);
  487. end
  488. else
  489. Message(parser_e_illegal_expression);
  490. end;
  491. subscriptn :
  492. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  493. typeconvn :
  494. begin
  495. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  496. Message(parser_e_illegal_expression);
  497. end;
  498. addrn :
  499. ;
  500. else
  501. Message(parser_e_illegal_expression);
  502. end;
  503. hp:=tunarynode(hp).left;
  504. end;
  505. srsym:=tloadnode(hp).symtableentry;
  506. case srsym.typ of
  507. procsym :
  508. begin
  509. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  510. if Tprocsym(srsym).ProcdefList.Count>1 then
  511. Message(parser_e_no_overloaded_procvars);
  512. if po_abstractmethod in pd.procoptions then
  513. Message(type_e_cant_take_address_of_abstract_method)
  514. else
  515. list.concat(Tai_const.Createname(pd.mangledname,offset));
  516. end;
  517. staticvarsym :
  518. list.concat(Tai_const.Createname(tstaticvarsym(srsym).mangledname,offset));
  519. labelsym :
  520. list.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,offset));
  521. constsym :
  522. if tconstsym(srsym).consttyp=constresourcestring then
  523. list.concat(Tai_const.Createname(make_mangledname('RESSTR',tconstsym(srsym).owner,tconstsym(srsym).name),sizeof(pint)))
  524. else
  525. Message(type_e_variable_id_expected);
  526. else
  527. Message(type_e_variable_id_expected);
  528. end;
  529. end
  530. else
  531. Message(parser_e_illegal_expression);
  532. end
  533. else
  534. { allow typeof(Object type)}
  535. if (p.nodetype=inlinen) and
  536. (tinlinenode(p).inlinenumber=in_typeof_x) then
  537. begin
  538. if (tinlinenode(p).left.nodetype=typen) then
  539. begin
  540. list.concat(Tai_const.createname(
  541. tobjectdef(tinlinenode(p).left.resultdef).vmt_mangledname,0));
  542. end
  543. else
  544. Message(parser_e_illegal_expression);
  545. end
  546. else
  547. Message(parser_e_illegal_expression);
  548. p.free;
  549. end;
  550. procedure parse_setdef(list:tasmlist;def:tsetdef);
  551. type
  552. setbytes = array[0..31] of byte;
  553. Psetbytes = ^setbytes;
  554. var
  555. p : tnode;
  556. i : longint;
  557. begin
  558. p:=comp_expr(true,false);
  559. if p.nodetype=setconstn then
  560. begin
  561. { be sure to convert to the correct result, else
  562. it can generate smallset data instead of normalset (PFV) }
  563. inserttypeconv(p,def);
  564. { we only allow const sets }
  565. if (p.nodetype<>setconstn) or
  566. assigned(tsetconstnode(p).left) then
  567. Message(parser_e_illegal_expression)
  568. else
  569. begin
  570. tsetconstnode(p).adjustforsetbase;
  571. { this writing is endian-dependant }
  572. if source_info.endian = target_info.endian then
  573. begin
  574. for i:=0 to p.resultdef.size-1 do
  575. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[i]));
  576. end
  577. else
  578. begin
  579. for i:=0 to p.resultdef.size-1 do
  580. list.concat(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(p).value_set)^[i])));
  581. end;
  582. end;
  583. end
  584. else
  585. Message(parser_e_illegal_expression);
  586. p.free;
  587. end;
  588. procedure parse_enumdef(list:tasmlist;def:tenumdef);
  589. var
  590. p : tnode;
  591. begin
  592. p:=comp_expr(true,false);
  593. if p.nodetype=ordconstn then
  594. begin
  595. if equal_defs(p.resultdef,def) or
  596. is_subequal(p.resultdef,def) then
  597. begin
  598. case longint(p.resultdef.size) of
  599. 1 : list.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value.svalue)));
  600. 2 : list.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value.svalue)));
  601. 4 : list.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value.svalue)));
  602. end;
  603. end
  604. else
  605. IncompatibleTypes(p.resultdef,def);
  606. end
  607. else
  608. Message(parser_e_illegal_expression);
  609. p.free;
  610. end;
  611. procedure parse_stringdef(const hr:threc;def:tstringdef);
  612. var
  613. n : tnode;
  614. strlength : aint;
  615. strval : pchar;
  616. strch : char;
  617. ll : tasmlabel;
  618. ca : pchar;
  619. winlike : boolean;
  620. hsym : tconstsym;
  621. begin
  622. n:=comp_expr(true,false);
  623. { load strval and strlength of the constant tree }
  624. if (n.nodetype=stringconstn) or is_wide_or_unicode_string(def) or is_constwidecharnode(n) or
  625. ((n.nodetype=typen) and is_interfacecorba(ttypenode(n).typedef)) then
  626. begin
  627. { convert to the expected string type so that
  628. for widestrings strval is a pcompilerwidestring }
  629. inserttypeconv(n,def);
  630. if (not codegenerror) and
  631. (n.nodetype=stringconstn) then
  632. begin
  633. strlength:=tstringconstnode(n).len;
  634. strval:=tstringconstnode(n).value_str;
  635. end
  636. else
  637. begin
  638. { an error occurred trying to convert the result to a string }
  639. strlength:=-1;
  640. { it's possible that the type conversion could not be
  641. evaluated at compile-time }
  642. if not codegenerror then
  643. CGMessage(parser_e_widestring_to_ansi_compile_time);
  644. end;
  645. end
  646. else if is_constcharnode(n) then
  647. begin
  648. { strval:=pchar(@tordconstnode(n).value);
  649. THIS FAIL on BIG_ENDIAN MACHINES PM }
  650. strch:=chr(tordconstnode(n).value.svalue and $ff);
  651. strval:=@strch;
  652. strlength:=1
  653. end
  654. else if is_constresourcestringnode(n) then
  655. begin
  656. hsym:=tconstsym(tloadnode(n).symtableentry);
  657. strval:=pchar(hsym.value.valueptr);
  658. strlength:=hsym.value.len;
  659. { Delphi-compatible (mis)feature:
  660. Link AnsiString constants to their initializing resourcestring,
  661. enabling them to be (re)translated at runtime.
  662. Wide/UnicodeString are currently rejected above (with incorrect error message).
  663. ShortStrings cannot be handled unless another table is built for them;
  664. considering this acceptable, because Delphi rejects them altogether.
  665. }
  666. if (not is_shortstring(def)) and
  667. ((hr.origsym.owner.symtablelevel<=main_program_level) or
  668. (hr.origblock=bt_const)) then
  669. begin
  670. current_asmdata.ResStrInits.Concat(
  671. TTCInitItem.Create(hr.origsym,hr.offset,
  672. current_asmdata.RefAsmSymbol(make_mangledname('RESSTR',hsym.owner,hsym.name)))
  673. );
  674. Include(hr.origsym.varoptions,vo_force_finalize);
  675. end;
  676. end
  677. else
  678. begin
  679. Message(parser_e_illegal_expression);
  680. strlength:=-1;
  681. end;
  682. if strlength>=0 then
  683. begin
  684. case def.stringtype of
  685. st_shortstring:
  686. begin
  687. if strlength>=def.size then
  688. begin
  689. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  690. strlength:=def.size-1;
  691. end;
  692. hr.list.concat(Tai_const.Create_8bit(strlength));
  693. { this can also handle longer strings }
  694. getmem(ca,strlength+1);
  695. move(strval^,ca^,strlength);
  696. ca[strlength]:=#0;
  697. hr.list.concat(Tai_string.Create_pchar(ca,strlength));
  698. { fillup with spaces if size is shorter }
  699. if def.size>strlength then
  700. begin
  701. getmem(ca,def.size-strlength);
  702. { def.size contains also the leading length, so we }
  703. { we have to subtract one }
  704. fillchar(ca[0],def.size-strlength-1,' ');
  705. ca[def.size-strlength-1]:=#0;
  706. { this can also handle longer strings }
  707. hr.list.concat(Tai_string.Create_pchar(ca,def.size-strlength-1));
  708. end;
  709. end;
  710. st_ansistring:
  711. begin
  712. { an empty ansi string is nil! }
  713. if (strlength=0) then
  714. ll := nil
  715. else
  716. ll := emit_ansistring_const(current_asmdata.asmlists[al_const],strval,strlength);
  717. hr.list.concat(Tai_const.Create_sym(ll));
  718. end;
  719. st_unicodestring,
  720. st_widestring:
  721. begin
  722. { an empty wide/unicode string is nil! }
  723. if (strlength=0) then
  724. ll := nil
  725. else
  726. begin
  727. winlike := (def.stringtype=st_widestring) and (tf_winlikewidestring in target_info.flags);
  728. ll := emit_unicodestring_const(current_asmdata.asmlists[al_const],
  729. strval,
  730. winlike);
  731. { Collect Windows widestrings that need initialization at startup.
  732. Local initialized vars are excluded because they are initialized
  733. at function entry instead. }
  734. if winlike and ((hr.origsym.owner.symtablelevel <= main_program_level) or
  735. (hr.origblock=bt_const)) then
  736. begin
  737. current_asmdata.WideInits.Concat(
  738. TTCInitItem.Create(hr.origsym, hr.offset, ll)
  739. );
  740. ll := nil;
  741. Include(hr.origsym.varoptions, vo_force_finalize);
  742. end;
  743. end;
  744. hr.list.concat(Tai_const.Create_sym(ll));
  745. end;
  746. else
  747. internalerror(200107081);
  748. end;
  749. end;
  750. n.free;
  751. end;
  752. { parse a single constant and add it to the packed const info }
  753. { represented by curval etc (see explanation of bitpackval for }
  754. { what the different parameters mean) }
  755. function parse_single_packed_const(list: tasmlist; def: tdef; var bp: tbitpackedval): boolean;
  756. var
  757. n : tnode;
  758. begin
  759. result:=true;
  760. n:=comp_expr(true,false);
  761. if (n.nodetype <> ordconstn) or
  762. (not equal_defs(n.resultdef,def) and
  763. not is_subequal(n.resultdef,def)) then
  764. begin
  765. n.free;
  766. incompatibletypes(n.resultdef,def);
  767. consume_all_until(_SEMICOLON);
  768. result:=false;
  769. exit;
  770. end;
  771. if (Tordconstnode(n).value<qword(low(Aword))) or (Tordconstnode(n).value>qword(high(Aword))) then
  772. message(parser_e_range_check_error)
  773. else
  774. bitpackval(Tordconstnode(n).value.uvalue,bp);
  775. if (bp.curbitoffset>=AIntBits) then
  776. flush_packed_value(list,bp);
  777. n.free;
  778. end;
  779. { parses a packed array constant }
  780. procedure parse_packed_array_def(list: tasmlist; def: tarraydef);
  781. var
  782. i : aint;
  783. bp : tbitpackedval;
  784. begin
  785. if not(def.elementdef.typ in [orddef,enumdef]) then
  786. internalerror(2007022010);
  787. { begin of the array }
  788. consume(_LKLAMMER);
  789. initbitpackval(bp,def.elepackedbitsize);
  790. i:=def.lowrange;
  791. { can't use for-loop, fails when cross-compiling from }
  792. { 32 to 64 bit because i is then 64 bit }
  793. while (i<def.highrange) do
  794. begin
  795. { get next item of the packed array }
  796. if not parse_single_packed_const(list,def.elementdef,bp) then
  797. exit;
  798. consume(_COMMA);
  799. inc(i);
  800. end;
  801. { final item }
  802. if not parse_single_packed_const(list,def.elementdef,bp) then
  803. exit;
  804. { flush final incomplete value if necessary }
  805. if (bp.curbitoffset <> 0) then
  806. flush_packed_value(list,bp);
  807. consume(_RKLAMMER);
  808. end;
  809. procedure parse_arraydef(hr:threc;def:tarraydef);
  810. var
  811. n : tnode;
  812. i : longint;
  813. len : asizeint;
  814. ch : array[0..1] of char;
  815. ca : pbyte;
  816. int_const: tai_const;
  817. char_size: integer;
  818. begin
  819. { dynamic array nil }
  820. if is_dynamic_array(def) then
  821. begin
  822. { Only allow nil initialization }
  823. consume(_NIL);
  824. hr.list.concat(Tai_const.Create_sym(nil));
  825. end
  826. { packed array constant }
  827. else if is_packed_array(def) and
  828. ((def.elepackedbitsize mod 8 <> 0) or
  829. not ispowerof2(def.elepackedbitsize div 8,i)) then
  830. begin
  831. parse_packed_array_def(hr.list,def);
  832. end
  833. { normal array const between brackets }
  834. else if try_to_consume(_LKLAMMER) then
  835. begin
  836. hr.offset:=0;
  837. for i:=def.lowrange to def.highrange-1 do
  838. begin
  839. read_typed_const_data(hr,def.elementdef);
  840. Inc(hr.offset,def.elementdef.size);
  841. if token=_RKLAMMER then
  842. begin
  843. Message1(parser_e_more_array_elements_expected,tostr(def.highrange-i));
  844. consume(_RKLAMMER);
  845. exit;
  846. end
  847. else
  848. consume(_COMMA);
  849. end;
  850. read_typed_const_data(hr,def.elementdef);
  851. consume(_RKLAMMER);
  852. end
  853. { if array of char then we allow also a string }
  854. else if is_anychar(def.elementdef) then
  855. begin
  856. char_size:=def.elementdef.size;
  857. n:=comp_expr(true,false);
  858. if n.nodetype=stringconstn then
  859. begin
  860. len:=tstringconstnode(n).len;
  861. case char_size of
  862. 1:
  863. ca:=pointer(tstringconstnode(n).value_str);
  864. 2:
  865. begin
  866. inserttypeconv(n,cwidestringtype);
  867. if n.nodetype<>stringconstn then
  868. internalerror(2010033003);
  869. ca:=pointer(pcompilerwidestring(tstringconstnode(n).value_str)^.data)
  870. end;
  871. else
  872. internalerror(2010033005);
  873. end;
  874. { For tp7 the maximum lentgh can be 255 }
  875. if (m_tp7 in current_settings.modeswitches) and
  876. (len>255) then
  877. len:=255;
  878. end
  879. else if is_constcharnode(n) then
  880. begin
  881. case char_size of
  882. 1:
  883. ch[0]:=chr(tordconstnode(n).value.uvalue and $ff);
  884. 2:
  885. begin
  886. inserttypeconv(n,cwidechartype);
  887. if not is_constwidecharnode(n) then
  888. internalerror(2010033001);
  889. widechar(ch):=widechar(tordconstnode(n).value.uvalue and $ffff);
  890. end;
  891. else
  892. internalerror(2010033002);
  893. end;
  894. ca:=@ch;
  895. len:=1;
  896. end
  897. else
  898. begin
  899. Message(parser_e_illegal_expression);
  900. len:=0;
  901. end;
  902. if len>(def.highrange-def.lowrange+1) then
  903. Message(parser_e_string_larger_array);
  904. for i:=0 to def.highrange-def.lowrange do
  905. begin
  906. if i<len then
  907. begin
  908. case char_size of
  909. 1:
  910. int_const:=Tai_const.Create_char(char_size,pbyte(ca)^);
  911. 2:
  912. int_const:=Tai_const.Create_char(char_size,pword(ca)^);
  913. else
  914. internalerror(2010033004);
  915. end;
  916. inc(ca, char_size);
  917. end
  918. else
  919. {Fill the remaining positions with #0.}
  920. int_const:=Tai_const.Create_char(char_size,0);
  921. hr.list.concat(int_const)
  922. end;
  923. n.free;
  924. end
  925. else
  926. begin
  927. { we want the ( }
  928. consume(_LKLAMMER);
  929. end;
  930. end;
  931. procedure parse_procvardef(list:tasmlist;def:tprocvardef);
  932. var
  933. tmpn,n : tnode;
  934. pd : tprocdef;
  935. begin
  936. { Procvars and pointers are no longer compatible. }
  937. { under tp: =nil or =var under fpc: =nil or =@var }
  938. if try_to_consume(_NIL) then
  939. begin
  940. list.concat(Tai_const.Create_sym(nil));
  941. if not def.is_addressonly then
  942. list.concat(Tai_const.Create_sym(nil));
  943. exit;
  944. end;
  945. { you can't assign a value other than NIL to a typed constant }
  946. { which is a "procedure of object", because this also requires }
  947. { address of an object/class instance, which is not known at }
  948. { compile time (JM) }
  949. if (po_methodpointer in def.procoptions) then
  950. Message(parser_e_no_procvarobj_const);
  951. { parse the rest too, so we can continue with error checking }
  952. getprocvardef:=def;
  953. n:=comp_expr(true,false);
  954. getprocvardef:=nil;
  955. if codegenerror then
  956. begin
  957. n.free;
  958. exit;
  959. end;
  960. { let type conversion check everything needed }
  961. inserttypeconv(n,def);
  962. if codegenerror then
  963. begin
  964. n.free;
  965. exit;
  966. end;
  967. { remove typeconvs, that will normally insert a lea
  968. instruction which is not necessary for us }
  969. while n.nodetype=typeconvn do
  970. begin
  971. tmpn:=ttypeconvnode(n).left;
  972. ttypeconvnode(n).left:=nil;
  973. n.free;
  974. n:=tmpn;
  975. end;
  976. { remove addrn which we also don't need here }
  977. if n.nodetype=addrn then
  978. begin
  979. tmpn:=taddrnode(n).left;
  980. taddrnode(n).left:=nil;
  981. n.free;
  982. n:=tmpn;
  983. end;
  984. { we now need to have a loadn with a procsym }
  985. if (n.nodetype=loadn) and
  986. (tloadnode(n).symtableentry.typ=procsym) then
  987. begin
  988. pd:=tloadnode(n).procdef;
  989. list.concat(Tai_const.createname(pd.mangledname,0));
  990. { nested procvar typed consts can only be initialised with nil
  991. (checked above) or with a global procedure (checked here),
  992. because in other cases we need a valid frame pointer }
  993. if is_nested_pd(def) then
  994. begin
  995. if is_nested_pd(pd) then
  996. Message(parser_e_no_procvarnested_const);
  997. list.concat(Tai_const.Create_sym(nil));
  998. end
  999. end
  1000. else
  1001. Message(parser_e_illegal_expression);
  1002. n.free;
  1003. end;
  1004. procedure parse_recorddef(hr:threc;def:trecorddef);
  1005. var
  1006. n : tnode;
  1007. symidx : longint;
  1008. recsym,
  1009. srsym : tsym;
  1010. hs : string;
  1011. sorg,s : TIDString;
  1012. tmpguid : tguid;
  1013. curroffset,
  1014. fillbytes : aint;
  1015. bp : tbitpackedval;
  1016. error,
  1017. is_packed: boolean;
  1018. startoffset: aint;
  1019. procedure handle_stringconstn;
  1020. var
  1021. i : longint;
  1022. begin
  1023. hs:=strpas(tstringconstnode(n).value_str);
  1024. if string2guid(hs,tmpguid) then
  1025. begin
  1026. hr.list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  1027. hr.list.concat(Tai_const.Create_16bit(tmpguid.D2));
  1028. hr.list.concat(Tai_const.Create_16bit(tmpguid.D3));
  1029. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1030. hr.list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  1031. end
  1032. else
  1033. Message(parser_e_improper_guid_syntax);
  1034. end;
  1035. var
  1036. i : longint;
  1037. begin
  1038. { GUID }
  1039. if (def=rec_tguid) and (token=_ID) then
  1040. begin
  1041. n:=comp_expr(true,false);
  1042. if n.nodetype=stringconstn then
  1043. handle_stringconstn
  1044. else
  1045. begin
  1046. inserttypeconv(n,rec_tguid);
  1047. if n.nodetype=guidconstn then
  1048. begin
  1049. tmpguid:=tguidconstnode(n).value;
  1050. hr.list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  1051. hr.list.concat(Tai_const.Create_16bit(tmpguid.D2));
  1052. hr.list.concat(Tai_const.Create_16bit(tmpguid.D3));
  1053. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  1054. hr.list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  1055. end
  1056. else
  1057. Message(parser_e_illegal_expression);
  1058. end;
  1059. n.free;
  1060. exit;
  1061. end;
  1062. if (def=rec_tguid) and ((token=_CSTRING) or (token=_CCHAR)) then
  1063. begin
  1064. n:=comp_expr(true,false);
  1065. inserttypeconv(n,cshortstringtype);
  1066. if n.nodetype=stringconstn then
  1067. handle_stringconstn
  1068. else
  1069. Message(parser_e_illegal_expression);
  1070. n.free;
  1071. exit;
  1072. end;
  1073. { bitpacked record? }
  1074. is_packed:=is_packed_record_or_object(def);
  1075. if (is_packed) then
  1076. begin
  1077. { loadbitsize = 8, bitpacked records are always padded to }
  1078. { a multiple of a byte. packedbitsize will be set separately }
  1079. { for each field }
  1080. initbitpackval(bp,0);
  1081. bp.loadbitsize:=8;
  1082. end;
  1083. { normal record }
  1084. consume(_LKLAMMER);
  1085. curroffset:=0;
  1086. symidx:=0;
  1087. sorg:='';
  1088. srsym:=tsym(def.symtable.SymList[symidx]);
  1089. recsym := nil;
  1090. startoffset:=hr.offset;
  1091. while token<>_RKLAMMER do
  1092. begin
  1093. s:=pattern;
  1094. sorg:=orgpattern;
  1095. consume(_ID);
  1096. consume(_COLON);
  1097. error := false;
  1098. recsym := tsym(def.symtable.Find(s));
  1099. if not assigned(recsym) then
  1100. begin
  1101. Message1(sym_e_illegal_field,sorg);
  1102. error := true;
  1103. end;
  1104. if (not error) and
  1105. (not assigned(srsym) or
  1106. (s <> srsym.name)) then
  1107. { possible variant record (JM) }
  1108. begin
  1109. { All parts of a variant start at the same offset }
  1110. { Also allow jumping from one variant part to another, }
  1111. { as long as the offsets match }
  1112. if (assigned(srsym) and
  1113. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  1114. { srsym is not assigned after parsing w2 in the }
  1115. { typed const in the next example: }
  1116. { type tr = record case byte of }
  1117. { 1: (l1,l2: dword); }
  1118. { 2: (w1,w2: word); }
  1119. { end; }
  1120. { const r: tr = (w1:1;w2:1;l2:5); }
  1121. (tfieldvarsym(recsym).fieldoffset = curroffset) then
  1122. begin
  1123. srsym := recsym;
  1124. symidx := def.symtable.SymList.indexof(srsym)
  1125. end
  1126. { going backwards isn't allowed in any mode }
  1127. else if (tfieldvarsym(recsym).fieldoffset<curroffset) then
  1128. begin
  1129. Message(parser_e_invalid_record_const);
  1130. error := true;
  1131. end
  1132. { Delphi allows you to skip fields }
  1133. else if (m_delphi in current_settings.modeswitches) then
  1134. begin
  1135. Message1(parser_w_skipped_fields_before,sorg);
  1136. srsym := recsym;
  1137. end
  1138. { FPC and TP don't }
  1139. else
  1140. begin
  1141. Message1(parser_e_skipped_fields_before,sorg);
  1142. error := true;
  1143. end;
  1144. end;
  1145. if error then
  1146. consume_all_until(_SEMICOLON)
  1147. else
  1148. begin
  1149. { if needed fill (alignment) }
  1150. if tfieldvarsym(srsym).fieldoffset>curroffset then
  1151. begin
  1152. if not(is_packed) then
  1153. fillbytes:=tfieldvarsym(srsym).fieldoffset-curroffset
  1154. else
  1155. begin
  1156. flush_packed_value(hr.list,bp);
  1157. { curoffset is now aligned to the next byte }
  1158. curroffset:=align(curroffset,8);
  1159. { offsets are in bits in this case }
  1160. fillbytes:=(tfieldvarsym(srsym).fieldoffset-curroffset) div 8;
  1161. end;
  1162. for i:=1 to fillbytes do
  1163. hr.list.concat(Tai_const.Create_8bit(0))
  1164. end;
  1165. { new position }
  1166. curroffset:=tfieldvarsym(srsym).fieldoffset;
  1167. if not(is_packed) then
  1168. inc(curroffset,tfieldvarsym(srsym).vardef.size)
  1169. else
  1170. inc(curroffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1171. { read the data }
  1172. if not(is_packed) or
  1173. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1174. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1175. begin
  1176. if is_packed then
  1177. begin
  1178. flush_packed_value(hr.list,bp);
  1179. curroffset:=align(curroffset,8);
  1180. end;
  1181. hr.offset:=startoffset+tfieldvarsym(srsym).fieldoffset;
  1182. read_typed_const_data(hr,tfieldvarsym(srsym).vardef);
  1183. end
  1184. else
  1185. begin
  1186. bp.packedbitsize:=tfieldvarsym(srsym).vardef.packedbitsize;
  1187. parse_single_packed_const(hr.list,tfieldvarsym(srsym).vardef,bp);
  1188. end;
  1189. { keep previous field for checking whether whole }
  1190. { record was initialized (JM) }
  1191. recsym := srsym;
  1192. { goto next field }
  1193. inc(symidx);
  1194. if symidx<def.symtable.SymList.Count then
  1195. srsym:=tsym(def.symtable.SymList[symidx])
  1196. else
  1197. srsym:=nil;
  1198. if token=_SEMICOLON then
  1199. consume(_SEMICOLON)
  1200. else if (token=_COMMA) and (m_mac in current_settings.modeswitches) then
  1201. consume(_COMMA)
  1202. else
  1203. break;
  1204. end;
  1205. end;
  1206. { are there any fields left, but don't complain if there only
  1207. come other variant parts after the last initialized field }
  1208. if assigned(srsym) and
  1209. (
  1210. (recsym=nil) or
  1211. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1212. ) then
  1213. Message1(parser_w_skipped_fields_after,sorg);
  1214. if not(is_packed) then
  1215. fillbytes:=def.size-curroffset
  1216. else
  1217. begin
  1218. flush_packed_value(hr.list,bp);
  1219. curroffset:=align(curroffset,8);
  1220. fillbytes:=def.size-(curroffset div 8);
  1221. end;
  1222. for i:=1 to fillbytes do
  1223. hr.list.concat(Tai_const.Create_8bit(0));
  1224. consume(_RKLAMMER);
  1225. end;
  1226. { note: hr is passed by value }
  1227. procedure parse_objectdef(hr:threc;def:tobjectdef);
  1228. var
  1229. n : tnode;
  1230. i : longint;
  1231. obj : tobjectdef;
  1232. srsym : tsym;
  1233. st : tsymtable;
  1234. curroffset : aint;
  1235. s,sorg : TIDString;
  1236. vmtwritten : boolean;
  1237. startoffset:aint;
  1238. begin
  1239. { no support for packed object }
  1240. if is_packed_record_or_object(def) then
  1241. begin
  1242. Message(type_e_no_const_packed_record);
  1243. exit;
  1244. end;
  1245. { only allow nil for implicit pointer object types }
  1246. if is_implicit_pointer_object_type(def) then
  1247. begin
  1248. n:=comp_expr(true,false);
  1249. if n.nodetype<>niln then
  1250. begin
  1251. Message(parser_e_type_const_not_possible);
  1252. consume_all_until(_SEMICOLON);
  1253. end
  1254. else
  1255. hr.list.concat(Tai_const.Create_sym(nil));
  1256. n.free;
  1257. exit;
  1258. end;
  1259. { for objects we allow it only if it doesn't contain a vmt }
  1260. if (oo_has_vmt in def.objectoptions) and
  1261. (m_fpc in current_settings.modeswitches) then
  1262. begin
  1263. Message(parser_e_type_object_constants);
  1264. exit;
  1265. end;
  1266. consume(_LKLAMMER);
  1267. startoffset:=hr.offset;
  1268. curroffset:=0;
  1269. vmtwritten:=false;
  1270. while token<>_RKLAMMER do
  1271. begin
  1272. s:=pattern;
  1273. sorg:=orgpattern;
  1274. consume(_ID);
  1275. consume(_COLON);
  1276. srsym:=nil;
  1277. obj:=tobjectdef(def);
  1278. st:=obj.symtable;
  1279. while (srsym=nil) and assigned(st) do
  1280. begin
  1281. srsym:=tsym(st.Find(s));
  1282. if assigned(obj) then
  1283. obj:=obj.childof;
  1284. if assigned(obj) then
  1285. st:=obj.symtable
  1286. else
  1287. st:=nil;
  1288. end;
  1289. if (srsym=nil) or
  1290. (srsym.typ<>fieldvarsym) then
  1291. begin
  1292. if (srsym=nil) then
  1293. Message1(sym_e_id_not_found,sorg)
  1294. else
  1295. Message1(sym_e_illegal_field,sorg);
  1296. consume_all_until(_RKLAMMER);
  1297. break;
  1298. end
  1299. else
  1300. with tfieldvarsym(srsym) do
  1301. begin
  1302. { check position }
  1303. if fieldoffset<curroffset then
  1304. message(parser_e_invalid_record_const);
  1305. { check in VMT needs to be added for TP mode }
  1306. if not(vmtwritten) and
  1307. not(m_fpc in current_settings.modeswitches) and
  1308. (oo_has_vmt in def.objectoptions) and
  1309. (def.vmt_offset<fieldoffset) then
  1310. begin
  1311. for i:=1 to def.vmt_offset-curroffset do
  1312. hr.list.concat(tai_const.create_8bit(0));
  1313. hr.list.concat(tai_const.createname(def.vmt_mangledname,0));
  1314. { this is more general }
  1315. curroffset:=def.vmt_offset + sizeof(pint);
  1316. vmtwritten:=true;
  1317. end;
  1318. { if needed fill }
  1319. if fieldoffset>curroffset then
  1320. for i:=1 to fieldoffset-curroffset do
  1321. hr.list.concat(Tai_const.Create_8bit(0));
  1322. { new position }
  1323. curroffset:=fieldoffset+vardef.size;
  1324. { read the data }
  1325. hr.offset:=startoffset+fieldoffset;
  1326. read_typed_const_data(hr,vardef);
  1327. if not try_to_consume(_SEMICOLON) then
  1328. break;
  1329. end;
  1330. end;
  1331. if not(m_fpc in current_settings.modeswitches) and
  1332. (oo_has_vmt in def.objectoptions) and
  1333. (def.vmt_offset>=curroffset) then
  1334. begin
  1335. for i:=1 to def.vmt_offset-curroffset do
  1336. hr.list.concat(tai_const.create_8bit(0));
  1337. hr.list.concat(tai_const.createname(def.vmt_mangledname,0));
  1338. { this is more general }
  1339. curroffset:=def.vmt_offset + sizeof(pint);
  1340. end;
  1341. for i:=1 to def.size-curroffset do
  1342. hr.list.concat(Tai_const.Create_8bit(0));
  1343. consume(_RKLAMMER);
  1344. end;
  1345. procedure read_typed_const_data(var hr:threc;def:tdef);
  1346. var
  1347. old_block_type : tblock_type;
  1348. begin
  1349. old_block_type:=block_type;
  1350. block_type:=bt_const;
  1351. case def.typ of
  1352. orddef :
  1353. parse_orddef(hr.list,torddef(def));
  1354. floatdef :
  1355. parse_floatdef(hr.list,tfloatdef(def));
  1356. classrefdef :
  1357. parse_classrefdef(hr.list,tclassrefdef(def));
  1358. pointerdef :
  1359. parse_pointerdef(hr.list,tpointerdef(def));
  1360. setdef :
  1361. parse_setdef(hr.list,tsetdef(def));
  1362. enumdef :
  1363. parse_enumdef(hr.list,tenumdef(def));
  1364. stringdef :
  1365. parse_stringdef(hr,tstringdef(def));
  1366. arraydef :
  1367. parse_arraydef(hr,tarraydef(def));
  1368. procvardef:
  1369. parse_procvardef(hr.list,tprocvardef(def));
  1370. recorddef:
  1371. parse_recorddef(hr,trecorddef(def));
  1372. objectdef:
  1373. parse_objectdef(hr,tobjectdef(def));
  1374. errordef:
  1375. begin
  1376. { try to consume something useful }
  1377. if token=_LKLAMMER then
  1378. consume_all_until(_RKLAMMER)
  1379. else
  1380. consume_all_until(_SEMICOLON);
  1381. end;
  1382. else
  1383. Message(parser_e_type_const_not_possible);
  1384. end;
  1385. block_type:=old_block_type;
  1386. end;
  1387. {$maxfpuregisters default}
  1388. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym;in_structure:boolean);
  1389. var
  1390. storefilepos : tfileposinfo;
  1391. cursectype : TAsmSectionType;
  1392. hrec : threc;
  1393. section : ansistring;
  1394. begin
  1395. { mark the staticvarsym as typedconst }
  1396. include(sym.varoptions,vo_is_typed_const);
  1397. { The variable has a value assigned }
  1398. sym.varstate:=vs_initialised;
  1399. { the variable can't be placed in a register }
  1400. sym.varregable:=vr_none;
  1401. { generate data for typed const }
  1402. storefilepos:=current_filepos;
  1403. current_filepos:=sym.fileinfo;
  1404. if sym.varspez=vs_const then
  1405. cursectype:=sec_rodata
  1406. else
  1407. cursectype:=sec_data;
  1408. maybe_new_object_file(list);
  1409. hrec.list:=tasmlist.create;
  1410. hrec.origsym:=sym;
  1411. hrec.offset:=0;
  1412. hrec.origblock:=block_type;
  1413. read_typed_const_data(hrec,sym.vardef);
  1414. { Parse hints }
  1415. try_consume_hintdirective(sym.symoptions,sym.deprecatedmsg);
  1416. consume(_SEMICOLON);
  1417. { parse public/external/export/... }
  1418. if not in_structure and
  1419. (
  1420. (
  1421. (token = _ID) and
  1422. (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) and
  1423. (m_cvar_support in current_settings.modeswitches)
  1424. ) or
  1425. (
  1426. (m_mac in current_settings.modeswitches) and
  1427. (
  1428. (cs_external_var in current_settings.localswitches) or
  1429. (cs_externally_visible in current_settings.localswitches)
  1430. )
  1431. )
  1432. ) then
  1433. read_public_and_external(sym);
  1434. { try to parse a section directive }
  1435. if not in_structure and (target_info.system in systems_allow_section) and
  1436. (symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) and
  1437. (idtoken=_SECTION) then
  1438. begin
  1439. try_consume_sectiondirective(section);
  1440. if section<>'' then
  1441. begin
  1442. if (sym.varoptions *[vo_is_external,vo_is_weak_external])<>[] then
  1443. Message(parser_e_externals_no_section);
  1444. if sym.typ<>staticvarsym then
  1445. Message(parser_e_section_no_locals);
  1446. tstaticvarsym(sym).section:=section;
  1447. include(sym.varoptions, vo_has_section);
  1448. end;
  1449. end;
  1450. { only now add items based on the symbolname, because it may }
  1451. { have been modified by the directives parsed above }
  1452. if vo_has_section in sym.varoptions then
  1453. new_section(list,sec_user,sym.section,const_align(sym.vardef.alignment))
  1454. else
  1455. new_section(list,cursectype,lower(sym.mangledname),const_align(sym.vardef.alignment));
  1456. if (sym.owner.symtabletype=globalsymtable) or
  1457. create_smartlink or
  1458. (assigned(current_procinfo) and
  1459. (po_inline in current_procinfo.procdef.procoptions)) or
  1460. DLLSource then
  1461. list.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,0))
  1462. else
  1463. list.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,0));
  1464. { add the parsed value }
  1465. list.concatlist(hrec.list);
  1466. hrec.list.free;
  1467. list.concat(tai_symbol_end.Createname(sym.mangledname));
  1468. current_filepos:=storefilepos;
  1469. end;
  1470. end.