pdecvar.pas 54 KB

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