pdecvar.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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. {$message warn unlocalized string}
  571. if not is_interface(p.proptype.def) then
  572. begin
  573. writeln('Implements property must have interface type');
  574. Message1(sym_e_illegal_field, pattern);
  575. end;
  576. if pattern <> p.proptype.def.mangledparaname() then
  577. begin
  578. writeln('Implements-property must implement interface of same type');
  579. Message1(sym_e_illegal_field, pattern);
  580. end;
  581. end;
  582. { remove temporary procvardefs }
  583. readprocdef.free;
  584. writeprocdef.free;
  585. result:=p;
  586. end;
  587. function maybe_parse_proc_directives(const tt:ttype):boolean;
  588. var
  589. newtype : ttypesym;
  590. begin
  591. result:=false;
  592. { Process procvar directives before = and ; }
  593. if (tt.def.deftype=procvardef) and
  594. (tt.def.typesym=nil) and
  595. check_proc_directive(true) then
  596. begin
  597. newtype:=ttypesym.create('unnamed',tt);
  598. parse_var_proc_directives(tsym(newtype));
  599. newtype.restype.def:=nil;
  600. tt.def.typesym:=nil;
  601. newtype.free;
  602. result:=true;
  603. end;
  604. end;
  605. const
  606. variantrecordlevel : longint = 0;
  607. procedure read_var_decls(options:Tvar_dec_options);
  608. procedure read_default_value(sc : TFPObjectList;tt : ttype;is_threadvar : boolean);
  609. var
  610. vs : tabstractnormalvarsym;
  611. tcsym : ttypedconstsym;
  612. begin
  613. vs:=tabstractnormalvarsym(sc[0]);
  614. if sc.count>1 then
  615. Message(parser_e_initialized_only_one_var);
  616. if is_threadvar then
  617. Message(parser_e_initialized_not_for_threadvar);
  618. if symtablestack.top.symtabletype=localsymtable then
  619. begin
  620. consume(_EQUAL);
  621. tcsym:=ttypedconstsym.createtype('$default'+vs.realname,tt,false);
  622. include(tcsym.symoptions,sp_internal);
  623. vs.defaultconstsym:=tcsym;
  624. symtablestack.top.insert(tcsym);
  625. readtypedconst(current_asmdata.asmlists[al_typedconsts],tt,tcsym,false);
  626. { The variable has a value assigned }
  627. vs.varstate:=vs_initialised;
  628. end
  629. else
  630. begin
  631. tcsym:=ttypedconstsym.createtype(vs.realname,tt,true);
  632. tcsym.fileinfo:=vs.fileinfo;
  633. symtablestack.top.replace(vs,tcsym);
  634. vs.free;
  635. consume(_EQUAL);
  636. readtypedconst(current_asmdata.asmlists[al_typedconsts],tt,tcsym,true);
  637. end;
  638. end;
  639. var
  640. sc : TFPObjectList;
  641. i : longint;
  642. old_block_type : tblock_type;
  643. symdone : boolean;
  644. { to handle absolute }
  645. abssym : tabsolutevarsym;
  646. { c var }
  647. is_dll,
  648. hasdefaultvalue,
  649. is_gpc_name,is_cdecl,
  650. extern_var,export_var : boolean;
  651. old_current_object_option : tsymoptions;
  652. hs,sorg,C_name,dll_name : string;
  653. tt : ttype;
  654. hp,pt : tnode;
  655. vs : tabstractvarsym;
  656. hintsymoptions : tsymoptions;
  657. semicolonatend,semicoloneaten: boolean;
  658. begin
  659. old_current_object_option:=current_object_option;
  660. { all variables are public if not in a object declaration }
  661. current_object_option:=[sp_public];
  662. old_block_type:=block_type;
  663. block_type:=bt_type;
  664. is_gpc_name:=false;
  665. { Force an expected ID error message }
  666. if not (token in [_ID,_CASE,_END]) then
  667. consume(_ID);
  668. { read vars }
  669. sc:=TFPObjectList.create(false);
  670. while (token=_ID) do
  671. begin
  672. sorg:=orgpattern;
  673. semicoloneaten:=false;
  674. hasdefaultvalue:=false;
  675. symdone:=false;
  676. sc.clear;
  677. repeat
  678. if (token = _ID) then
  679. begin
  680. case symtablestack.top.symtabletype of
  681. localsymtable :
  682. vs:=tlocalvarsym.create(orgpattern,vs_value,generrortype,[]);
  683. staticsymtable,
  684. globalsymtable :
  685. vs:=tglobalvarsym.create(orgpattern,vs_value,generrortype,[]);
  686. else
  687. internalerror(200411064);
  688. end;
  689. sc.add(vs);
  690. symtablestack.top.insert(vs);
  691. end;
  692. consume(_ID);
  693. until not try_to_consume(_COMMA);
  694. consume(_COLON);
  695. if (m_gpc in aktmodeswitches) and
  696. (token=_ID) and
  697. (orgpattern='__asmname__') then
  698. begin
  699. consume(_ID);
  700. C_name:=get_stringconst;
  701. Is_gpc_name:=true;
  702. end;
  703. { this is needed for Delphi mode at least
  704. but should be OK for all modes !! (PM) }
  705. ignore_equal:=true;
  706. read_anon_type(tt,false);
  707. ignore_equal:=false;
  708. { Process procvar directives }
  709. if maybe_parse_proc_directives(tt) then
  710. semicoloneaten:=true;
  711. if is_gpc_name then
  712. begin
  713. vs:=tabstractvarsym(sc[0]);
  714. if sc.count>1 then
  715. Message(parser_e_absolute_only_one_var);
  716. vs.vartype:=tt;
  717. if vs.typ=globalvarsym then
  718. begin
  719. tglobalvarsym(vs).set_mangledname(target_info.Cprefix+sorg);
  720. include(vs.varoptions,vo_is_C_var);
  721. include(vs.varoptions,vo_is_external);
  722. end
  723. else
  724. Message(parser_e_no_local_var_external);
  725. symdone:=true;
  726. end;
  727. { check for absolute }
  728. if not symdone and
  729. try_to_consume(_ABSOLUTE) then
  730. begin
  731. abssym:=nil;
  732. { only allowed for one var }
  733. vs:=tabstractvarsym(sc[0]);
  734. if sc.count>1 then
  735. Message(parser_e_absolute_only_one_var);
  736. { parse the rest }
  737. pt:=expr;
  738. { check allowed absolute types }
  739. if (pt.nodetype=stringconstn) or
  740. (is_constcharnode(pt)) then
  741. begin
  742. abssym:=tabsolutevarsym.create(vs.realname,tt);
  743. abssym.fileinfo:=vs.fileinfo;
  744. if pt.nodetype=stringconstn then
  745. hs:=strpas(tstringconstnode(pt).value_str)
  746. else
  747. hs:=chr(tordconstnode(pt).value);
  748. consume(token);
  749. abssym.abstyp:=toasm;
  750. abssym.asmname:=stringdup(hs);
  751. { replace the varsym }
  752. symtablestack.top.replace(vs,abssym);
  753. vs.free;
  754. end
  755. { address }
  756. else if is_constintnode(pt) and
  757. ((target_info.system in [system_i386_go32v2,system_i386_watcom,
  758. system_i386_wdosx,system_i386_win32,
  759. system_arm_wince,system_i386_wince,
  760. system_arm_gba]) or
  761. (m_objfpc in aktmodeswitches) or
  762. (m_delphi in aktmodeswitches)) then
  763. begin
  764. abssym:=tabsolutevarsym.create(vs.realname,tt);
  765. abssym.fileinfo:=vs.fileinfo;
  766. abssym.abstyp:=toaddr;
  767. abssym.addroffset:=tordconstnode(pt).value;
  768. {$ifdef i386}
  769. abssym.absseg:=false;
  770. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  771. try_to_consume(_COLON) then
  772. begin
  773. pt.free;
  774. pt:=expr;
  775. if is_constintnode(pt) then
  776. begin
  777. abssym.addroffset:=abssym.addroffset shl 4+tordconstnode(pt).value;
  778. abssym.absseg:=true;
  779. end
  780. else
  781. Message(type_e_ordinal_expr_expected);
  782. end;
  783. {$endif i386}
  784. symtablestack.top.replace(vs,abssym);
  785. vs.free;
  786. end
  787. { variable }
  788. else
  789. begin
  790. { remove subscriptn before checking for loadn }
  791. hp:=pt;
  792. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  793. hp:=tunarynode(hp).left;
  794. if (hp.nodetype=loadn) then
  795. begin
  796. { we should check the result type of loadn }
  797. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,globalvarsym,localvarsym,
  798. paravarsym,typedconstsym]) then
  799. Message(parser_e_absolute_only_to_var_or_const);
  800. abssym:=tabsolutevarsym.create(vs.realname,tt);
  801. abssym.fileinfo:=vs.fileinfo;
  802. abssym.abstyp:=tovar;
  803. abssym.ref:=node_to_symlist(pt);
  804. symtablestack.top.replace(vs,abssym);
  805. vs.free;
  806. end
  807. else
  808. Message(parser_e_absolute_only_to_var_or_const);
  809. end;
  810. if assigned(abssym) then
  811. begin
  812. { try to consume the hint directives with absolute symbols }
  813. hintsymoptions:=[];
  814. try_consume_hintdirective(hintsymoptions);
  815. abssym.symoptions := abssym.symoptions + hintsymoptions;
  816. end;
  817. pt.free;
  818. symdone:=true;
  819. end;
  820. { try to parse the hint directives }
  821. hintsymoptions:=[];
  822. try_consume_hintdirective(hintsymoptions);
  823. { Handling of Delphi typed const = initialized vars }
  824. if (token=_EQUAL) and
  825. not(m_tp7 in aktmodeswitches) and
  826. (symtablestack.top.symtabletype<>parasymtable) then
  827. begin
  828. { Add calling convention for procvar }
  829. if (tt.def.deftype=procvardef) and
  830. (tt.def.typesym=nil) then
  831. handle_calling_convention(tprocvardef(tt.def));
  832. read_default_value(sc,tt,vd_threadvar in options);
  833. consume(_SEMICOLON);
  834. { for locals we've created typedconstsym with a different name }
  835. if symtablestack.top.symtabletype<>localsymtable then
  836. symdone:=true;
  837. hasdefaultvalue:=true;
  838. end
  839. else
  840. begin
  841. if not(semicoloneaten) then
  842. consume(_SEMICOLON);
  843. end;
  844. { Support calling convention for procvars after semicolon }
  845. if not(hasdefaultvalue) and
  846. (tt.def.deftype=procvardef) and
  847. (tt.def.typesym=nil) then
  848. begin
  849. { Parse procvar directives after ; }
  850. maybe_parse_proc_directives(tt);
  851. { Add calling convention for procvar }
  852. handle_calling_convention(tprocvardef(tt.def));
  853. { Handling of Delphi typed const = initialized vars }
  854. if (token=_EQUAL) and
  855. not(m_tp7 in aktmodeswitches) and
  856. (symtablestack.top.symtabletype<>parasymtable) then
  857. begin
  858. read_default_value(sc,tt,vd_threadvar in options);
  859. consume(_SEMICOLON);
  860. symdone:=true;
  861. hasdefaultvalue:=true;
  862. end;
  863. end;
  864. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  865. if not symdone then
  866. begin
  867. if (
  868. (token=_ID) and
  869. (m_cvar_support in aktmodeswitches) and
  870. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR])
  871. ) or
  872. (
  873. (m_mac in aktmodeswitches) and
  874. ((cs_external_var in aktlocalswitches) or (cs_externally_visible in aktlocalswitches))
  875. ) then
  876. begin
  877. { only allowed for one var }
  878. vs:=tabstractvarsym(sc[0]);
  879. if sc.count>1 then
  880. Message(parser_e_absolute_only_one_var);
  881. { set type of the var }
  882. vs.vartype:=tt;
  883. vs.symoptions := vs.symoptions + hintsymoptions;
  884. { defaults }
  885. is_dll:=false;
  886. is_cdecl:=false;
  887. extern_var:=false;
  888. export_var:=false;
  889. C_name:=sorg;
  890. semicolonatend:= false;
  891. { cdecl }
  892. if try_to_consume(_CVAR) then
  893. begin
  894. consume(_SEMICOLON);
  895. is_cdecl:=true;
  896. C_name:=target_info.Cprefix+sorg;
  897. end;
  898. { external }
  899. if try_to_consume(_EXTERNAL) then
  900. begin
  901. extern_var:=true;
  902. semicolonatend:= true;
  903. end;
  904. { macpas specific handling due to some switches}
  905. if (m_mac in aktmodeswitches) then
  906. begin
  907. if (cs_external_var in aktlocalswitches) then
  908. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  909. is_cdecl:=true;
  910. C_name:=target_info.Cprefix+sorg;
  911. extern_var:=true;
  912. end
  913. else if (cs_externally_visible in aktlocalswitches) then
  914. begin {The effect of this is the same as if cvar has been given as directives.}
  915. is_cdecl:=true;
  916. C_name:=target_info.Cprefix+sorg;
  917. end;
  918. vs.varregable := vr_none;
  919. end;
  920. { export }
  921. if idtoken in [_EXPORT,_PUBLIC] then
  922. begin
  923. consume(_ID);
  924. if extern_var then
  925. Message(parser_e_not_external_and_export)
  926. else
  927. begin
  928. export_var:=true;
  929. semicolonatend:= true;
  930. end;
  931. end;
  932. { external and export need a name after when no cdecl is used }
  933. if not is_cdecl then
  934. begin
  935. { dll name ? }
  936. if (extern_var) and (idtoken<>_NAME) then
  937. begin
  938. is_dll:=true;
  939. dll_name:=get_stringconst;
  940. end;
  941. if try_to_consume(_NAME) then
  942. C_name:=get_stringconst
  943. else
  944. C_name:=sorg;
  945. end;
  946. { consume the ; when export or external is used }
  947. if semicolonatend then
  948. consume(_SEMICOLON);
  949. { set some vars options }
  950. if is_dll then
  951. include(vs.varoptions,vo_is_dll_var)
  952. else
  953. include(vs.varoptions,vo_is_C_var);
  954. if (is_dll) and
  955. (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  956. C_Name := target_info.Cprefix+C_Name;
  957. if export_var then
  958. begin
  959. inc(vs.refs);
  960. include(vs.varoptions,vo_is_exported);
  961. end;
  962. if extern_var then
  963. include(vs.varoptions,vo_is_external);
  964. if vs.typ=globalvarsym then
  965. begin
  966. tglobalvarsym(vs).set_mangledname(C_Name);
  967. { insert in the al_globals when it is not external }
  968. if (not extern_var) then
  969. insertbssdata(tglobalvarsym(vs));
  970. { now we can insert it in the import lib if its a dll, or
  971. add it to the externals }
  972. if extern_var then
  973. begin
  974. vs.varregable := vr_none;
  975. if is_dll then
  976. begin
  977. if not(current_module.uses_imports) then
  978. begin
  979. current_module.uses_imports:=true;
  980. importlib.preparelib(current_module.realmodulename^);
  981. end;
  982. importlib.importvariable(tglobalvarsym(vs),C_name,dll_name);
  983. end
  984. else
  985. if tf_has_dllscanner in target_info.flags then
  986. current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
  987. end;
  988. end
  989. else
  990. Message(parser_e_no_local_var_external);
  991. symdone:=true;
  992. end;
  993. end;
  994. { insert it in the symtable, if not done yet }
  995. if not symdone then
  996. begin
  997. for i:=0 to sc.count-1 do
  998. begin
  999. vs:=tabstractvarsym(sc[i]);
  1000. vs.vartype:=tt;
  1001. { insert any additional hint directives }
  1002. vs.symoptions := vs.symoptions + hintsymoptions;
  1003. if vd_threadvar in options then
  1004. include(vs.varoptions,vo_is_thread_var);
  1005. { static data fields are inserted in the globalsymtable }
  1006. if vs.typ=globalvarsym then
  1007. insertbssdata(tglobalvarsym(vs));
  1008. end;
  1009. end;
  1010. end;
  1011. block_type:=old_block_type;
  1012. current_object_option:=old_current_object_option;
  1013. { free the list }
  1014. sc.free;
  1015. end;
  1016. procedure read_record_fields(options:Tvar_dec_options);
  1017. var
  1018. sc : TFPObjectList;
  1019. i : longint;
  1020. old_block_type : tblock_type;
  1021. old_current_object_option : tsymoptions;
  1022. hs,sorg : string;
  1023. tt,casetype : ttype;
  1024. { maxsize contains the max. size of a variant }
  1025. { startvarrec contains the start of the variant part of a record }
  1026. maxsize, startvarrecsize : longint;
  1027. usedalign,
  1028. maxalignment,startvarrecalign,
  1029. maxpadalign, startpadalign: shortint;
  1030. pt : tnode;
  1031. fieldvs : tfieldvarsym;
  1032. hstaticvs : tglobalvarsym;
  1033. vs : tabstractvarsym;
  1034. srsym : tsym;
  1035. srsymtable : tsymtable;
  1036. recst : tabstractrecordsymtable;
  1037. unionsymtable : trecordsymtable;
  1038. offset : longint;
  1039. uniondef : trecorddef;
  1040. unionsym : tfieldvarsym;
  1041. uniontype : ttype;
  1042. hintsymoptions : tsymoptions;
  1043. semicoloneaten: boolean;
  1044. {$ifdef powerpc}
  1045. tempdef: tdef;
  1046. is_first_field: boolean;
  1047. {$endif powerpc}
  1048. begin
  1049. recst:=tabstractrecordsymtable(symtablestack.top);
  1050. {$ifdef powerpc}
  1051. is_first_field := true;
  1052. {$endif powerpc}
  1053. old_current_object_option:=current_object_option;
  1054. { all variables are public if not in a object declaration }
  1055. if not(vd_object in options) then
  1056. current_object_option:=[sp_public];
  1057. old_block_type:=block_type;
  1058. block_type:=bt_type;
  1059. { Force an expected ID error message }
  1060. if not (token in [_ID,_CASE,_END]) then
  1061. consume(_ID);
  1062. { read vars }
  1063. sc:=TFPObjectList.create(false);
  1064. while (token=_ID) and
  1065. not((vd_object in options) and
  1066. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1067. begin
  1068. sorg:=orgpattern;
  1069. semicoloneaten:=false;
  1070. sc.clear;
  1071. repeat
  1072. if try_to_consume(_ID) then
  1073. begin
  1074. vs:=tfieldvarsym.create(orgpattern,vs_value,generrortype,[]);
  1075. sc.add(vs);
  1076. recst.insert(vs);
  1077. end;
  1078. until not try_to_consume(_COMMA);
  1079. consume(_COLON);
  1080. { Don't search in the recordsymtable for types }
  1081. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1082. symtablestack.pop(recst);
  1083. ignore_equal:=true;
  1084. read_anon_type(tt,false);
  1085. ignore_equal:=false;
  1086. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1087. symtablestack.push(recst);
  1088. { Process procvar directives }
  1089. if maybe_parse_proc_directives(tt) then
  1090. semicoloneaten:=true;
  1091. {$ifdef powerpc}
  1092. { from gcc/gcc/config/rs6000/rs6000.h:
  1093. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1094. /* Return the alignment of a struct based on the Macintosh PowerPC
  1095. alignment rules. In general the alignment of a struct is
  1096. determined by the greatest alignment of its elements. However, the
  1097. PowerPC rules cause the alignment of a struct to peg at word
  1098. alignment except when the first field has greater than word
  1099. (32-bit) alignment, in which case the alignment is determined by
  1100. the alignment of the first field. */
  1101. }
  1102. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos]) and
  1103. is_first_field and
  1104. (symtablestack.top.symtabletype = recordsymtable) and
  1105. (trecordsymtable(symtablestack.top).usefieldalignment = -1) then
  1106. begin
  1107. tempdef := tt.def;
  1108. while tempdef.deftype = arraydef do
  1109. tempdef := tarraydef(tempdef).elementtype.def;
  1110. if tempdef.deftype <> recorddef then
  1111. maxpadalign := tempdef.alignment
  1112. else
  1113. maxpadalign := trecorddef(tempdef).padalignment;
  1114. if (maxpadalign > 4) and
  1115. (maxpadalign > trecordsymtable(symtablestack.top).padalignment) then
  1116. trecordsymtable(symtablestack.top).padalignment := maxpadalign;
  1117. is_first_field := false;
  1118. end;
  1119. {$endif powerpc}
  1120. { types that use init/final are not allowed in variant parts, but
  1121. classes are allowed }
  1122. if (variantrecordlevel>0) and
  1123. (tt.def.needs_inittable and not is_class(tt.def)) then
  1124. Message(parser_e_cant_use_inittable_here);
  1125. { try to parse the hint directives }
  1126. hintsymoptions:=[];
  1127. try_consume_hintdirective(hintsymoptions);
  1128. { Records and objects can't have default values }
  1129. { for a record there doesn't need to be a ; before the END or ) }
  1130. if not(token in [_END,_RKLAMMER]) and
  1131. not(semicoloneaten) then
  1132. consume(_SEMICOLON);
  1133. { Parse procvar directives after ; }
  1134. maybe_parse_proc_directives(tt);
  1135. { Add calling convention for procvar }
  1136. if (tt.def.deftype=procvardef) and
  1137. (tt.def.typesym=nil) then
  1138. handle_calling_convention(tprocvardef(tt.def));
  1139. { Check for STATIC directive }
  1140. if (vd_object in options) and
  1141. (cs_static_keyword in aktmoduleswitches) and
  1142. (try_to_consume(_STATIC)) then
  1143. begin
  1144. include(current_object_option,sp_static);
  1145. consume(_SEMICOLON);
  1146. end;
  1147. if (sp_published in current_object_option) and
  1148. not(is_class(tt.def)) then
  1149. begin
  1150. Message(parser_e_cant_publish_that);
  1151. exclude(current_object_option,sp_published);
  1152. { recover by changing access type to public }
  1153. for i:=0 to sc.count-1 do
  1154. begin
  1155. fieldvs:=tfieldvarsym(sc[i]);
  1156. exclude(fieldvs.symoptions,sp_published);
  1157. include(fieldvs.symoptions,sp_public);
  1158. end;
  1159. end
  1160. else
  1161. if (sp_published in current_object_option) and
  1162. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  1163. begin
  1164. Message(parser_e_only_publishable_classes_can__be_published);
  1165. exclude(current_object_option,sp_published);
  1166. end;
  1167. { update variable options }
  1168. for i:=0 to sc.count-1 do
  1169. begin
  1170. fieldvs:=tfieldvarsym(sc[i]);
  1171. fieldvs.vartype:=tt;
  1172. { insert any additional hint directives }
  1173. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1174. if (sp_static in current_object_option) then
  1175. include(fieldvs.symoptions,sp_static);
  1176. { static data fields are inserted in the globalsymtable }
  1177. if (sp_static in current_object_option) then
  1178. begin
  1179. hstaticvs:=tglobalvarsym.create('$'+lower(symtablestack.top.name^)+'_'+fieldvs.name,vs_value,tt,[]);
  1180. recst.defowner.owner.insert(hstaticvs);
  1181. insertbssdata(hstaticvs);
  1182. end
  1183. else
  1184. recst.addfield(fieldvs);
  1185. end;
  1186. { restore current_object_option, it can be changed for
  1187. publishing or static }
  1188. current_object_option:=old_current_object_option;
  1189. end;
  1190. { Check for Case }
  1191. if (vd_record in options) and
  1192. try_to_consume(_CASE) then
  1193. begin
  1194. maxsize:=0;
  1195. maxalignment:=0;
  1196. maxpadalign:=0;
  1197. { including a field declaration? }
  1198. fieldvs:=nil;
  1199. sorg:=orgpattern;
  1200. hs:=pattern;
  1201. searchsym(hs,srsym,srsymtable);
  1202. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1203. begin
  1204. consume(_ID);
  1205. consume(_COLON);
  1206. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrortype,[]);
  1207. symtablestack.top.insert(fieldvs);
  1208. end;
  1209. read_anon_type(casetype,true);
  1210. if assigned(fieldvs) then
  1211. begin
  1212. fieldvs.vartype:=casetype;
  1213. recst.addfield(fieldvs);
  1214. end;
  1215. if not(is_ordinal(casetype.def))
  1216. {$ifndef cpu64bit}
  1217. or is_64bitint(casetype.def)
  1218. {$endif cpu64bit}
  1219. then
  1220. Message(type_e_ordinal_expr_expected);
  1221. consume(_OF);
  1222. UnionSymtable:=trecordsymtable.create(aktpackrecords);
  1223. UnionDef:=trecorddef.create(unionsymtable);
  1224. uniondef.isunion:=true;
  1225. startvarrecsize:=UnionSymtable.datasize;
  1226. startvarrecalign:=UnionSymtable.fieldalignment;
  1227. startpadalign:=Unionsymtable.padalignment;
  1228. symtablestack.push(UnionSymtable);
  1229. repeat
  1230. repeat
  1231. pt:=comp_expr(true);
  1232. if not(pt.nodetype=ordconstn) then
  1233. Message(parser_e_illegal_expression);
  1234. pt.free;
  1235. if token=_COMMA then
  1236. consume(_COMMA)
  1237. else
  1238. break;
  1239. until false;
  1240. consume(_COLON);
  1241. { read the vars }
  1242. consume(_LKLAMMER);
  1243. inc(variantrecordlevel);
  1244. if token<>_RKLAMMER then
  1245. read_record_fields([vd_record]);
  1246. dec(variantrecordlevel);
  1247. consume(_RKLAMMER);
  1248. { calculates maximal variant size }
  1249. maxsize:=max(maxsize,unionsymtable.datasize);
  1250. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1251. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1252. { the items of the next variant are overlayed }
  1253. unionsymtable.datasize:=startvarrecsize;
  1254. unionsymtable.fieldalignment:=startvarrecalign;
  1255. unionsymtable.padalignment:=startpadalign;
  1256. if (token<>_END) and (token<>_RKLAMMER) then
  1257. consume(_SEMICOLON)
  1258. else
  1259. break;
  1260. until (token=_END) or (token=_RKLAMMER);
  1261. symtablestack.pop(UnionSymtable);
  1262. { at last set the record size to that of the biggest variant }
  1263. unionsymtable.datasize:=maxsize;
  1264. unionsymtable.fieldalignment:=maxalignment;
  1265. uniontype.def:=uniondef;
  1266. uniontype.sym:=nil;
  1267. UnionSym:=tfieldvarsym.create('$case',vs_value,uniontype,[]);
  1268. unionsymtable.addalignmentpadding;
  1269. {$ifdef powerpc}
  1270. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1271. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos]) and
  1272. is_first_field and
  1273. (recst.usefieldalignment = -1) and
  1274. (maxpadalign > recst.padalignment) then
  1275. recst.padalignment:=maxpadalign;
  1276. {$endif powerpc}
  1277. { Align the offset where the union symtable is added }
  1278. if (recst.usefieldalignment=-1) then
  1279. usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.maxCrecordalign)
  1280. else
  1281. usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.recordalignmax);
  1282. offset:=align(recst.datasize,usedalign);
  1283. recst.datasize:=offset+unionsymtable.datasize;
  1284. if unionsymtable.recordalignment>recst.fieldalignment then
  1285. recst.fieldalignment:=unionsymtable.recordalignment;
  1286. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1287. unionsym.free;
  1288. uniondef.free;
  1289. end;
  1290. block_type:=old_block_type;
  1291. current_object_option:=old_current_object_option;
  1292. { free the list }
  1293. sc.free;
  1294. {$ifdef powerpc}
  1295. is_first_field := false;
  1296. {$endif powerpc}
  1297. end;
  1298. end.