ptconst.pas 59 KB

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