pdecvar.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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.Find(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.typ=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).Find_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).Find_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).Find_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.typ in [classrefdef,pointerdef]) or
  543. ((p.propdef.typ=setdef) and
  544. (tsetdef(p.propdef).settype=smallset))) or
  545. ((p.propdef.typ=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.typ=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. try
  591. { NOTE: This code will be fixed when the strings are added to the localized string table }
  592. if not is_interface(p.propdef) then
  593. begin
  594. Comment(V_Error, 'Implements property must have interface type');
  595. exit;
  596. end;
  597. if pattern <> p.propdef.mangledparaname() then
  598. begin
  599. Comment(V_Error, 'Implements-property must implement interface of correct type');
  600. exit;
  601. end;
  602. if not assigned(p.propaccesslist[palt_read].firstsym) then
  603. begin
  604. Comment(V_Error, 'Implements-property must have read specifier');
  605. exit;
  606. end;
  607. if assigned(p.propaccesslist[palt_write].firstsym) then
  608. begin
  609. Comment(V_Error, 'Implements-property must not have write-specifier');
  610. exit;
  611. end;
  612. if assigned(p.propaccesslist[palt_stored].firstsym) then
  613. begin
  614. Comment(V_Error, 'Implements-property must not have stored-specifier');
  615. exit;
  616. end;
  617. found:=false;
  618. for i:=0 to aclass.ImplementedInterfaces.Count-1 do
  619. begin
  620. ImplIntf:=TImplementedInterface(aclass.ImplementedInterfaces[i]);
  621. if ImplIntf.IntfDef.Objname^=pattern then
  622. begin
  623. found:=true;
  624. break;
  625. end;
  626. end;
  627. if found then
  628. begin
  629. ImplIntf.itype := etFieldValue;
  630. ImplIntf.iioffset := tfieldvarsym(p.propaccesslist[palt_read].firstsym^.sym).fieldoffset;
  631. end
  632. else
  633. Comment(V_Error, 'Implements-property used on unimplemented interface');
  634. finally
  635. end;
  636. end;
  637. { remove temporary procvardefs }
  638. readprocdef.owner.deletedef(readprocdef);
  639. writeprocdef.owner.deletedef(writeprocdef);
  640. result:=p;
  641. end;
  642. function maybe_parse_proc_directives(def:tdef):boolean;
  643. var
  644. newtype : ttypesym;
  645. begin
  646. result:=false;
  647. { Process procvar directives before = and ; }
  648. if (def.typ=procvardef) and
  649. (def.typesym=nil) and
  650. check_proc_directive(true) then
  651. begin
  652. newtype:=ttypesym.create('unnamed',def);
  653. parse_var_proc_directives(tsym(newtype));
  654. newtype.typedef:=nil;
  655. def.typesym:=nil;
  656. newtype.free;
  657. result:=true;
  658. end;
  659. end;
  660. const
  661. variantrecordlevel : longint = 0;
  662. procedure read_var_decls(options:Tvar_dec_options);
  663. procedure read_default_value(sc : TFPObjectList);
  664. var
  665. vs : tabstractnormalvarsym;
  666. tcsym : tstaticvarsym;
  667. begin
  668. vs:=tabstractnormalvarsym(sc[0]);
  669. if sc.count>1 then
  670. Message(parser_e_initialized_only_one_var);
  671. if vo_is_thread_var in vs.varoptions then
  672. Message(parser_e_initialized_not_for_threadvar);
  673. consume(_EQUAL);
  674. case vs.typ of
  675. localvarsym :
  676. begin
  677. tcsym:=tstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  678. include(tcsym.symoptions,sp_internal);
  679. vs.defaultconstsym:=tcsym;
  680. symtablestack.top.insert(tcsym);
  681. read_typed_const(current_asmdata.asmlists[al_typedconsts],tcsym);
  682. end;
  683. staticvarsym :
  684. begin
  685. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs));
  686. end;
  687. else
  688. internalerror(200611051);
  689. end;
  690. vs.varstate:=vs_initialised;
  691. end;
  692. procedure read_gpc_name(sc : TFPObjectList);
  693. var
  694. vs : tabstractnormalvarsym;
  695. C_Name : string;
  696. begin
  697. consume(_ID);
  698. C_Name:=get_stringconst;
  699. vs:=tabstractnormalvarsym(sc[0]);
  700. if sc.count>1 then
  701. Message(parser_e_absolute_only_one_var);
  702. if vs.typ=staticvarsym then
  703. begin
  704. tstaticvarsym(vs).set_mangledname(C_Name);
  705. include(vs.varoptions,vo_is_external);
  706. end
  707. else
  708. Message(parser_e_no_local_var_external);
  709. end;
  710. procedure read_absolute(sc : TFPObjectList);
  711. var
  712. vs : tabstractvarsym;
  713. abssym : tabsolutevarsym;
  714. pt,hp : tnode;
  715. st : tsymtable;
  716. begin
  717. abssym:=nil;
  718. { only allowed for one var }
  719. vs:=tabstractvarsym(sc[0]);
  720. if sc.count>1 then
  721. Message(parser_e_absolute_only_one_var);
  722. { parse the rest }
  723. pt:=expr;
  724. { check allowed absolute types }
  725. if (pt.nodetype=stringconstn) or
  726. (is_constcharnode(pt)) then
  727. begin
  728. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  729. abssym.fileinfo:=vs.fileinfo;
  730. if pt.nodetype=stringconstn then
  731. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  732. else
  733. abssym.asmname:=stringdup(chr(tordconstnode(pt).value));
  734. consume(token);
  735. abssym.abstyp:=toasm;
  736. end
  737. { address }
  738. else if is_constintnode(pt) then
  739. begin
  740. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  741. abssym.fileinfo:=vs.fileinfo;
  742. abssym.abstyp:=toaddr;
  743. abssym.addroffset:=tordconstnode(pt).value;
  744. {$ifdef i386}
  745. abssym.absseg:=false;
  746. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  747. try_to_consume(_COLON) then
  748. begin
  749. pt.free;
  750. pt:=expr;
  751. if is_constintnode(pt) then
  752. begin
  753. abssym.addroffset:=abssym.addroffset shl 4+tordconstnode(pt).value;
  754. abssym.absseg:=true;
  755. end
  756. else
  757. Message(type_e_ordinal_expr_expected);
  758. end;
  759. {$endif i386}
  760. end
  761. { variable }
  762. else
  763. begin
  764. { remove subscriptn before checking for loadn }
  765. hp:=pt;
  766. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  767. hp:=tunarynode(hp).left;
  768. if (hp.nodetype=loadn) then
  769. begin
  770. { we should check the result type of loadn }
  771. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym]) then
  772. Message(parser_e_absolute_only_to_var_or_const);
  773. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  774. abssym.fileinfo:=vs.fileinfo;
  775. abssym.abstyp:=tovar;
  776. abssym.ref:=node_to_propaccesslist(pt);
  777. end
  778. else
  779. Message(parser_e_absolute_only_to_var_or_const);
  780. end;
  781. pt.free;
  782. { replace old varsym with the new absolutevarsym }
  783. if assigned(abssym) then
  784. begin
  785. st:=vs.owner;
  786. vs.owner.Delete(vs);
  787. st.insert(abssym);
  788. sc[0]:=abssym;
  789. end;
  790. end;
  791. procedure read_public_and_external(sc:TFPObjectList);
  792. var
  793. vs : tabstractvarsym;
  794. is_dll,
  795. is_cdecl,
  796. is_external_var,
  797. is_public_var : boolean;
  798. dll_name,
  799. C_name : string;
  800. begin
  801. { only allowed for one var }
  802. vs:=tabstractvarsym(sc[0]);
  803. if sc.count>1 then
  804. Message(parser_e_absolute_only_one_var);
  805. { only allow external and public on global symbols }
  806. if vs.typ<>staticvarsym then
  807. begin
  808. Message(parser_e_no_local_var_external);
  809. exit;
  810. end;
  811. { defaults }
  812. is_dll:=false;
  813. is_cdecl:=false;
  814. is_external_var:=false;
  815. is_public_var:=false;
  816. C_name:=vs.realname;
  817. { macpas specific handling due to some switches}
  818. if (m_mac in current_settings.modeswitches) then
  819. begin
  820. if (cs_external_var in current_settings.localswitches) then
  821. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  822. is_cdecl:=true;
  823. is_external_var:=true;
  824. end
  825. else if (cs_externally_visible in current_settings.localswitches) then
  826. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  827. is_cdecl:=true;
  828. is_public_var:=true;
  829. end;
  830. end;
  831. { cdecl }
  832. if try_to_consume(_CVAR) then
  833. begin
  834. consume(_SEMICOLON);
  835. is_cdecl:=true;
  836. end;
  837. { external }
  838. if try_to_consume(_EXTERNAL) then
  839. begin
  840. is_external_var:=true;
  841. if not is_cdecl then
  842. begin
  843. if idtoken<>_NAME then
  844. begin
  845. is_dll:=true;
  846. dll_name:=get_stringconst;
  847. if ExtractFileExt(dll_name)='' then
  848. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  849. end;
  850. if try_to_consume(_NAME) then
  851. C_name:=get_stringconst;
  852. end;
  853. consume(_SEMICOLON);
  854. end;
  855. { export or public }
  856. if idtoken in [_EXPORT,_PUBLIC] then
  857. begin
  858. consume(_ID);
  859. if is_external_var then
  860. Message(parser_e_not_external_and_export)
  861. else
  862. is_public_var:=true;
  863. if try_to_consume(_NAME) then
  864. C_name:=get_stringconst;
  865. consume(_SEMICOLON);
  866. end;
  867. { Windows uses an indirect reference using import tables }
  868. if is_dll and
  869. (target_info.system in system_all_windows) then
  870. include(vs.varoptions,vo_is_dll_var);
  871. { Add C _ prefix }
  872. if is_cdecl or
  873. (
  874. is_dll and
  875. (target_info.system in [system_powerpc_darwin,system_i386_darwin])
  876. ) then
  877. C_Name := target_info.Cprefix+C_Name;
  878. if is_public_var then
  879. begin
  880. include(vs.varoptions,vo_is_public);
  881. vs.varregable := vr_none;
  882. { mark as referenced }
  883. inc(vs.refs);
  884. end;
  885. { now we can insert it in the import lib if its a dll, or
  886. add it to the externals }
  887. if is_external_var then
  888. begin
  889. include(vs.varoptions,vo_is_external);
  890. vs.varregable := vr_none;
  891. if is_dll then
  892. current_module.AddExternalImport(dll_name,C_Name,0,true)
  893. else
  894. if tf_has_dllscanner in target_info.flags then
  895. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  896. end;
  897. { Set the assembler name }
  898. tstaticvarsym(vs).set_mangledname(C_Name);
  899. end;
  900. var
  901. sc : TFPObjectList;
  902. vs : tabstractvarsym;
  903. hdef : tdef;
  904. i : longint;
  905. semicoloneaten,
  906. hasdefaultvalue : boolean;
  907. old_current_object_option : tsymoptions;
  908. hintsymoptions : tsymoptions;
  909. old_block_type : tblock_type;
  910. begin
  911. old_current_object_option:=current_object_option;
  912. { all variables are public if not in a object declaration }
  913. current_object_option:=[sp_public];
  914. old_block_type:=block_type;
  915. block_type:=bt_type;
  916. { Force an expected ID error message }
  917. if not (token in [_ID,_CASE,_END]) then
  918. consume(_ID);
  919. { read vars }
  920. sc:=TFPObjectList.create(false);
  921. while (token=_ID) do
  922. begin
  923. semicoloneaten:=false;
  924. hasdefaultvalue:=false;
  925. sc.clear;
  926. repeat
  927. if (token = _ID) then
  928. begin
  929. case symtablestack.top.symtabletype of
  930. localsymtable :
  931. vs:=tlocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  932. staticsymtable,
  933. globalsymtable :
  934. begin
  935. vs:=tstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  936. if vd_threadvar in options then
  937. include(vs.varoptions,vo_is_thread_var);
  938. end;
  939. else
  940. internalerror(200411064);
  941. end;
  942. sc.add(vs);
  943. symtablestack.top.insert(vs);
  944. end;
  945. consume(_ID);
  946. until not try_to_consume(_COMMA);
  947. consume(_COLON);
  948. if (m_gpc in current_settings.modeswitches) and
  949. (token=_ID) and
  950. (orgpattern='__asmname__') then
  951. read_gpc_name(sc);
  952. { read variable type def }
  953. read_anon_type(hdef,false);
  954. for i:=0 to sc.count-1 do
  955. begin
  956. vs:=tabstractvarsym(sc[i]);
  957. vs.vardef:=hdef;
  958. end;
  959. { Process procvar directives }
  960. if maybe_parse_proc_directives(hdef) then
  961. semicoloneaten:=true;
  962. { check for absolute }
  963. if try_to_consume(_ABSOLUTE) then
  964. read_absolute(sc);
  965. { try to parse the hint directives }
  966. hintsymoptions:=[];
  967. try_consume_hintdirective(hintsymoptions);
  968. for i:=0 to sc.count-1 do
  969. begin
  970. vs:=tabstractvarsym(sc[i]);
  971. vs.symoptions := vs.symoptions + hintsymoptions;
  972. end;
  973. { Handling of Delphi typed const = initialized vars }
  974. if (token=_EQUAL) and
  975. not(m_tp7 in current_settings.modeswitches) and
  976. (symtablestack.top.symtabletype<>parasymtable) then
  977. begin
  978. { Add calling convention for procvar }
  979. if (hdef.typ=procvardef) and
  980. (hdef.typesym=nil) then
  981. handle_calling_convention(tprocvardef(hdef));
  982. read_default_value(sc);
  983. consume(_SEMICOLON);
  984. hasdefaultvalue:=true;
  985. end
  986. else
  987. begin
  988. if not(semicoloneaten) then
  989. consume(_SEMICOLON);
  990. end;
  991. { Support calling convention for procvars after semicolon }
  992. if not(hasdefaultvalue) and
  993. (hdef.typ=procvardef) and
  994. (hdef.typesym=nil) then
  995. begin
  996. { Parse procvar directives after ; }
  997. maybe_parse_proc_directives(hdef);
  998. { Add calling convention for procvar }
  999. handle_calling_convention(tprocvardef(hdef));
  1000. { Handling of Delphi typed const = initialized vars }
  1001. if (token=_EQUAL) and
  1002. not(m_tp7 in current_settings.modeswitches) and
  1003. (symtablestack.top.symtabletype<>parasymtable) then
  1004. begin
  1005. read_default_value(sc);
  1006. consume(_SEMICOLON);
  1007. hasdefaultvalue:=true;
  1008. end;
  1009. end;
  1010. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1011. if (not hasdefaultvalue) and
  1012. (
  1013. (
  1014. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) and
  1015. (m_cvar_support in current_settings.modeswitches)
  1016. ) or
  1017. (
  1018. (m_mac in current_settings.modeswitches) and
  1019. (
  1020. (cs_external_var in current_settings.localswitches) or
  1021. (cs_externally_visible in current_settings.localswitches)
  1022. )
  1023. )
  1024. ) then
  1025. read_public_and_external(sc);
  1026. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1027. for i:=0 to sc.count-1 do
  1028. begin
  1029. vs:=tabstractvarsym(sc[i]);
  1030. if (vs.typ=staticvarsym) and
  1031. not(vo_is_typed_const in vs.varoptions) and
  1032. not(vo_is_external in vs.varoptions) then
  1033. insertbssdata(tstaticvarsym(vs));
  1034. end;
  1035. end;
  1036. block_type:=old_block_type;
  1037. current_object_option:=old_current_object_option;
  1038. { free the list }
  1039. sc.free;
  1040. end;
  1041. procedure read_record_fields(options:Tvar_dec_options);
  1042. var
  1043. sc : TFPObjectList;
  1044. i : longint;
  1045. old_block_type : tblock_type;
  1046. old_current_object_option : tsymoptions;
  1047. hs,sorg : string;
  1048. hdef,casetype : tdef;
  1049. { maxsize contains the max. size of a variant }
  1050. { startvarrec contains the start of the variant part of a record }
  1051. maxsize, startvarrecsize : longint;
  1052. usedalign,
  1053. maxalignment,startvarrecalign,
  1054. maxpadalign, startpadalign: shortint;
  1055. pt : tnode;
  1056. fieldvs : tfieldvarsym;
  1057. hstaticvs : tstaticvarsym;
  1058. vs : tabstractvarsym;
  1059. srsym : tsym;
  1060. srsymtable : TSymtable;
  1061. recst : tabstractrecordsymtable;
  1062. unionsymtable : trecordsymtable;
  1063. offset : longint;
  1064. uniondef : trecorddef;
  1065. hintsymoptions : tsymoptions;
  1066. semicoloneaten: boolean;
  1067. {$ifdef powerpc}
  1068. tempdef: tdef;
  1069. is_first_field: boolean;
  1070. {$endif powerpc}
  1071. begin
  1072. recst:=tabstractrecordsymtable(symtablestack.top);
  1073. {$ifdef powerpc}
  1074. is_first_field := true;
  1075. {$endif powerpc}
  1076. old_current_object_option:=current_object_option;
  1077. { all variables are public if not in a object declaration }
  1078. if not(vd_object in options) then
  1079. current_object_option:=[sp_public];
  1080. old_block_type:=block_type;
  1081. block_type:=bt_type;
  1082. { Force an expected ID error message }
  1083. if not (token in [_ID,_CASE,_END]) then
  1084. consume(_ID);
  1085. { read vars }
  1086. sc:=TFPObjectList.create(false);
  1087. while (token=_ID) and
  1088. not((vd_object in options) and
  1089. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1090. begin
  1091. sorg:=orgpattern;
  1092. semicoloneaten:=false;
  1093. sc.clear;
  1094. repeat
  1095. if try_to_consume(_ID) then
  1096. begin
  1097. vs:=tfieldvarsym.create(orgpattern,vs_value,generrordef,[]);
  1098. sc.add(vs);
  1099. recst.insert(vs);
  1100. end;
  1101. until not try_to_consume(_COMMA);
  1102. consume(_COLON);
  1103. { Don't search in the recordsymtable for types }
  1104. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1105. symtablestack.pop(recst);
  1106. read_anon_type(hdef,false);
  1107. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1108. symtablestack.push(recst);
  1109. { Process procvar directives }
  1110. if maybe_parse_proc_directives(hdef) then
  1111. semicoloneaten:=true;
  1112. {$ifdef powerpc}
  1113. { from gcc/gcc/config/rs6000/rs6000.h:
  1114. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1115. /* Return the alignment of a struct based on the Macintosh PowerPC
  1116. alignment rules. In general the alignment of a struct is
  1117. determined by the greatest alignment of its elements. However, the
  1118. PowerPC rules cause the alignment of a struct to peg at word
  1119. alignment except when the first field has greater than word
  1120. (32-bit) alignment, in which case the alignment is determined by
  1121. the alignment of the first field. */
  1122. }
  1123. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos]) and
  1124. is_first_field and
  1125. (symtablestack.top.symtabletype = recordsymtable) and
  1126. (trecordsymtable(symtablestack.top).usefieldalignment = -1) then
  1127. begin
  1128. tempdef := hdef;
  1129. while tempdef.typ = arraydef do
  1130. tempdef := tarraydef(tempdef).elementdef;
  1131. if tempdef.typ <> recorddef then
  1132. maxpadalign := tempdef.alignment
  1133. else
  1134. maxpadalign := trecorddef(tempdef).padalignment;
  1135. if (maxpadalign > 4) and
  1136. (maxpadalign > trecordsymtable(symtablestack.top).padalignment) then
  1137. trecordsymtable(symtablestack.top).padalignment := maxpadalign;
  1138. is_first_field := false;
  1139. end;
  1140. {$endif powerpc}
  1141. { types that use init/final are not allowed in variant parts, but
  1142. classes are allowed }
  1143. if (variantrecordlevel>0) and
  1144. (hdef.needs_inittable and not is_class(hdef)) then
  1145. Message(parser_e_cant_use_inittable_here);
  1146. { try to parse the hint directives }
  1147. hintsymoptions:=[];
  1148. try_consume_hintdirective(hintsymoptions);
  1149. { update variable type and hints }
  1150. for i:=0 to sc.count-1 do
  1151. begin
  1152. fieldvs:=tfieldvarsym(sc[i]);
  1153. fieldvs.vardef:=hdef;
  1154. { insert any additional hint directives }
  1155. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1156. end;
  1157. { Records and objects can't have default values }
  1158. { for a record there doesn't need to be a ; before the END or ) }
  1159. if not(token in [_END,_RKLAMMER]) and
  1160. not(semicoloneaten) then
  1161. consume(_SEMICOLON);
  1162. { Parse procvar directives after ; }
  1163. maybe_parse_proc_directives(hdef);
  1164. { Add calling convention for procvar }
  1165. if (hdef.typ=procvardef) and
  1166. (hdef.typesym=nil) then
  1167. handle_calling_convention(tprocvardef(hdef));
  1168. { Check for STATIC directive }
  1169. if (vd_object in options) and
  1170. (cs_static_keyword in current_settings.moduleswitches) and
  1171. (try_to_consume(_STATIC)) then
  1172. begin
  1173. { add static flag and staticvarsyms }
  1174. for i:=0 to sc.count-1 do
  1175. begin
  1176. fieldvs:=tfieldvarsym(sc[i]);
  1177. include(fieldvs.symoptions,sp_static);
  1178. hstaticvs:=tstaticvarsym.create('$'+lower(symtablestack.top.name^)+'_'+fieldvs.name,vs_value,hdef,[]);
  1179. recst.defowner.owner.insert(hstaticvs);
  1180. insertbssdata(hstaticvs);
  1181. end;
  1182. consume(_SEMICOLON);
  1183. end;
  1184. if (sp_published in current_object_option) and
  1185. not(is_class(hdef)) then
  1186. begin
  1187. Message(parser_e_cant_publish_that);
  1188. exclude(current_object_option,sp_published);
  1189. { recover by changing access type to public }
  1190. for i:=0 to sc.count-1 do
  1191. begin
  1192. fieldvs:=tfieldvarsym(sc[i]);
  1193. exclude(fieldvs.symoptions,sp_published);
  1194. include(fieldvs.symoptions,sp_public);
  1195. end;
  1196. end
  1197. else
  1198. if (sp_published in current_object_option) and
  1199. not(oo_can_have_published in tobjectdef(hdef).objectoptions) then
  1200. begin
  1201. Message(parser_e_only_publishable_classes_can__be_published);
  1202. exclude(current_object_option,sp_published);
  1203. end;
  1204. { Generate field in the recordsymtable }
  1205. for i:=0 to sc.count-1 do
  1206. begin
  1207. fieldvs:=tfieldvarsym(sc[i]);
  1208. { static data fields are already inserted in the globalsymtable }
  1209. if not(sp_static in current_object_option) then
  1210. recst.addfield(fieldvs);
  1211. end;
  1212. { restore current_object_option, it can be changed for
  1213. publishing or static }
  1214. current_object_option:=old_current_object_option;
  1215. end;
  1216. { Check for Case }
  1217. if (vd_record in options) and
  1218. try_to_consume(_CASE) then
  1219. begin
  1220. maxsize:=0;
  1221. maxalignment:=0;
  1222. maxpadalign:=0;
  1223. { including a field declaration? }
  1224. fieldvs:=nil;
  1225. sorg:=orgpattern;
  1226. hs:=pattern;
  1227. searchsym(hs,srsym,srsymtable);
  1228. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1229. begin
  1230. consume(_ID);
  1231. consume(_COLON);
  1232. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1233. symtablestack.top.insert(fieldvs);
  1234. end;
  1235. read_anon_type(casetype,true);
  1236. if assigned(fieldvs) then
  1237. begin
  1238. fieldvs.vardef:=casetype;
  1239. recst.addfield(fieldvs);
  1240. end;
  1241. if not(is_ordinal(casetype))
  1242. {$ifndef cpu64bit}
  1243. or is_64bitint(casetype)
  1244. {$endif cpu64bit}
  1245. then
  1246. Message(type_e_ordinal_expr_expected);
  1247. consume(_OF);
  1248. UnionSymtable:=trecordsymtable.create(current_settings.packrecords);
  1249. UnionDef:=trecorddef.create(unionsymtable);
  1250. uniondef.isunion:=true;
  1251. startvarrecsize:=UnionSymtable.datasize;
  1252. { align the bitpacking to the next byte }
  1253. UnionSymtable.datasize:=startvarrecsize;
  1254. startvarrecalign:=UnionSymtable.fieldalignment;
  1255. startpadalign:=Unionsymtable.padalignment;
  1256. symtablestack.push(UnionSymtable);
  1257. repeat
  1258. repeat
  1259. pt:=comp_expr(true);
  1260. if not(pt.nodetype=ordconstn) then
  1261. Message(parser_e_illegal_expression);
  1262. if try_to_consume(_POINTPOINT) then
  1263. pt:=crangenode.create(pt,comp_expr(true));
  1264. pt.free;
  1265. if token=_COMMA then
  1266. consume(_COMMA)
  1267. else
  1268. break;
  1269. until false;
  1270. consume(_COLON);
  1271. { read the vars }
  1272. consume(_LKLAMMER);
  1273. inc(variantrecordlevel);
  1274. if token<>_RKLAMMER then
  1275. read_record_fields([vd_record]);
  1276. dec(variantrecordlevel);
  1277. consume(_RKLAMMER);
  1278. { calculates maximal variant size }
  1279. maxsize:=max(maxsize,unionsymtable.datasize);
  1280. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1281. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1282. { the items of the next variant are overlayed }
  1283. unionsymtable.datasize:=startvarrecsize;
  1284. unionsymtable.fieldalignment:=startvarrecalign;
  1285. unionsymtable.padalignment:=startpadalign;
  1286. if (token<>_END) and (token<>_RKLAMMER) then
  1287. consume(_SEMICOLON)
  1288. else
  1289. break;
  1290. until (token=_END) or (token=_RKLAMMER);
  1291. symtablestack.pop(UnionSymtable);
  1292. { at last set the record size to that of the biggest variant }
  1293. unionsymtable.datasize:=maxsize;
  1294. unionsymtable.fieldalignment:=maxalignment;
  1295. unionsymtable.addalignmentpadding;
  1296. {$ifdef powerpc}
  1297. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1298. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos]) and
  1299. is_first_field and
  1300. (recst.usefieldalignment = -1) and
  1301. (maxpadalign > recst.padalignment) then
  1302. recst.padalignment:=maxpadalign;
  1303. {$endif powerpc}
  1304. { Align the offset where the union symtable is added }
  1305. if (recst.usefieldalignment=-1) then
  1306. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  1307. else
  1308. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1309. offset:=align(recst.datasize,usedalign);
  1310. recst.datasize:=offset+unionsymtable.datasize;
  1311. if unionsymtable.recordalignment>recst.fieldalignment then
  1312. recst.fieldalignment:=unionsymtable.recordalignment;
  1313. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1314. uniondef.owner.deletedef(uniondef);
  1315. end;
  1316. block_type:=old_block_type;
  1317. current_object_option:=old_current_object_option;
  1318. { free the list }
  1319. sc.free;
  1320. {$ifdef powerpc}
  1321. is_first_field := false;
  1322. {$endif powerpc}
  1323. end;
  1324. end.