pdecvar.pas 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Parses variable declarations. Used for var statement and record
  4. definitions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecvar;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symsym,symdef;
  23. type
  24. tvar_dec_option=(vd_record,vd_object,vd_threadvar);
  25. tvar_dec_options=set of tvar_dec_option;
  26. function read_property_dec(aclass:tobjectdef):tpropertysym;
  27. procedure read_var_decls(options:Tvar_dec_options);
  28. procedure read_record_fields(options:Tvar_dec_options);
  29. implementation
  30. uses
  31. { common }
  32. cutils,cclasses,
  33. { global }
  34. globtype,globals,tokens,verbose,
  35. systems,
  36. { symtable }
  37. symconst,symbase,symtype,symtable,defutil,defcmp,
  38. fmodule,
  39. { pass 1 }
  40. node,pass_1,aasmdata,
  41. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,
  42. { codegen }
  43. ncgutil,
  44. { parser }
  45. scanner,
  46. pbase,pexpr,ptype,ptconst,pdecsub,
  47. { link }
  48. import
  49. ;
  50. function read_property_dec(aclass:tobjectdef):tpropertysym;
  51. { convert a node tree to symlist and return the last
  52. symbol }
  53. function parse_symlist(pl:tsymlist;var def:tdef):boolean;
  54. var
  55. idx : longint;
  56. sym : tsym;
  57. srsymtable : tsymtable;
  58. st : tsymtable;
  59. p : tnode;
  60. begin
  61. result:=true;
  62. def:=nil;
  63. if token=_ID then
  64. begin
  65. if assigned(aclass) then
  66. sym:=search_class_member(aclass,pattern)
  67. else
  68. searchsym(pattern,sym,srsymtable);
  69. if assigned(sym) then
  70. begin
  71. case sym.typ of
  72. fieldvarsym :
  73. begin
  74. if not(sp_private in current_object_option) then
  75. addsymref(sym);
  76. pl.addsym(sl_load,sym);
  77. def:=tfieldvarsym(sym).vartype.def;
  78. end;
  79. procsym :
  80. begin
  81. if not(sp_private in current_object_option) then
  82. addsymref(sym);
  83. pl.addsym(sl_call,sym);
  84. end;
  85. else
  86. begin
  87. Message1(parser_e_illegal_field_or_method,orgpattern);
  88. result:=false;
  89. end;
  90. end;
  91. end
  92. else
  93. begin
  94. Message1(parser_e_illegal_field_or_method,orgpattern);
  95. result:=false;
  96. end;
  97. consume(_ID);
  98. repeat
  99. case token of
  100. _ID,
  101. _SEMICOLON :
  102. begin
  103. break;
  104. end;
  105. _POINT :
  106. begin
  107. consume(_POINT);
  108. if assigned(def) then
  109. begin
  110. st:=def.getsymtable(gs_record);
  111. if assigned(st) then
  112. begin
  113. sym:=tsym(st.search(pattern));
  114. if assigned(sym) then
  115. begin
  116. pl.addsym(sl_subscript,sym);
  117. case sym.typ of
  118. fieldvarsym :
  119. def:=tfieldvarsym(sym).vartype.def;
  120. else
  121. begin
  122. Message1(sym_e_illegal_field,orgpattern);
  123. result:=false;
  124. end;
  125. end;
  126. end
  127. else
  128. begin
  129. Message1(sym_e_illegal_field,orgpattern);
  130. result:=false;
  131. end;
  132. end
  133. else
  134. begin
  135. Message(parser_e_invalid_qualifier);
  136. result:=false;
  137. end;
  138. end
  139. else
  140. begin
  141. Message(parser_e_invalid_qualifier);
  142. result:=false;
  143. end;
  144. consume(_ID);
  145. end;
  146. _LECKKLAMMER :
  147. begin
  148. consume(_LECKKLAMMER);
  149. repeat
  150. if def.deftype=arraydef then
  151. begin
  152. idx:=0;
  153. p:=comp_expr(true);
  154. if (not codegenerror) then
  155. begin
  156. if (p.nodetype=ordconstn) then
  157. begin
  158. if compare_defs(p.resulttype.def,tarraydef(def).rangetype.def,nothingn)>=te_equal then
  159. idx:=tordconstnode(p).value
  160. else
  161. IncompatibleTypes(p.resulttype.def,tarraydef(def).rangetype.def);
  162. end
  163. else
  164. Message(type_e_ordinal_expr_expected)
  165. end;
  166. p.free;
  167. pl.addconst(sl_vec,idx,p.resulttype);
  168. def:=tarraydef(def).elementtype.def;
  169. end
  170. else
  171. begin
  172. Message(parser_e_invalid_qualifier);
  173. result:=false;
  174. end;
  175. until not try_to_consume(_COMMA);
  176. consume(_RECKKLAMMER);
  177. end;
  178. else
  179. begin
  180. Message(parser_e_ill_property_access_sym);
  181. result:=false;
  182. break;
  183. end;
  184. end;
  185. until false;
  186. end
  187. else
  188. begin
  189. Message(parser_e_ill_property_access_sym);
  190. result:=false;
  191. end;
  192. end;
  193. var
  194. sym : tsym;
  195. p : tpropertysym;
  196. overriden : tsym;
  197. varspez : tvarspez;
  198. tt : ttype;
  199. arraytype : ttype;
  200. def : tdef;
  201. pt : tnode;
  202. sc : TFPObjectList;
  203. paranr : word;
  204. i : longint;
  205. hreadparavs,
  206. hparavs : tparavarsym;
  207. readprocdef,
  208. writeprocdef : tprocvardef;
  209. begin
  210. { Generate temp procvardefs to search for matching read/write
  211. procedures. the readprocdef will store all definitions }
  212. paranr:=0;
  213. readprocdef:=tprocvardef.create(normal_function_level);
  214. writeprocdef:=tprocvardef.create(normal_function_level);
  215. { make it method pointers }
  216. if assigned(aclass) then
  217. begin
  218. include(readprocdef.procoptions,po_methodpointer);
  219. include(writeprocdef.procoptions,po_methodpointer);
  220. end;
  221. if token<>_ID then
  222. begin
  223. consume(_ID);
  224. consume(_SEMICOLON);
  225. exit;
  226. end;
  227. { Generate propertysym and insert in symtablestack }
  228. p:=tpropertysym.create(orgpattern);
  229. symtablestack.top.insert(p);
  230. consume(_ID);
  231. { property parameters ? }
  232. if try_to_consume(_LECKKLAMMER) then
  233. begin
  234. if (sp_published in current_object_option) and
  235. not (m_delphi in aktmodeswitches) then
  236. Message(parser_e_cant_publish_that_property);
  237. { create a list of the parameters }
  238. symtablestack.push(readprocdef.parast);
  239. sc:=TFPObjectList.create(false);
  240. inc(testcurobject);
  241. repeat
  242. if try_to_consume(_VAR) then
  243. varspez:=vs_var
  244. else if try_to_consume(_CONST) then
  245. varspez:=vs_const
  246. else if (m_out in aktmodeswitches) and try_to_consume(_OUT) then
  247. varspez:=vs_out
  248. else
  249. varspez:=vs_value;
  250. sc.clear;
  251. repeat
  252. inc(paranr);
  253. hreadparavs:=tparavarsym.create(orgpattern,10*paranr,varspez,generrortype,[]);
  254. readprocdef.parast.insert(hreadparavs);
  255. sc.add(hreadparavs);
  256. consume(_ID);
  257. until not try_to_consume(_COMMA);
  258. if try_to_consume(_COLON) then
  259. begin
  260. if try_to_consume(_ARRAY) then
  261. begin
  262. consume(_OF);
  263. { define range and type of range }
  264. tt.setdef(tarraydef.create(0,-1,s32inttype));
  265. { define field type }
  266. single_type(arraytype,false);
  267. tarraydef(tt.def).setelementtype(arraytype);
  268. end
  269. else
  270. single_type(tt,false);
  271. end
  272. else
  273. tt:=cformaltype;
  274. for i:=0 to sc.count-1 do
  275. begin
  276. hreadparavs:=tparavarsym(sc[i]);
  277. hreadparavs.vartype:=tt;
  278. { also update the writeprocdef }
  279. hparavs:=tparavarsym.create(hreadparavs.realname,hreadparavs.paranr,vs_value,tt,[]);
  280. writeprocdef.parast.insert(hparavs);
  281. end;
  282. until not try_to_consume(_SEMICOLON);
  283. sc.free;
  284. dec(testcurobject);
  285. symtablestack.pop(readprocdef.parast);
  286. consume(_RECKKLAMMER);
  287. { the parser need to know if a property has parameters, the
  288. index parameter doesn't count (PFV) }
  289. if paranr>0 then
  290. include(p.propoptions,ppo_hasparameters);
  291. end;
  292. { overriden property ? }
  293. { force property interface
  294. there is a property parameter
  295. a global property }
  296. if (token=_COLON) or (paranr>0) or (aclass=nil) then
  297. begin
  298. consume(_COLON);
  299. single_type(p.proptype,false);
  300. if (idtoken=_INDEX) then
  301. begin
  302. consume(_INDEX);
  303. pt:=comp_expr(true);
  304. { Only allow enum and integer indexes. Convert all integer
  305. values to s32int to be compatible with delphi, because the
  306. procedure matching requires equal parameters }
  307. if is_constnode(pt) and
  308. is_ordinal(pt.resulttype.def)
  309. {$ifndef cpu64bit}
  310. and (not is_64bitint(pt.resulttype.def))
  311. {$endif cpu64bit}
  312. then
  313. begin
  314. if is_integer(pt.resulttype.def) then
  315. inserttypeconv_internal(pt,s32inttype);
  316. p.index:=tordconstnode(pt).value;
  317. end
  318. else
  319. begin
  320. Message(parser_e_invalid_property_index_value);
  321. p.index:=0;
  322. end;
  323. p.indextype:=pt.resulttype;
  324. include(p.propoptions,ppo_indexed);
  325. { concat a longint to the para templates }
  326. inc(paranr);
  327. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indextype,[]);
  328. readprocdef.parast.insert(hparavs);
  329. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indextype,[]);
  330. writeprocdef.parast.insert(hparavs);
  331. pt.free;
  332. end;
  333. end
  334. else
  335. begin
  336. { do an property override }
  337. overriden:=search_class_member(aclass.childof,p.name);
  338. if assigned(overriden) and (overriden.typ=propertysym) and not(is_dispinterface(aclass)) then
  339. begin
  340. p.dooverride(tpropertysym(overriden));
  341. end
  342. else
  343. begin
  344. p.proptype:=generrortype;
  345. message(parser_e_no_property_found_to_override);
  346. end;
  347. end;
  348. if ((sp_published in current_object_option) or is_dispinterface(aclass)) and
  349. not(p.proptype.def.is_publishable) then
  350. Message(parser_e_cant_publish_that_property);
  351. if not(is_dispinterface(aclass)) then
  352. begin
  353. if try_to_consume(_READ) then
  354. begin
  355. p.readaccess.clear;
  356. if parse_symlist(p.readaccess,def) then
  357. begin
  358. sym:=p.readaccess.firstsym^.sym;
  359. case sym.typ of
  360. procsym :
  361. begin
  362. { read is function returning the type of the property }
  363. readprocdef.rettype:=p.proptype;
  364. { Insert hidden parameters }
  365. handle_calling_convention(readprocdef);
  366. { search procdefs matching readprocdef }
  367. { we ignore hidden stuff here because the property access symbol might have
  368. non default calling conventions which might change the hidden stuff;
  369. see tw3216.pp (FK) }
  370. p.readaccess.procdef:=Tprocsym(sym).search_procdef_bypara(readprocdef.paras,p.proptype.def,[cpo_allowdefaults,cpo_ignorehidden]);
  371. if not assigned(p.readaccess.procdef) then
  372. Message(parser_e_ill_property_access_sym);
  373. end;
  374. fieldvarsym :
  375. begin
  376. if not assigned(def) then
  377. internalerror(200310071);
  378. if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
  379. begin
  380. { property parameters are allowed if this is
  381. an indexed property, because the index is then
  382. the parameter.
  383. Note: In the help of Kylix it is written
  384. that it isn't allowed, but the compiler accepts it (PFV) }
  385. if (ppo_hasparameters in p.propoptions) then
  386. Message(parser_e_ill_property_access_sym);
  387. end
  388. else
  389. IncompatibleTypes(def,p.proptype.def);
  390. end;
  391. else
  392. Message(parser_e_ill_property_access_sym);
  393. end;
  394. end;
  395. end;
  396. if try_to_consume(_WRITE) then
  397. begin
  398. p.writeaccess.clear;
  399. if parse_symlist(p.writeaccess,def) then
  400. begin
  401. sym:=p.writeaccess.firstsym^.sym;
  402. case sym.typ of
  403. procsym :
  404. begin
  405. { write is a procedure with an extra value parameter
  406. of the of the property }
  407. writeprocdef.rettype:=voidtype;
  408. inc(paranr);
  409. hparavs:=tparavarsym.create('$value',10*paranr,vs_value,p.proptype,[]);
  410. writeprocdef.parast.insert(hparavs);
  411. { Insert hidden parameters }
  412. handle_calling_convention(writeprocdef);
  413. { search procdefs matching writeprocdef }
  414. p.writeaccess.procdef:=Tprocsym(sym).search_procdef_bypara(writeprocdef.paras,writeprocdef.rettype.def,[cpo_allowdefaults]);
  415. if not assigned(p.writeaccess.procdef) then
  416. Message(parser_e_ill_property_access_sym);
  417. end;
  418. fieldvarsym :
  419. begin
  420. if not assigned(def) then
  421. internalerror(200310072);
  422. if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
  423. begin
  424. { property parameters are allowed if this is
  425. an indexed property, because the index is then
  426. the parameter.
  427. Note: In the help of Kylix it is written
  428. that it isn't allowed, but the compiler accepts it (PFV) }
  429. if (ppo_hasparameters in p.propoptions) then
  430. Message(parser_e_ill_property_access_sym);
  431. end
  432. else
  433. IncompatibleTypes(def,p.proptype.def);
  434. end;
  435. else
  436. Message(parser_e_ill_property_access_sym);
  437. end;
  438. end;
  439. end;
  440. end
  441. else
  442. begin
  443. if try_to_consume(_READONLY) then
  444. begin
  445. end
  446. else if try_to_consume(_WRITEONLY) then
  447. begin
  448. end;
  449. if try_to_consume(_DISPID) then
  450. begin
  451. pt:=comp_expr(true);
  452. if is_constintnode(pt) then
  453. // tprocdef(pd).extnumber:=tordconstnode(pt).value
  454. else
  455. Message(parser_e_dispid_must_be_ord_const);
  456. pt.free;
  457. end;
  458. end;
  459. if assigned(aclass) and not(is_dispinterface(aclass)) then
  460. begin
  461. { ppo_stored might be not set by an overridden property }
  462. if not(ppo_is_override in p.propoptions) then
  463. include(p.propoptions,ppo_stored);
  464. if try_to_consume(_STORED) then
  465. begin
  466. include(p.propoptions,ppo_stored);
  467. p.storedaccess.clear;
  468. case token of
  469. _ID:
  470. begin
  471. { in the case that idtoken=_DEFAULT }
  472. { we have to do nothing except }
  473. { setting ppo_stored, it's the same }
  474. { as stored true }
  475. if idtoken<>_DEFAULT then
  476. begin
  477. if parse_symlist(p.storedaccess,def) then
  478. begin
  479. sym:=p.storedaccess.firstsym^.sym;
  480. case sym.typ of
  481. procsym :
  482. begin
  483. p.storedaccess.procdef:=Tprocsym(sym).search_procdef_nopara_boolret;
  484. if not assigned(p.storedaccess.procdef) then
  485. message(parser_e_ill_property_storage_sym);
  486. end;
  487. fieldvarsym :
  488. begin
  489. if not assigned(def) then
  490. internalerror(200310073);
  491. if (ppo_hasparameters in p.propoptions) or
  492. not(is_boolean(def)) then
  493. Message(parser_e_stored_property_must_be_boolean);
  494. end;
  495. else
  496. Message(parser_e_ill_property_access_sym);
  497. end;
  498. end;
  499. end;
  500. end;
  501. _FALSE:
  502. begin
  503. consume(_FALSE);
  504. exclude(p.propoptions,ppo_stored);
  505. end;
  506. _TRUE:
  507. begin
  508. p.default:=longint($80000000);
  509. consume(_TRUE);
  510. end;
  511. end;
  512. end;
  513. end;
  514. if try_to_consume(_DEFAULT) then
  515. begin
  516. if not(is_ordinal(p.proptype.def) or
  517. {$ifndef cpu64bit}
  518. is_64bitint(p.proptype.def) or
  519. {$endif cpu64bit}
  520. is_class(p.proptype.def) or
  521. is_single(p.proptype.def) or
  522. (p.proptype.def.deftype in [classrefdef,pointerdef]) or
  523. ((p.proptype.def.deftype=setdef) and
  524. (tsetdef(p.proptype.def).settype=smallset))) or
  525. ((p.proptype.def.deftype=arraydef) and
  526. (ppo_indexed in p.propoptions)) or
  527. (ppo_hasparameters in p.propoptions) then
  528. begin
  529. Message(parser_e_property_cant_have_a_default_value);
  530. { Error recovery }
  531. pt:=comp_expr(true);
  532. pt.free;
  533. end
  534. else
  535. begin
  536. { Get the result of the default, the firstpass is
  537. needed to support values like -1 }
  538. pt:=comp_expr(true);
  539. if (p.proptype.def.deftype=setdef) and
  540. (pt.nodetype=arrayconstructorn) then
  541. begin
  542. arrayconstructor_to_set(pt);
  543. do_resulttypepass(pt);
  544. end;
  545. inserttypeconv(pt,p.proptype);
  546. if not(is_constnode(pt)) then
  547. Message(parser_e_property_default_value_must_const);
  548. { Set default value }
  549. case pt.nodetype of
  550. setconstn :
  551. p.default:=plongint(tsetconstnode(pt).value_set)^;
  552. ordconstn :
  553. p.default:=longint(tordconstnode(pt).value);
  554. niln :
  555. p.default:=0;
  556. realconstn:
  557. p.default:=longint(single(trealconstnode(pt).value_real));
  558. end;
  559. pt.free;
  560. end;
  561. end
  562. else if try_to_consume(_NODEFAULT) then
  563. begin
  564. p.default:=longint($80000000);
  565. end;
  566. { Parse possible "implements" keyword }
  567. if try_to_consume(_IMPLEMENTS) then
  568. begin
  569. consume(_ID);
  570. writeln('Implements [', pattern, ']');
  571. if not is_interface(p.proptype.def) then
  572. writeln('Implements property must have interface type'); //FIXME: will be converted to proper message()
  573. end;
  574. { remove temporary procvardefs }
  575. readprocdef.free;
  576. writeprocdef.free;
  577. result:=p;
  578. end;
  579. function maybe_parse_proc_directives(const tt:ttype):boolean;
  580. var
  581. newtype : ttypesym;
  582. begin
  583. result:=false;
  584. { Process procvar directives before = and ; }
  585. if (tt.def.deftype=procvardef) and
  586. (tt.def.typesym=nil) and
  587. check_proc_directive(true) then
  588. begin
  589. newtype:=ttypesym.create('unnamed',tt);
  590. parse_var_proc_directives(tsym(newtype));
  591. newtype.restype.def:=nil;
  592. tt.def.typesym:=nil;
  593. newtype.free;
  594. result:=true;
  595. end;
  596. end;
  597. const
  598. variantrecordlevel : longint = 0;
  599. procedure read_var_decls(options:Tvar_dec_options);
  600. procedure read_default_value(sc : TFPObjectList;tt : ttype;is_threadvar : boolean);
  601. var
  602. vs : tabstractnormalvarsym;
  603. tcsym : ttypedconstsym;
  604. begin
  605. vs:=tabstractnormalvarsym(sc[0]);
  606. if sc.count>1 then
  607. Message(parser_e_initialized_only_one_var);
  608. if is_threadvar then
  609. Message(parser_e_initialized_not_for_threadvar);
  610. if symtablestack.top.symtabletype=localsymtable then
  611. begin
  612. consume(_EQUAL);
  613. tcsym:=ttypedconstsym.createtype('$default'+vs.realname,tt,false);
  614. include(tcsym.symoptions,sp_internal);
  615. vs.defaultconstsym:=tcsym;
  616. symtablestack.top.insert(tcsym);
  617. readtypedconst(current_asmdata.asmlists[al_typedconsts],tt,tcsym,false);
  618. { The variable has a value assigned }
  619. vs.varstate:=vs_initialised;
  620. end
  621. else
  622. begin
  623. tcsym:=ttypedconstsym.createtype(vs.realname,tt,true);
  624. tcsym.fileinfo:=vs.fileinfo;
  625. symtablestack.top.replace(vs,tcsym);
  626. vs.free;
  627. consume(_EQUAL);
  628. readtypedconst(current_asmdata.asmlists[al_typedconsts],tt,tcsym,true);
  629. end;
  630. end;
  631. var
  632. sc : TFPObjectList;
  633. i : longint;
  634. old_block_type : tblock_type;
  635. symdone : boolean;
  636. { to handle absolute }
  637. abssym : tabsolutevarsym;
  638. { c var }
  639. is_dll,
  640. hasdefaultvalue,
  641. is_gpc_name,is_cdecl,
  642. extern_var,export_var : boolean;
  643. old_current_object_option : tsymoptions;
  644. hs,sorg,C_name,dll_name : string;
  645. tt : ttype;
  646. hp,pt : tnode;
  647. vs : tabstractvarsym;
  648. hintsymoptions : tsymoptions;
  649. semicolonatend,semicoloneaten: boolean;
  650. begin
  651. old_current_object_option:=current_object_option;
  652. { all variables are public if not in a object declaration }
  653. current_object_option:=[sp_public];
  654. old_block_type:=block_type;
  655. block_type:=bt_type;
  656. is_gpc_name:=false;
  657. { Force an expected ID error message }
  658. if not (token in [_ID,_CASE,_END]) then
  659. consume(_ID);
  660. { read vars }
  661. sc:=TFPObjectList.create(false);
  662. while (token=_ID) do
  663. begin
  664. sorg:=orgpattern;
  665. semicoloneaten:=false;
  666. hasdefaultvalue:=false;
  667. symdone:=false;
  668. sc.clear;
  669. repeat
  670. if (token = _ID) then
  671. begin
  672. case symtablestack.top.symtabletype of
  673. localsymtable :
  674. vs:=tlocalvarsym.create(orgpattern,vs_value,generrortype,[]);
  675. staticsymtable,
  676. globalsymtable :
  677. vs:=tglobalvarsym.create(orgpattern,vs_value,generrortype,[]);
  678. else
  679. internalerror(200411064);
  680. end;
  681. sc.add(vs);
  682. symtablestack.top.insert(vs);
  683. end;
  684. consume(_ID);
  685. until not try_to_consume(_COMMA);
  686. consume(_COLON);
  687. if (m_gpc in aktmodeswitches) and
  688. (token=_ID) and
  689. (orgpattern='__asmname__') then
  690. begin
  691. consume(_ID);
  692. C_name:=get_stringconst;
  693. Is_gpc_name:=true;
  694. end;
  695. { this is needed for Delphi mode at least
  696. but should be OK for all modes !! (PM) }
  697. ignore_equal:=true;
  698. read_anon_type(tt,false);
  699. ignore_equal:=false;
  700. { Process procvar directives }
  701. if maybe_parse_proc_directives(tt) then
  702. semicoloneaten:=true;
  703. if is_gpc_name then
  704. begin
  705. vs:=tabstractvarsym(sc[0]);
  706. if sc.count>1 then
  707. Message(parser_e_absolute_only_one_var);
  708. vs.vartype:=tt;
  709. if vs.typ=globalvarsym then
  710. begin
  711. tglobalvarsym(vs).set_mangledname(target_info.Cprefix+sorg);
  712. include(vs.varoptions,vo_is_C_var);
  713. include(vs.varoptions,vo_is_external);
  714. end
  715. else
  716. Message(parser_e_no_local_var_external);
  717. symdone:=true;
  718. end;
  719. { check for absolute }
  720. if not symdone and
  721. try_to_consume(_ABSOLUTE) then
  722. begin
  723. abssym:=nil;
  724. { only allowed for one var }
  725. vs:=tabstractvarsym(sc[0]);
  726. if sc.count>1 then
  727. Message(parser_e_absolute_only_one_var);
  728. { parse the rest }
  729. pt:=expr;
  730. { check allowed absolute types }
  731. if (pt.nodetype=stringconstn) or
  732. (is_constcharnode(pt)) then
  733. begin
  734. abssym:=tabsolutevarsym.create(vs.realname,tt);
  735. abssym.fileinfo:=vs.fileinfo;
  736. if pt.nodetype=stringconstn then
  737. hs:=strpas(tstringconstnode(pt).value_str)
  738. else
  739. hs:=chr(tordconstnode(pt).value);
  740. consume(token);
  741. abssym.abstyp:=toasm;
  742. abssym.asmname:=stringdup(hs);
  743. { replace the varsym }
  744. symtablestack.top.replace(vs,abssym);
  745. vs.free;
  746. end
  747. { address }
  748. else if is_constintnode(pt) and
  749. ((target_info.system in [system_i386_go32v2,system_i386_watcom,
  750. system_i386_wdosx,system_i386_win32,
  751. system_arm_wince,system_i386_wince,
  752. system_arm_gba]) or
  753. (m_objfpc in aktmodeswitches) or
  754. (m_delphi in aktmodeswitches)) then
  755. begin
  756. abssym:=tabsolutevarsym.create(vs.realname,tt);
  757. abssym.fileinfo:=vs.fileinfo;
  758. abssym.abstyp:=toaddr;
  759. abssym.addroffset:=tordconstnode(pt).value;
  760. {$ifdef i386}
  761. abssym.absseg:=false;
  762. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  763. try_to_consume(_COLON) then
  764. begin
  765. pt.free;
  766. pt:=expr;
  767. if is_constintnode(pt) then
  768. begin
  769. abssym.addroffset:=abssym.addroffset shl 4+tordconstnode(pt).value;
  770. abssym.absseg:=true;
  771. end
  772. else
  773. Message(type_e_ordinal_expr_expected);
  774. end;
  775. {$endif i386}
  776. symtablestack.top.replace(vs,abssym);
  777. vs.free;
  778. end
  779. { variable }
  780. else
  781. begin
  782. { remove subscriptn before checking for loadn }
  783. hp:=pt;
  784. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  785. hp:=tunarynode(hp).left;
  786. if (hp.nodetype=loadn) then
  787. begin
  788. { we should check the result type of loadn }
  789. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,globalvarsym,localvarsym,
  790. paravarsym,typedconstsym]) then
  791. Message(parser_e_absolute_only_to_var_or_const);
  792. abssym:=tabsolutevarsym.create(vs.realname,tt);
  793. abssym.fileinfo:=vs.fileinfo;
  794. abssym.abstyp:=tovar;
  795. abssym.ref:=node_to_symlist(pt);
  796. symtablestack.top.replace(vs,abssym);
  797. vs.free;
  798. end
  799. else
  800. Message(parser_e_absolute_only_to_var_or_const);
  801. end;
  802. if assigned(abssym) then
  803. begin
  804. { try to consume the hint directives with absolute symbols }
  805. hintsymoptions:=[];
  806. try_consume_hintdirective(hintsymoptions);
  807. abssym.symoptions := abssym.symoptions + hintsymoptions;
  808. end;
  809. pt.free;
  810. symdone:=true;
  811. end;
  812. { try to parse the hint directives }
  813. hintsymoptions:=[];
  814. try_consume_hintdirective(hintsymoptions);
  815. { Handling of Delphi typed const = initialized vars }
  816. if (token=_EQUAL) and
  817. not(m_tp7 in aktmodeswitches) and
  818. (symtablestack.top.symtabletype<>parasymtable) then
  819. begin
  820. { Add calling convention for procvar }
  821. if (tt.def.deftype=procvardef) and
  822. (tt.def.typesym=nil) then
  823. handle_calling_convention(tprocvardef(tt.def));
  824. read_default_value(sc,tt,vd_threadvar in options);
  825. consume(_SEMICOLON);
  826. { for locals we've created typedconstsym with a different name }
  827. if symtablestack.top.symtabletype<>localsymtable then
  828. symdone:=true;
  829. hasdefaultvalue:=true;
  830. end
  831. else
  832. begin
  833. if not(semicoloneaten) then
  834. consume(_SEMICOLON);
  835. end;
  836. { Support calling convention for procvars after semicolon }
  837. if not(hasdefaultvalue) and
  838. (tt.def.deftype=procvardef) and
  839. (tt.def.typesym=nil) then
  840. begin
  841. { Parse procvar directives after ; }
  842. maybe_parse_proc_directives(tt);
  843. { Add calling convention for procvar }
  844. handle_calling_convention(tprocvardef(tt.def));
  845. { Handling of Delphi typed const = initialized vars }
  846. if (token=_EQUAL) and
  847. not(m_tp7 in aktmodeswitches) and
  848. (symtablestack.top.symtabletype<>parasymtable) then
  849. begin
  850. read_default_value(sc,tt,vd_threadvar in options);
  851. consume(_SEMICOLON);
  852. symdone:=true;
  853. hasdefaultvalue:=true;
  854. end;
  855. end;
  856. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  857. if not symdone then
  858. begin
  859. if (
  860. (token=_ID) and
  861. (m_cvar_support in aktmodeswitches) and
  862. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR])
  863. ) or
  864. (
  865. (m_mac in aktmodeswitches) and
  866. ((cs_external_var in aktlocalswitches) or (cs_externally_visible in aktlocalswitches))
  867. ) then
  868. begin
  869. { only allowed for one var }
  870. vs:=tabstractvarsym(sc[0]);
  871. if sc.count>1 then
  872. Message(parser_e_absolute_only_one_var);
  873. { set type of the var }
  874. vs.vartype:=tt;
  875. vs.symoptions := vs.symoptions + hintsymoptions;
  876. { defaults }
  877. is_dll:=false;
  878. is_cdecl:=false;
  879. extern_var:=false;
  880. export_var:=false;
  881. C_name:=sorg;
  882. semicolonatend:= false;
  883. { cdecl }
  884. if try_to_consume(_CVAR) then
  885. begin
  886. consume(_SEMICOLON);
  887. is_cdecl:=true;
  888. C_name:=target_info.Cprefix+sorg;
  889. end;
  890. { external }
  891. if try_to_consume(_EXTERNAL) then
  892. begin
  893. extern_var:=true;
  894. semicolonatend:= true;
  895. end;
  896. { macpas specific handling due to some switches}
  897. if (m_mac in aktmodeswitches) then
  898. begin
  899. if (cs_external_var in aktlocalswitches) then
  900. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  901. is_cdecl:=true;
  902. C_name:=target_info.Cprefix+sorg;
  903. extern_var:=true;
  904. end
  905. else if (cs_externally_visible in aktlocalswitches) then
  906. begin {The effect of this is the same as if cvar has been given as directives.}
  907. is_cdecl:=true;
  908. C_name:=target_info.Cprefix+sorg;
  909. end;
  910. vs.varregable := vr_none;
  911. end;
  912. { export }
  913. if idtoken in [_EXPORT,_PUBLIC] then
  914. begin
  915. consume(_ID);
  916. if extern_var then
  917. Message(parser_e_not_external_and_export)
  918. else
  919. begin
  920. export_var:=true;
  921. semicolonatend:= true;
  922. end;
  923. end;
  924. { external and export need a name after when no cdecl is used }
  925. if not is_cdecl then
  926. begin
  927. { dll name ? }
  928. if (extern_var) and (idtoken<>_NAME) then
  929. begin
  930. is_dll:=true;
  931. dll_name:=get_stringconst;
  932. end;
  933. if try_to_consume(_NAME) then
  934. C_name:=get_stringconst
  935. else
  936. C_name:=sorg;
  937. end;
  938. { consume the ; when export or external is used }
  939. if semicolonatend then
  940. consume(_SEMICOLON);
  941. { set some vars options }
  942. if is_dll then
  943. include(vs.varoptions,vo_is_dll_var)
  944. else
  945. include(vs.varoptions,vo_is_C_var);
  946. if (is_dll) and
  947. (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  948. C_Name := target_info.Cprefix+C_Name;
  949. if export_var then
  950. begin
  951. inc(vs.refs);
  952. include(vs.varoptions,vo_is_exported);
  953. end;
  954. if extern_var then
  955. include(vs.varoptions,vo_is_external);
  956. if vs.typ=globalvarsym then
  957. begin
  958. tglobalvarsym(vs).set_mangledname(C_Name);
  959. { insert in the al_globals when it is not external }
  960. if (not extern_var) then
  961. insertbssdata(tglobalvarsym(vs));
  962. { now we can insert it in the import lib if its a dll, or
  963. add it to the externals }
  964. if extern_var then
  965. begin
  966. vs.varregable := vr_none;
  967. if is_dll then
  968. begin
  969. if not(current_module.uses_imports) then
  970. begin
  971. current_module.uses_imports:=true;
  972. importlib.preparelib(current_module.realmodulename^);
  973. end;
  974. importlib.importvariable(tglobalvarsym(vs),C_name,dll_name);
  975. end
  976. else
  977. if tf_has_dllscanner in target_info.flags then
  978. current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
  979. end;
  980. end
  981. else
  982. Message(parser_e_no_local_var_external);
  983. symdone:=true;
  984. end;
  985. end;
  986. { insert it in the symtable, if not done yet }
  987. if not symdone then
  988. begin
  989. for i:=0 to sc.count-1 do
  990. begin
  991. vs:=tabstractvarsym(sc[i]);
  992. vs.vartype:=tt;
  993. { insert any additional hint directives }
  994. vs.symoptions := vs.symoptions + hintsymoptions;
  995. if vd_threadvar in options then
  996. include(vs.varoptions,vo_is_thread_var);
  997. { static data fields are inserted in the globalsymtable }
  998. if vs.typ=globalvarsym then
  999. insertbssdata(tglobalvarsym(vs));
  1000. end;
  1001. end;
  1002. end;
  1003. block_type:=old_block_type;
  1004. current_object_option:=old_current_object_option;
  1005. { free the list }
  1006. sc.free;
  1007. end;
  1008. procedure read_record_fields(options:Tvar_dec_options);
  1009. var
  1010. sc : TFPObjectList;
  1011. i : longint;
  1012. old_block_type : tblock_type;
  1013. old_current_object_option : tsymoptions;
  1014. hs,sorg : string;
  1015. tt,casetype : ttype;
  1016. { maxsize contains the max. size of a variant }
  1017. { startvarrec contains the start of the variant part of a record }
  1018. maxsize, startvarrecsize : longint;
  1019. usedalign,
  1020. maxalignment,startvarrecalign,
  1021. maxpadalign, startpadalign: shortint;
  1022. pt : tnode;
  1023. fieldvs : tfieldvarsym;
  1024. hstaticvs : tglobalvarsym;
  1025. vs : tabstractvarsym;
  1026. srsym : tsym;
  1027. srsymtable : tsymtable;
  1028. recst : tabstractrecordsymtable;
  1029. unionsymtable : trecordsymtable;
  1030. offset : longint;
  1031. uniondef : trecorddef;
  1032. unionsym : tfieldvarsym;
  1033. uniontype : ttype;
  1034. hintsymoptions : tsymoptions;
  1035. semicoloneaten: boolean;
  1036. {$ifdef powerpc}
  1037. tempdef: tdef;
  1038. is_first_field: boolean;
  1039. {$endif powerpc}
  1040. begin
  1041. recst:=tabstractrecordsymtable(symtablestack.top);
  1042. {$ifdef powerpc}
  1043. is_first_field := true;
  1044. {$endif powerpc}
  1045. old_current_object_option:=current_object_option;
  1046. { all variables are public if not in a object declaration }
  1047. if not(vd_object in options) then
  1048. current_object_option:=[sp_public];
  1049. old_block_type:=block_type;
  1050. block_type:=bt_type;
  1051. { Force an expected ID error message }
  1052. if not (token in [_ID,_CASE,_END]) then
  1053. consume(_ID);
  1054. { read vars }
  1055. sc:=TFPObjectList.create(false);
  1056. while (token=_ID) and
  1057. not((vd_object in options) and
  1058. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1059. begin
  1060. sorg:=orgpattern;
  1061. semicoloneaten:=false;
  1062. sc.clear;
  1063. repeat
  1064. if try_to_consume(_ID) then
  1065. begin
  1066. vs:=tfieldvarsym.create(orgpattern,vs_value,generrortype,[]);
  1067. sc.add(vs);
  1068. recst.insert(vs);
  1069. end;
  1070. until not try_to_consume(_COMMA);
  1071. consume(_COLON);
  1072. { Don't search in the recordsymtable for types }
  1073. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1074. symtablestack.pop(recst);
  1075. ignore_equal:=true;
  1076. read_anon_type(tt,false);
  1077. ignore_equal:=false;
  1078. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1079. symtablestack.push(recst);
  1080. { Process procvar directives }
  1081. if maybe_parse_proc_directives(tt) then
  1082. semicoloneaten:=true;
  1083. {$ifdef powerpc}
  1084. { from gcc/gcc/config/rs6000/rs6000.h:
  1085. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1086. /* Return the alignment of a struct based on the Macintosh PowerPC
  1087. alignment rules. In general the alignment of a struct is
  1088. determined by the greatest alignment of its elements. However, the
  1089. PowerPC rules cause the alignment of a struct to peg at word
  1090. alignment except when the first field has greater than word
  1091. (32-bit) alignment, in which case the alignment is determined by
  1092. the alignment of the first field. */
  1093. }
  1094. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos]) and
  1095. is_first_field and
  1096. (symtablestack.top.symtabletype = recordsymtable) and
  1097. (trecordsymtable(symtablestack.top).usefieldalignment = -1) then
  1098. begin
  1099. tempdef := tt.def;
  1100. while tempdef.deftype = arraydef do
  1101. tempdef := tarraydef(tempdef).elementtype.def;
  1102. if tempdef.deftype <> recorddef then
  1103. maxpadalign := tempdef.alignment
  1104. else
  1105. maxpadalign := trecorddef(tempdef).padalignment;
  1106. if (maxpadalign > 4) and
  1107. (maxpadalign > trecordsymtable(symtablestack.top).padalignment) then
  1108. trecordsymtable(symtablestack.top).padalignment := maxpadalign;
  1109. is_first_field := false;
  1110. end;
  1111. {$endif powerpc}
  1112. { types that use init/final are not allowed in variant parts, but
  1113. classes are allowed }
  1114. if (variantrecordlevel>0) and
  1115. (tt.def.needs_inittable and not is_class(tt.def)) then
  1116. Message(parser_e_cant_use_inittable_here);
  1117. { try to parse the hint directives }
  1118. hintsymoptions:=[];
  1119. try_consume_hintdirective(hintsymoptions);
  1120. { Records and objects can't have default values }
  1121. { for a record there doesn't need to be a ; before the END or ) }
  1122. if not(token in [_END,_RKLAMMER]) and
  1123. not(semicoloneaten) then
  1124. consume(_SEMICOLON);
  1125. { Parse procvar directives after ; }
  1126. maybe_parse_proc_directives(tt);
  1127. { Add calling convention for procvar }
  1128. if (tt.def.deftype=procvardef) and
  1129. (tt.def.typesym=nil) then
  1130. handle_calling_convention(tprocvardef(tt.def));
  1131. { Check for STATIC directive }
  1132. if (vd_object in options) and
  1133. (cs_static_keyword in aktmoduleswitches) and
  1134. (try_to_consume(_STATIC)) then
  1135. begin
  1136. include(current_object_option,sp_static);
  1137. consume(_SEMICOLON);
  1138. end;
  1139. if (sp_published in current_object_option) and
  1140. not(is_class(tt.def)) then
  1141. begin
  1142. Message(parser_e_cant_publish_that);
  1143. exclude(current_object_option,sp_published);
  1144. { recover by changing access type to public }
  1145. for i:=0 to sc.count-1 do
  1146. begin
  1147. fieldvs:=tfieldvarsym(sc[i]);
  1148. exclude(fieldvs.symoptions,sp_published);
  1149. include(fieldvs.symoptions,sp_public);
  1150. end;
  1151. end
  1152. else
  1153. if (sp_published in current_object_option) and
  1154. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  1155. begin
  1156. Message(parser_e_only_publishable_classes_can__be_published);
  1157. exclude(current_object_option,sp_published);
  1158. end;
  1159. { update variable options }
  1160. for i:=0 to sc.count-1 do
  1161. begin
  1162. fieldvs:=tfieldvarsym(sc[i]);
  1163. fieldvs.vartype:=tt;
  1164. { insert any additional hint directives }
  1165. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1166. if (sp_static in current_object_option) then
  1167. include(fieldvs.symoptions,sp_static);
  1168. { static data fields are inserted in the globalsymtable }
  1169. if (sp_static in current_object_option) then
  1170. begin
  1171. hstaticvs:=tglobalvarsym.create('$'+lower(symtablestack.top.name^)+'_'+fieldvs.name,vs_value,tt,[]);
  1172. recst.defowner.owner.insert(hstaticvs);
  1173. insertbssdata(hstaticvs);
  1174. end
  1175. else
  1176. recst.addfield(fieldvs);
  1177. end;
  1178. { restore current_object_option, it can be changed for
  1179. publishing or static }
  1180. current_object_option:=old_current_object_option;
  1181. end;
  1182. { Check for Case }
  1183. if (vd_record in options) and
  1184. try_to_consume(_CASE) then
  1185. begin
  1186. maxsize:=0;
  1187. maxalignment:=0;
  1188. maxpadalign:=0;
  1189. { including a field declaration? }
  1190. fieldvs:=nil;
  1191. sorg:=orgpattern;
  1192. hs:=pattern;
  1193. searchsym(hs,srsym,srsymtable);
  1194. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1195. begin
  1196. consume(_ID);
  1197. consume(_COLON);
  1198. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrortype,[]);
  1199. symtablestack.top.insert(fieldvs);
  1200. end;
  1201. read_anon_type(casetype,true);
  1202. if assigned(fieldvs) then
  1203. begin
  1204. fieldvs.vartype:=casetype;
  1205. recst.addfield(fieldvs);
  1206. end;
  1207. if not(is_ordinal(casetype.def))
  1208. {$ifndef cpu64bit}
  1209. or is_64bitint(casetype.def)
  1210. {$endif cpu64bit}
  1211. then
  1212. Message(type_e_ordinal_expr_expected);
  1213. consume(_OF);
  1214. UnionSymtable:=trecordsymtable.create(aktpackrecords);
  1215. UnionDef:=trecorddef.create(unionsymtable);
  1216. uniondef.isunion:=true;
  1217. startvarrecsize:=UnionSymtable.datasize;
  1218. startvarrecalign:=UnionSymtable.fieldalignment;
  1219. startpadalign:=Unionsymtable.padalignment;
  1220. symtablestack.push(UnionSymtable);
  1221. repeat
  1222. repeat
  1223. pt:=comp_expr(true);
  1224. if not(pt.nodetype=ordconstn) then
  1225. Message(parser_e_illegal_expression);
  1226. pt.free;
  1227. if token=_COMMA then
  1228. consume(_COMMA)
  1229. else
  1230. break;
  1231. until false;
  1232. consume(_COLON);
  1233. { read the vars }
  1234. consume(_LKLAMMER);
  1235. inc(variantrecordlevel);
  1236. if token<>_RKLAMMER then
  1237. read_record_fields([vd_record]);
  1238. dec(variantrecordlevel);
  1239. consume(_RKLAMMER);
  1240. { calculates maximal variant size }
  1241. maxsize:=max(maxsize,unionsymtable.datasize);
  1242. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1243. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1244. { the items of the next variant are overlayed }
  1245. unionsymtable.datasize:=startvarrecsize;
  1246. unionsymtable.fieldalignment:=startvarrecalign;
  1247. unionsymtable.padalignment:=startpadalign;
  1248. if (token<>_END) and (token<>_RKLAMMER) then
  1249. consume(_SEMICOLON)
  1250. else
  1251. break;
  1252. until (token=_END) or (token=_RKLAMMER);
  1253. symtablestack.pop(UnionSymtable);
  1254. { at last set the record size to that of the biggest variant }
  1255. unionsymtable.datasize:=maxsize;
  1256. unionsymtable.fieldalignment:=maxalignment;
  1257. uniontype.def:=uniondef;
  1258. uniontype.sym:=nil;
  1259. UnionSym:=tfieldvarsym.create('$case',vs_value,uniontype,[]);
  1260. unionsymtable.addalignmentpadding;
  1261. {$ifdef powerpc}
  1262. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1263. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos]) and
  1264. is_first_field and
  1265. (recst.usefieldalignment = -1) and
  1266. (maxpadalign > recst.padalignment) then
  1267. recst.padalignment:=maxpadalign;
  1268. {$endif powerpc}
  1269. { Align the offset where the union symtable is added }
  1270. if (recst.usefieldalignment=-1) then
  1271. usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.maxCrecordalign)
  1272. else
  1273. usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.recordalignmax);
  1274. offset:=align(recst.datasize,usedalign);
  1275. recst.datasize:=offset+unionsymtable.datasize;
  1276. if unionsymtable.recordalignment>recst.fieldalignment then
  1277. recst.fieldalignment:=unionsymtable.recordalignment;
  1278. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1279. unionsym.free;
  1280. uniondef.free;
  1281. end;
  1282. block_type:=old_block_type;
  1283. current_object_option:=old_current_object_option;
  1284. { free the list }
  1285. sc.free;
  1286. {$ifdef powerpc}
  1287. is_first_field := false;
  1288. {$endif powerpc}
  1289. end;
  1290. end.