pdecvar.pas 54 KB

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