pdecvar.pas 57 KB

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