ptconst.pas 57 KB

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