ngtcon.pp 56 KB

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