ptconst.pas 57 KB

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