2
0

pdecvar.pas 56 KB

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