pdecvar.pas 57 KB

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