pdecvar.pas 54 KB

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