pdecvar.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. sorg:=orgpattern;
  1125. semicoloneaten:=false;
  1126. sc.clear;
  1127. repeat
  1128. if try_to_consume(_ID) then
  1129. begin
  1130. vs:=tfieldvarsym.create(orgpattern,vs_value,generrordef,[]);
  1131. sc.add(vs);
  1132. recst.insert(vs);
  1133. end;
  1134. until not try_to_consume(_COMMA);
  1135. consume(_COLON);
  1136. { Don't search in the recordsymtable for types }
  1137. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1138. symtablestack.pop(recst);
  1139. read_anon_type(hdef,false);
  1140. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1141. symtablestack.push(recst);
  1142. { Process procvar directives }
  1143. if maybe_parse_proc_directives(hdef) then
  1144. semicoloneaten:=true;
  1145. {$if defined(powerpc) or defined(powerpc64)}
  1146. { from gcc/gcc/config/rs6000/rs6000.h:
  1147. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1148. /* Return the alignment of a struct based on the Macintosh PowerPC
  1149. alignment rules. In general the alignment of a struct is
  1150. determined by the greatest alignment of its elements. However, the
  1151. PowerPC rules cause the alignment of a struct to peg at word
  1152. alignment except when the first field has greater than word
  1153. (32-bit) alignment, in which case the alignment is determined by
  1154. the alignment of the first field. */
  1155. }
  1156. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1157. is_first_field and
  1158. (symtablestack.top.symtabletype = recordsymtable) and
  1159. (trecordsymtable(symtablestack.top).usefieldalignment = -1) then
  1160. begin
  1161. tempdef := hdef;
  1162. while tempdef.typ = arraydef do
  1163. tempdef := tarraydef(tempdef).elementdef;
  1164. if tempdef.typ <> recorddef then
  1165. maxpadalign := tempdef.alignment
  1166. else
  1167. maxpadalign := trecorddef(tempdef).padalignment;
  1168. if (maxpadalign > 4) and
  1169. (maxpadalign > trecordsymtable(symtablestack.top).padalignment) then
  1170. trecordsymtable(symtablestack.top).padalignment := maxpadalign;
  1171. is_first_field := false;
  1172. end;
  1173. {$endif powerpc or powerpc64}
  1174. { types that use init/final are not allowed in variant parts, but
  1175. classes are allowed }
  1176. if (variantrecordlevel>0) and
  1177. (hdef.needs_inittable and not is_class(hdef)) then
  1178. Message(parser_e_cant_use_inittable_here);
  1179. { try to parse the hint directives }
  1180. hintsymoptions:=[];
  1181. try_consume_hintdirective(hintsymoptions);
  1182. { update variable type and hints }
  1183. for i:=0 to sc.count-1 do
  1184. begin
  1185. fieldvs:=tfieldvarsym(sc[i]);
  1186. fieldvs.vardef:=hdef;
  1187. { insert any additional hint directives }
  1188. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1189. end;
  1190. { Records and objects can't have default values }
  1191. { for a record there doesn't need to be a ; before the END or ) }
  1192. if not(token in [_END,_RKLAMMER]) and
  1193. not(semicoloneaten) then
  1194. consume(_SEMICOLON);
  1195. { Parse procvar directives after ; }
  1196. maybe_parse_proc_directives(hdef);
  1197. { Add calling convention for procvar }
  1198. if (hdef.typ=procvardef) and
  1199. (hdef.typesym=nil) then
  1200. handle_calling_convention(tprocvardef(hdef));
  1201. { Check for STATIC directive }
  1202. if (vd_object in options) and
  1203. (cs_static_keyword in current_settings.moduleswitches) and
  1204. (try_to_consume(_STATIC)) then
  1205. begin
  1206. { add static flag and staticvarsyms }
  1207. for i:=0 to sc.count-1 do
  1208. begin
  1209. fieldvs:=tfieldvarsym(sc[i]);
  1210. include(fieldvs.symoptions,sp_static);
  1211. hstaticvs:=tstaticvarsym.create('$'+lower(symtablestack.top.name^)+'_'+fieldvs.name,vs_value,hdef,[]);
  1212. recst.defowner.owner.insert(hstaticvs);
  1213. insertbssdata(hstaticvs);
  1214. end;
  1215. consume(_SEMICOLON);
  1216. end;
  1217. if (sp_published in current_object_option) and
  1218. not(is_class(hdef)) then
  1219. begin
  1220. Message(parser_e_cant_publish_that);
  1221. exclude(current_object_option,sp_published);
  1222. { recover by changing access type to public }
  1223. for i:=0 to sc.count-1 do
  1224. begin
  1225. fieldvs:=tfieldvarsym(sc[i]);
  1226. exclude(fieldvs.symoptions,sp_published);
  1227. include(fieldvs.symoptions,sp_public);
  1228. end;
  1229. end
  1230. else
  1231. if (sp_published in current_object_option) and
  1232. not(oo_can_have_published in tobjectdef(hdef).objectoptions) then
  1233. begin
  1234. Message(parser_e_only_publishable_classes_can__be_published);
  1235. exclude(current_object_option,sp_published);
  1236. end;
  1237. { Generate field in the recordsymtable }
  1238. for i:=0 to sc.count-1 do
  1239. begin
  1240. fieldvs:=tfieldvarsym(sc[i]);
  1241. { static data fields are already inserted in the globalsymtable }
  1242. if not(sp_static in current_object_option) then
  1243. recst.addfield(fieldvs);
  1244. end;
  1245. { restore current_object_option, it can be changed for
  1246. publishing or static }
  1247. current_object_option:=old_current_object_option;
  1248. end;
  1249. { Check for Case }
  1250. if (vd_record in options) and
  1251. try_to_consume(_CASE) then
  1252. begin
  1253. maxsize:=0;
  1254. maxalignment:=0;
  1255. maxpadalign:=0;
  1256. { including a field declaration? }
  1257. fieldvs:=nil;
  1258. sorg:=orgpattern;
  1259. hs:=pattern;
  1260. searchsym(hs,srsym,srsymtable);
  1261. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1262. begin
  1263. consume(_ID);
  1264. consume(_COLON);
  1265. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1266. symtablestack.top.insert(fieldvs);
  1267. end;
  1268. read_anon_type(casetype,true);
  1269. if assigned(fieldvs) then
  1270. begin
  1271. fieldvs.vardef:=casetype;
  1272. recst.addfield(fieldvs);
  1273. end;
  1274. if not(is_ordinal(casetype))
  1275. {$ifndef cpu64bit}
  1276. or is_64bitint(casetype)
  1277. {$endif cpu64bit}
  1278. then
  1279. Message(type_e_ordinal_expr_expected);
  1280. consume(_OF);
  1281. UnionSymtable:=trecordsymtable.create(current_settings.packrecords);
  1282. UnionDef:=trecorddef.create(unionsymtable);
  1283. uniondef.isunion:=true;
  1284. startvarrecsize:=UnionSymtable.datasize;
  1285. { align the bitpacking to the next byte }
  1286. UnionSymtable.datasize:=startvarrecsize;
  1287. startvarrecalign:=UnionSymtable.fieldalignment;
  1288. startpadalign:=Unionsymtable.padalignment;
  1289. symtablestack.push(UnionSymtable);
  1290. repeat
  1291. repeat
  1292. pt:=comp_expr(true);
  1293. if not(pt.nodetype=ordconstn) then
  1294. Message(parser_e_illegal_expression);
  1295. if try_to_consume(_POINTPOINT) then
  1296. pt:=crangenode.create(pt,comp_expr(true));
  1297. pt.free;
  1298. if token=_COMMA then
  1299. consume(_COMMA)
  1300. else
  1301. break;
  1302. until false;
  1303. consume(_COLON);
  1304. { read the vars }
  1305. consume(_LKLAMMER);
  1306. inc(variantrecordlevel);
  1307. if token<>_RKLAMMER then
  1308. read_record_fields([vd_record]);
  1309. dec(variantrecordlevel);
  1310. consume(_RKLAMMER);
  1311. { calculates maximal variant size }
  1312. maxsize:=max(maxsize,unionsymtable.datasize);
  1313. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1314. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1315. { the items of the next variant are overlayed }
  1316. unionsymtable.datasize:=startvarrecsize;
  1317. unionsymtable.fieldalignment:=startvarrecalign;
  1318. unionsymtable.padalignment:=startpadalign;
  1319. if (token<>_END) and (token<>_RKLAMMER) then
  1320. consume(_SEMICOLON)
  1321. else
  1322. break;
  1323. until (token=_END) or (token=_RKLAMMER);
  1324. symtablestack.pop(UnionSymtable);
  1325. { at last set the record size to that of the biggest variant }
  1326. unionsymtable.datasize:=maxsize;
  1327. unionsymtable.fieldalignment:=maxalignment;
  1328. unionsymtable.addalignmentpadding;
  1329. {$if defined(powerpc) or defined(powerpc64)}
  1330. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1331. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1332. is_first_field and
  1333. (recst.usefieldalignment = -1) and
  1334. (maxpadalign > recst.padalignment) then
  1335. recst.padalignment:=maxpadalign;
  1336. {$endif powerpc or powerpc64}
  1337. { Align the offset where the union symtable is added }
  1338. if (recst.usefieldalignment=-1) then
  1339. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  1340. else
  1341. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1342. offset:=align(recst.datasize,usedalign);
  1343. recst.datasize:=offset+unionsymtable.datasize;
  1344. if unionsymtable.recordalignment>recst.fieldalignment then
  1345. recst.fieldalignment:=unionsymtable.recordalignment;
  1346. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1347. uniondef.owner.deletedef(uniondef);
  1348. end;
  1349. block_type:=old_block_type;
  1350. current_object_option:=old_current_object_option;
  1351. { free the list }
  1352. sc.free;
  1353. {$ifdef powerpc}
  1354. is_first_field := false;
  1355. {$endif powerpc}
  1356. end;
  1357. end.