pdecvar.pas 54 KB

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