pdecvar.pas 54 KB

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