ptconst.pas 59 KB

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