pdecvar.pas 54 KB

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