pdecvar.pas 56 KB

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