ptconst.pas 57 KB

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