pdecvar.pas 54 KB

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