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. { type/range checking }
  162. inserttypeconv(p,tarraydef(def).rangedef);
  163. idx:=tordconstnode(p).value
  164. end
  165. else
  166. Message(type_e_ordinal_expr_expected)
  167. end;
  168. p.free;
  169. pl.addconst(sl_vec,idx,p.resultdef);
  170. def:=tarraydef(def).elementdef;
  171. end
  172. else
  173. begin
  174. Message(parser_e_invalid_qualifier);
  175. result:=false;
  176. end;
  177. until not try_to_consume(_COMMA);
  178. consume(_RECKKLAMMER);
  179. end;
  180. else
  181. begin
  182. Message(parser_e_ill_property_access_sym);
  183. result:=false;
  184. break;
  185. end;
  186. end;
  187. until false;
  188. end
  189. else
  190. begin
  191. Message(parser_e_ill_property_access_sym);
  192. result:=false;
  193. end;
  194. end;
  195. var
  196. sym : tsym;
  197. p : tpropertysym;
  198. overriden : tsym;
  199. varspez : tvarspez;
  200. hdef : tdef;
  201. arraytype : tdef;
  202. def : tdef;
  203. pt : tnode;
  204. sc : TFPObjectList;
  205. paranr : word;
  206. i : longint;
  207. ImplIntf : TImplementedInterface;
  208. found : boolean;
  209. hreadparavs,
  210. hparavs : tparavarsym;
  211. storedprocdef,
  212. readprocdef,
  213. writeprocdef : tprocvardef;
  214. begin
  215. { Generate temp procvardefs to search for matching read/write
  216. procedures. the readprocdef will store all definitions }
  217. paranr:=0;
  218. readprocdef:=tprocvardef.create(normal_function_level);
  219. writeprocdef:=tprocvardef.create(normal_function_level);
  220. storedprocdef:=tprocvardef.create(normal_function_level);
  221. { make it method pointers }
  222. if assigned(aclass) then
  223. begin
  224. include(readprocdef.procoptions,po_methodpointer);
  225. include(writeprocdef.procoptions,po_methodpointer);
  226. include(storedprocdef.procoptions,po_methodpointer);
  227. end;
  228. { method for stored must return boolean }
  229. storedprocdef.returndef:=booltype;
  230. if token<>_ID then
  231. begin
  232. consume(_ID);
  233. consume(_SEMICOLON);
  234. exit;
  235. end;
  236. { Generate propertysym and insert in symtablestack }
  237. p:=tpropertysym.create(orgpattern);
  238. symtablestack.top.insert(p);
  239. consume(_ID);
  240. { property parameters ? }
  241. if try_to_consume(_LECKKLAMMER) then
  242. begin
  243. if (sp_published in current_object_option) and
  244. not (m_delphi in current_settings.modeswitches) then
  245. Message(parser_e_cant_publish_that_property);
  246. { create a list of the parameters }
  247. symtablestack.push(readprocdef.parast);
  248. sc:=TFPObjectList.create(false);
  249. inc(testcurobject);
  250. repeat
  251. if try_to_consume(_VAR) then
  252. varspez:=vs_var
  253. else if try_to_consume(_CONST) then
  254. varspez:=vs_const
  255. else if (m_out in current_settings.modeswitches) and try_to_consume(_OUT) then
  256. varspez:=vs_out
  257. else
  258. varspez:=vs_value;
  259. sc.clear;
  260. repeat
  261. inc(paranr);
  262. hreadparavs:=tparavarsym.create(orgpattern,10*paranr,varspez,generrordef,[]);
  263. readprocdef.parast.insert(hreadparavs);
  264. sc.add(hreadparavs);
  265. consume(_ID);
  266. until not try_to_consume(_COMMA);
  267. if try_to_consume(_COLON) then
  268. begin
  269. if try_to_consume(_ARRAY) then
  270. begin
  271. consume(_OF);
  272. { define range and type of range }
  273. hdef:=tarraydef.create(0,-1,s32inttype);
  274. { define field type }
  275. single_type(arraytype,false);
  276. tarraydef(hdef).elementdef:=arraytype;
  277. end
  278. else
  279. single_type(hdef,false);
  280. end
  281. else
  282. hdef:=cformaltype;
  283. for i:=0 to sc.count-1 do
  284. begin
  285. hreadparavs:=tparavarsym(sc[i]);
  286. hreadparavs.vardef:=hdef;
  287. { also update the writeprocdef }
  288. hparavs:=tparavarsym.create(hreadparavs.realname,hreadparavs.paranr,vs_value,hdef,[]);
  289. writeprocdef.parast.insert(hparavs);
  290. end;
  291. until not try_to_consume(_SEMICOLON);
  292. sc.free;
  293. dec(testcurobject);
  294. symtablestack.pop(readprocdef.parast);
  295. consume(_RECKKLAMMER);
  296. { the parser need to know if a property has parameters, the
  297. index parameter doesn't count (PFV) }
  298. if paranr>0 then
  299. include(p.propoptions,ppo_hasparameters);
  300. end;
  301. { overriden property ? }
  302. { force property interface
  303. there is a property parameter
  304. a global property }
  305. if (token=_COLON) or (paranr>0) or (aclass=nil) then
  306. begin
  307. consume(_COLON);
  308. single_type(p.propdef,false);
  309. if (idtoken=_INDEX) then
  310. begin
  311. consume(_INDEX);
  312. pt:=comp_expr(true);
  313. { Only allow enum and integer indexes. Convert all integer
  314. values to s32int to be compatible with delphi, because the
  315. procedure matching requires equal parameters }
  316. if is_constnode(pt) and
  317. is_ordinal(pt.resultdef)
  318. {$ifndef cpu64bit}
  319. and (not is_64bitint(pt.resultdef))
  320. {$endif cpu64bit}
  321. then
  322. begin
  323. if is_integer(pt.resultdef) then
  324. inserttypeconv_internal(pt,s32inttype);
  325. p.index:=tordconstnode(pt).value;
  326. end
  327. else
  328. begin
  329. Message(parser_e_invalid_property_index_value);
  330. p.index:=0;
  331. end;
  332. p.indexdef:=pt.resultdef;
  333. include(p.propoptions,ppo_indexed);
  334. { concat a longint to the para templates }
  335. inc(paranr);
  336. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  337. readprocdef.parast.insert(hparavs);
  338. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  339. writeprocdef.parast.insert(hparavs);
  340. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  341. storedprocdef.parast.insert(hparavs);
  342. pt.free;
  343. end;
  344. end
  345. else
  346. begin
  347. { do an property override }
  348. overriden:=search_class_member(aclass.childof,p.name);
  349. if assigned(overriden) and
  350. (overriden.typ=propertysym) and
  351. not(is_dispinterface(aclass)) then
  352. begin
  353. p.overridenpropsym:=tpropertysym(overriden);
  354. { inherit all type related entries }
  355. p.indexdef:=tpropertysym(overriden).indexdef;
  356. p.propdef:=tpropertysym(overriden).propdef;
  357. p.index:=tpropertysym(overriden).index;
  358. p.default:=tpropertysym(overriden).default;
  359. p.propoptions:=tpropertysym(overriden).propoptions;
  360. end
  361. else
  362. begin
  363. p.propdef:=generrordef;
  364. message(parser_e_no_property_found_to_override);
  365. end;
  366. end;
  367. if ((sp_published in current_object_option) or is_dispinterface(aclass)) and
  368. not(p.propdef.is_publishable) then
  369. Message(parser_e_cant_publish_that_property);
  370. if not(is_dispinterface(aclass)) then
  371. begin
  372. if try_to_consume(_READ) then
  373. begin
  374. p.propaccesslist[palt_read].clear;
  375. if parse_symlist(p.propaccesslist[palt_read],def) then
  376. begin
  377. sym:=p.propaccesslist[palt_read].firstsym^.sym;
  378. case sym.typ of
  379. procsym :
  380. begin
  381. { read is function returning the type of the property }
  382. readprocdef.returndef:=p.propdef;
  383. { Insert hidden parameters }
  384. handle_calling_convention(readprocdef);
  385. { search procdefs matching readprocdef }
  386. { we ignore hidden stuff here because the property access symbol might have
  387. non default calling conventions which might change the hidden stuff;
  388. see tw3216.pp (FK) }
  389. p.propaccesslist[palt_read].procdef:=Tprocsym(sym).Find_procdef_bypara(readprocdef.paras,p.propdef,[cpo_allowdefaults,cpo_ignorehidden]);
  390. if not assigned(p.propaccesslist[palt_read].procdef) then
  391. Message(parser_e_ill_property_access_sym);
  392. end;
  393. fieldvarsym :
  394. begin
  395. if not assigned(def) then
  396. internalerror(200310071);
  397. if compare_defs(def,p.propdef,nothingn)>=te_equal then
  398. begin
  399. { property parameters are allowed if this is
  400. an indexed property, because the index is then
  401. the parameter.
  402. Note: In the help of Kylix it is written
  403. that it isn't allowed, but the compiler accepts it (PFV) }
  404. if (ppo_hasparameters in p.propoptions) then
  405. Message(parser_e_ill_property_access_sym);
  406. end
  407. else
  408. IncompatibleTypes(def,p.propdef);
  409. end;
  410. else
  411. Message(parser_e_ill_property_access_sym);
  412. end;
  413. end;
  414. end;
  415. if try_to_consume(_WRITE) then
  416. begin
  417. p.propaccesslist[palt_write].clear;
  418. if parse_symlist(p.propaccesslist[palt_write],def) then
  419. begin
  420. sym:=p.propaccesslist[palt_write].firstsym^.sym;
  421. case sym.typ of
  422. procsym :
  423. begin
  424. { write is a procedure with an extra value parameter
  425. of the of the property }
  426. writeprocdef.returndef:=voidtype;
  427. inc(paranr);
  428. hparavs:=tparavarsym.create('$value',10*paranr,vs_value,p.propdef,[]);
  429. writeprocdef.parast.insert(hparavs);
  430. { Insert hidden parameters }
  431. handle_calling_convention(writeprocdef);
  432. { search procdefs matching writeprocdef }
  433. p.propaccesslist[palt_write].procdef:=Tprocsym(sym).Find_procdef_bypara(writeprocdef.paras,writeprocdef.returndef,[cpo_allowdefaults]);
  434. if not assigned(p.propaccesslist[palt_write].procdef) then
  435. Message(parser_e_ill_property_access_sym);
  436. end;
  437. fieldvarsym :
  438. begin
  439. if not assigned(def) then
  440. internalerror(200310072);
  441. if compare_defs(def,p.propdef,nothingn)>=te_equal then
  442. begin
  443. { property parameters are allowed if this is
  444. an indexed property, because the index is then
  445. the parameter.
  446. Note: In the help of Kylix it is written
  447. that it isn't allowed, but the compiler accepts it (PFV) }
  448. if (ppo_hasparameters in p.propoptions) then
  449. Message(parser_e_ill_property_access_sym);
  450. end
  451. else
  452. IncompatibleTypes(def,p.propdef);
  453. end;
  454. else
  455. Message(parser_e_ill_property_access_sym);
  456. end;
  457. end;
  458. end;
  459. end
  460. else
  461. begin
  462. if try_to_consume(_READONLY) then
  463. begin
  464. end
  465. else if try_to_consume(_WRITEONLY) then
  466. begin
  467. end;
  468. if try_to_consume(_DISPID) then
  469. begin
  470. pt:=comp_expr(true);
  471. if is_constintnode(pt) then
  472. // tprocdef(pd).extnumber:=tordconstnode(pt).value
  473. else
  474. Message(parser_e_dispid_must_be_ord_const);
  475. pt.free;
  476. end;
  477. end;
  478. if assigned(aclass) and not(is_dispinterface(aclass)) then
  479. begin
  480. { ppo_stored is default on for not overriden properties }
  481. if not assigned(p.overridenpropsym) then
  482. include(p.propoptions,ppo_stored);
  483. if try_to_consume(_STORED) then
  484. begin
  485. include(p.propoptions,ppo_stored);
  486. p.propaccesslist[palt_stored].clear;
  487. case token of
  488. _ID:
  489. begin
  490. { in the case that idtoken=_DEFAULT }
  491. { we have to do nothing except }
  492. { setting ppo_stored, it's the same }
  493. { as stored true }
  494. if idtoken<>_DEFAULT then
  495. begin
  496. if parse_symlist(p.propaccesslist[palt_stored],def) then
  497. begin
  498. sym:=p.propaccesslist[palt_stored].firstsym^.sym;
  499. case sym.typ of
  500. procsym :
  501. begin
  502. { Insert hidden parameters }
  503. handle_calling_convention(storedprocdef);
  504. p.propaccesslist[palt_stored].procdef:=Tprocsym(sym).Find_procdef_bypara(storedprocdef.paras,storedprocdef.returndef,[cpo_allowdefaults,cpo_ignorehidden]);
  505. if not assigned(p.propaccesslist[palt_stored].procdef) then
  506. message(parser_e_ill_property_storage_sym);
  507. end;
  508. fieldvarsym :
  509. begin
  510. if not assigned(def) then
  511. internalerror(200310073);
  512. if (ppo_hasparameters in p.propoptions) or
  513. not(is_boolean(def)) then
  514. Message(parser_e_stored_property_must_be_boolean);
  515. end;
  516. else
  517. Message(parser_e_ill_property_access_sym);
  518. end;
  519. end;
  520. end;
  521. end;
  522. _FALSE:
  523. begin
  524. consume(_FALSE);
  525. exclude(p.propoptions,ppo_stored);
  526. end;
  527. _TRUE:
  528. begin
  529. p.default:=longint($80000000);
  530. consume(_TRUE);
  531. end;
  532. end;
  533. end;
  534. end;
  535. if try_to_consume(_DEFAULT) then
  536. begin
  537. if not(is_ordinal(p.propdef) or
  538. {$ifndef cpu64bit}
  539. is_64bitint(p.propdef) or
  540. {$endif cpu64bit}
  541. is_class(p.propdef) or
  542. is_single(p.propdef) or
  543. (p.propdef.typ in [classrefdef,pointerdef]) or
  544. ((p.propdef.typ=setdef) and
  545. (tsetdef(p.propdef).settype=smallset))) or
  546. ((p.propdef.typ=arraydef) and
  547. (ppo_indexed in p.propoptions)) or
  548. (ppo_hasparameters in p.propoptions) then
  549. begin
  550. Message(parser_e_property_cant_have_a_default_value);
  551. { Error recovery }
  552. pt:=comp_expr(true);
  553. pt.free;
  554. end
  555. else
  556. begin
  557. { Get the result of the default, the firstpass is
  558. needed to support values like -1 }
  559. pt:=comp_expr(true);
  560. if (p.propdef.typ=setdef) and
  561. (pt.nodetype=arrayconstructorn) then
  562. begin
  563. arrayconstructor_to_set(pt);
  564. do_typecheckpass(pt);
  565. end;
  566. inserttypeconv(pt,p.propdef);
  567. if not(is_constnode(pt)) then
  568. Message(parser_e_property_default_value_must_const);
  569. { Set default value }
  570. case pt.nodetype of
  571. setconstn :
  572. p.default:=plongint(tsetconstnode(pt).value_set)^;
  573. ordconstn :
  574. p.default:=longint(tordconstnode(pt).value);
  575. niln :
  576. p.default:=0;
  577. realconstn:
  578. p.default:=longint(single(trealconstnode(pt).value_real));
  579. end;
  580. pt.free;
  581. end;
  582. end
  583. else if try_to_consume(_NODEFAULT) then
  584. begin
  585. p.default:=longint($80000000);
  586. end;
  587. { Parse possible "implements" keyword }
  588. if try_to_consume(_IMPLEMENTS) then
  589. begin
  590. consume(_ID);
  591. try
  592. { NOTE: This code will be fixed when the strings are added to the localized string table }
  593. if not is_interface(p.propdef) then
  594. begin
  595. Comment(V_Error, 'Implements property must have interface type');
  596. exit;
  597. end;
  598. if pattern <> p.propdef.mangledparaname() then
  599. begin
  600. Comment(V_Error, 'Implements-property must implement interface of correct type');
  601. exit;
  602. end;
  603. if not assigned(p.propaccesslist[palt_read].firstsym) then
  604. begin
  605. Comment(V_Error, 'Implements-property must have read specifier');
  606. exit;
  607. end;
  608. if assigned(p.propaccesslist[palt_write].firstsym) then
  609. begin
  610. Comment(V_Error, 'Implements-property must not have write-specifier');
  611. exit;
  612. end;
  613. if assigned(p.propaccesslist[palt_stored].firstsym) then
  614. begin
  615. Comment(V_Error, 'Implements-property must not have stored-specifier');
  616. exit;
  617. end;
  618. found:=false;
  619. for i:=0 to aclass.ImplementedInterfaces.Count-1 do
  620. begin
  621. ImplIntf:=TImplementedInterface(aclass.ImplementedInterfaces[i]);
  622. { FIXME: Is this check valid? }
  623. if ImplIntf.IntfDef.Objname^=pattern then
  624. begin
  625. found:=true;
  626. break;
  627. end;
  628. end;
  629. if found then
  630. begin
  631. ImplIntf.IType := etFieldValue;
  632. ImplIntf.FieldOffset := tfieldvarsym(p.propaccesslist[palt_read].firstsym^.sym).fieldoffset;
  633. end
  634. else
  635. Comment(V_Error, 'Implements-property used on unimplemented interface');
  636. finally
  637. end;
  638. end;
  639. { remove temporary procvardefs }
  640. readprocdef.owner.deletedef(readprocdef);
  641. writeprocdef.owner.deletedef(writeprocdef);
  642. result:=p;
  643. end;
  644. function maybe_parse_proc_directives(def:tdef):boolean;
  645. var
  646. newtype : ttypesym;
  647. begin
  648. result:=false;
  649. { Process procvar directives before = and ; }
  650. if (def.typ=procvardef) and
  651. (def.typesym=nil) and
  652. check_proc_directive(true) then
  653. begin
  654. newtype:=ttypesym.create('unnamed',def);
  655. parse_var_proc_directives(tsym(newtype));
  656. newtype.typedef:=nil;
  657. def.typesym:=nil;
  658. newtype.free;
  659. result:=true;
  660. end;
  661. end;
  662. const
  663. variantrecordlevel : longint = 0;
  664. procedure read_var_decls(options:Tvar_dec_options);
  665. procedure read_default_value(sc : TFPObjectList);
  666. var
  667. vs : tabstractnormalvarsym;
  668. tcsym : tstaticvarsym;
  669. begin
  670. vs:=tabstractnormalvarsym(sc[0]);
  671. if sc.count>1 then
  672. Message(parser_e_initialized_only_one_var);
  673. if vo_is_thread_var in vs.varoptions then
  674. Message(parser_e_initialized_not_for_threadvar);
  675. consume(_EQUAL);
  676. case vs.typ of
  677. localvarsym :
  678. begin
  679. tcsym:=tstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  680. include(tcsym.symoptions,sp_internal);
  681. vs.defaultconstsym:=tcsym;
  682. symtablestack.top.insert(tcsym);
  683. read_typed_const(current_asmdata.asmlists[al_typedconsts],tcsym);
  684. end;
  685. staticvarsym :
  686. begin
  687. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs));
  688. end;
  689. else
  690. internalerror(200611051);
  691. end;
  692. vs.varstate:=vs_initialised;
  693. end;
  694. {$ifdef gpc_mode}
  695. procedure read_gpc_name(sc : TFPObjectList);
  696. var
  697. vs : tabstractnormalvarsym;
  698. C_Name : string;
  699. begin
  700. consume(_ID);
  701. C_Name:=get_stringconst;
  702. vs:=tabstractnormalvarsym(sc[0]);
  703. if sc.count>1 then
  704. Message(parser_e_absolute_only_one_var);
  705. if vs.typ=staticvarsym then
  706. begin
  707. tstaticvarsym(vs).set_mangledname(C_Name);
  708. include(vs.varoptions,vo_is_external);
  709. end
  710. else
  711. Message(parser_e_no_local_var_external);
  712. end;
  713. {$endif}
  714. procedure read_absolute(sc : TFPObjectList);
  715. var
  716. vs : tabstractvarsym;
  717. abssym : tabsolutevarsym;
  718. pt,hp : tnode;
  719. st : tsymtable;
  720. begin
  721. abssym:=nil;
  722. { only allowed for one var }
  723. vs:=tabstractvarsym(sc[0]);
  724. if sc.count>1 then
  725. Message(parser_e_absolute_only_one_var);
  726. if vo_is_typed_const in vs.varoptions then
  727. Message(parser_e_initialized_not_for_external);
  728. { parse the rest }
  729. pt:=expr;
  730. { check allowed absolute types }
  731. if (pt.nodetype=stringconstn) or
  732. (is_constcharnode(pt)) then
  733. begin
  734. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  735. abssym.fileinfo:=vs.fileinfo;
  736. if pt.nodetype=stringconstn then
  737. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  738. else
  739. abssym.asmname:=stringdup(chr(tordconstnode(pt).value));
  740. consume(token);
  741. abssym.abstyp:=toasm;
  742. end
  743. { address }
  744. else if is_constintnode(pt) then
  745. begin
  746. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  747. abssym.fileinfo:=vs.fileinfo;
  748. abssym.abstyp:=toaddr;
  749. abssym.addroffset:=tordconstnode(pt).value;
  750. {$ifdef i386}
  751. abssym.absseg:=false;
  752. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  753. try_to_consume(_COLON) then
  754. begin
  755. pt.free;
  756. pt:=expr;
  757. if is_constintnode(pt) then
  758. begin
  759. abssym.addroffset:=abssym.addroffset shl 4+tordconstnode(pt).value;
  760. abssym.absseg:=true;
  761. end
  762. else
  763. Message(type_e_ordinal_expr_expected);
  764. end;
  765. {$endif i386}
  766. end
  767. { variable }
  768. else
  769. begin
  770. { remove subscriptn before checking for loadn }
  771. hp:=pt;
  772. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  773. hp:=tunarynode(hp).left;
  774. if (hp.nodetype=loadn) then
  775. begin
  776. { we should check the result type of loadn }
  777. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym]) then
  778. Message(parser_e_absolute_only_to_var_or_const);
  779. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  780. abssym.fileinfo:=vs.fileinfo;
  781. abssym.abstyp:=tovar;
  782. abssym.ref:=node_to_propaccesslist(pt);
  783. { if the sizes are different, can't be a regvar since you }
  784. { can't be "absolute upper 8 bits of a register" (except }
  785. { if its a record field of the same size of a record }
  786. { regvar, but in that case pt.resultdef.size will have }
  787. { the same size since it refers to the field and not to }
  788. { the whole record -- which is why we use pt and not hp) }
  789. { we can't take the size of an open array }
  790. if is_open_array(pt.resultdef) or
  791. (vs.vardef.size <> pt.resultdef.size) then
  792. make_not_regable(pt,vr_addr);
  793. end
  794. else
  795. Message(parser_e_absolute_only_to_var_or_const);
  796. end;
  797. pt.free;
  798. { replace old varsym with the new absolutevarsym }
  799. if assigned(abssym) then
  800. begin
  801. st:=vs.owner;
  802. vs.owner.Delete(vs);
  803. st.insert(abssym);
  804. sc[0]:=abssym;
  805. end;
  806. end;
  807. procedure read_public_and_external(sc:TFPObjectList);
  808. var
  809. vs : tabstractvarsym;
  810. is_dll,
  811. is_cdecl,
  812. is_external_var,
  813. is_public_var : boolean;
  814. dll_name,
  815. C_name : string;
  816. begin
  817. { only allowed for one var }
  818. vs:=tabstractvarsym(sc[0]);
  819. if sc.count>1 then
  820. Message(parser_e_absolute_only_one_var);
  821. { only allow external and public on global symbols }
  822. if vs.typ<>staticvarsym then
  823. begin
  824. Message(parser_e_no_local_var_external);
  825. exit;
  826. end;
  827. { defaults }
  828. is_dll:=false;
  829. is_cdecl:=false;
  830. is_external_var:=false;
  831. is_public_var:=false;
  832. C_name:=vs.realname;
  833. { macpas specific handling due to some switches}
  834. if (m_mac in current_settings.modeswitches) then
  835. begin
  836. if (cs_external_var in current_settings.localswitches) then
  837. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  838. is_cdecl:=true;
  839. is_external_var:=true;
  840. end
  841. else if (cs_externally_visible in current_settings.localswitches) then
  842. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  843. is_cdecl:=true;
  844. is_public_var:=true;
  845. end;
  846. end;
  847. { cdecl }
  848. if try_to_consume(_CVAR) then
  849. begin
  850. consume(_SEMICOLON);
  851. is_cdecl:=true;
  852. end;
  853. { external }
  854. if try_to_consume(_EXTERNAL) then
  855. begin
  856. is_external_var:=true;
  857. if not is_cdecl then
  858. begin
  859. if idtoken<>_NAME then
  860. begin
  861. is_dll:=true;
  862. dll_name:=get_stringconst;
  863. if ExtractFileExt(dll_name)='' then
  864. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  865. end;
  866. if try_to_consume(_NAME) then
  867. C_name:=get_stringconst;
  868. end;
  869. consume(_SEMICOLON);
  870. end;
  871. { export or public }
  872. if idtoken in [_EXPORT,_PUBLIC] then
  873. begin
  874. consume(_ID);
  875. if is_external_var then
  876. Message(parser_e_not_external_and_export)
  877. else
  878. is_public_var:=true;
  879. if try_to_consume(_NAME) then
  880. C_name:=get_stringconst;
  881. consume(_SEMICOLON);
  882. end;
  883. { Windows uses an indirect reference using import tables }
  884. if is_dll and
  885. (target_info.system in system_all_windows) then
  886. include(vs.varoptions,vo_is_dll_var);
  887. { Add C _ prefix }
  888. if is_cdecl or
  889. (
  890. is_dll and
  891. (target_info.system in [system_powerpc_darwin,system_i386_darwin])
  892. ) then
  893. C_Name := target_info.Cprefix+C_Name;
  894. if is_public_var then
  895. begin
  896. include(vs.varoptions,vo_is_public);
  897. vs.varregable := vr_none;
  898. { mark as referenced }
  899. inc(vs.refs);
  900. end;
  901. { now we can insert it in the import lib if its a dll, or
  902. add it to the externals }
  903. if is_external_var then
  904. begin
  905. if vo_is_typed_const in vs.varoptions then
  906. Message(parser_e_initialized_not_for_external);
  907. include(vs.varoptions,vo_is_external);
  908. vs.varregable := vr_none;
  909. if is_dll then
  910. current_module.AddExternalImport(dll_name,C_Name,0,true)
  911. else
  912. if tf_has_dllscanner in target_info.flags then
  913. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  914. end;
  915. { Set the assembler name }
  916. tstaticvarsym(vs).set_mangledname(C_Name);
  917. end;
  918. var
  919. sc : TFPObjectList;
  920. vs : tabstractvarsym;
  921. hdef : tdef;
  922. i : longint;
  923. semicoloneaten,
  924. allowdefaultvalue,
  925. hasdefaultvalue : boolean;
  926. old_current_object_option : tsymoptions;
  927. hintsymoptions : tsymoptions;
  928. old_block_type : tblock_type;
  929. begin
  930. old_current_object_option:=current_object_option;
  931. { all variables are public if not in a object declaration }
  932. current_object_option:=[sp_public];
  933. old_block_type:=block_type;
  934. block_type:=bt_type;
  935. { Force an expected ID error message }
  936. if not (token in [_ID,_CASE,_END]) then
  937. consume(_ID);
  938. { read vars }
  939. sc:=TFPObjectList.create(false);
  940. while (token=_ID) do
  941. begin
  942. semicoloneaten:=false;
  943. hasdefaultvalue:=false;
  944. allowdefaultvalue:=true;
  945. sc.clear;
  946. repeat
  947. if (token = _ID) then
  948. begin
  949. case symtablestack.top.symtabletype of
  950. localsymtable :
  951. vs:=tlocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  952. staticsymtable,
  953. globalsymtable :
  954. begin
  955. vs:=tstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  956. if vd_threadvar in options then
  957. include(vs.varoptions,vo_is_thread_var);
  958. end;
  959. else
  960. internalerror(200411064);
  961. end;
  962. sc.add(vs);
  963. symtablestack.top.insert(vs);
  964. end;
  965. consume(_ID);
  966. until not try_to_consume(_COMMA);
  967. consume(_COLON);
  968. {$ifdef gpc_mode}
  969. if (m_gpc in current_settings.modeswitches) and
  970. (token=_ID) and
  971. (orgpattern='__asmname__') then
  972. read_gpc_name(sc);
  973. {$endif}
  974. { read variable type def }
  975. read_anon_type(hdef,false);
  976. for i:=0 to sc.count-1 do
  977. begin
  978. vs:=tabstractvarsym(sc[i]);
  979. vs.vardef:=hdef;
  980. end;
  981. { Process procvar directives }
  982. if maybe_parse_proc_directives(hdef) then
  983. semicoloneaten:=true;
  984. { check for absolute }
  985. if try_to_consume(_ABSOLUTE) then
  986. begin
  987. read_absolute(sc);
  988. allowdefaultvalue:=false;
  989. end;
  990. { Check for EXTERNAL etc directives before a semicolon }
  991. if (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  992. begin
  993. read_public_and_external(sc);
  994. allowdefaultvalue:=false;
  995. semicoloneaten:=true;
  996. end;
  997. { try to parse the hint directives }
  998. hintsymoptions:=[];
  999. try_consume_hintdirective(hintsymoptions);
  1000. for i:=0 to sc.count-1 do
  1001. begin
  1002. vs:=tabstractvarsym(sc[i]);
  1003. vs.symoptions := vs.symoptions + hintsymoptions;
  1004. end;
  1005. { Handling of Delphi typed const = initialized vars }
  1006. if allowdefaultvalue and
  1007. (token=_EQUAL) and
  1008. not(m_tp7 in current_settings.modeswitches) and
  1009. (symtablestack.top.symtabletype<>parasymtable) then
  1010. begin
  1011. { Add calling convention for procvar }
  1012. if (hdef.typ=procvardef) and
  1013. (hdef.typesym=nil) then
  1014. handle_calling_convention(tprocvardef(hdef));
  1015. read_default_value(sc);
  1016. consume(_SEMICOLON);
  1017. hasdefaultvalue:=true;
  1018. end
  1019. else
  1020. begin
  1021. if not(semicoloneaten) then
  1022. consume(_SEMICOLON);
  1023. end;
  1024. { Support calling convention for procvars after semicolon }
  1025. if not(hasdefaultvalue) and
  1026. (hdef.typ=procvardef) and
  1027. (hdef.typesym=nil) then
  1028. begin
  1029. { Parse procvar directives after ; }
  1030. maybe_parse_proc_directives(hdef);
  1031. { Add calling convention for procvar }
  1032. handle_calling_convention(tprocvardef(hdef));
  1033. { Handling of Delphi typed const = initialized vars }
  1034. if (token=_EQUAL) and
  1035. not(m_tp7 in current_settings.modeswitches) and
  1036. (symtablestack.top.symtabletype<>parasymtable) then
  1037. begin
  1038. read_default_value(sc);
  1039. consume(_SEMICOLON);
  1040. hasdefaultvalue:=true;
  1041. end;
  1042. end;
  1043. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1044. if (
  1045. (
  1046. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) and
  1047. (m_cvar_support in current_settings.modeswitches)
  1048. ) or
  1049. (
  1050. (m_mac in current_settings.modeswitches) and
  1051. (
  1052. (cs_external_var in current_settings.localswitches) or
  1053. (cs_externally_visible in current_settings.localswitches)
  1054. )
  1055. )
  1056. ) then
  1057. read_public_and_external(sc);
  1058. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1059. for i:=0 to sc.count-1 do
  1060. begin
  1061. vs:=tabstractvarsym(sc[i]);
  1062. if (vs.typ=staticvarsym) and
  1063. not(vo_is_typed_const in vs.varoptions) and
  1064. not(vo_is_external in vs.varoptions) then
  1065. insertbssdata(tstaticvarsym(vs));
  1066. end;
  1067. end;
  1068. block_type:=old_block_type;
  1069. current_object_option:=old_current_object_option;
  1070. { free the list }
  1071. sc.free;
  1072. end;
  1073. procedure read_record_fields(options:Tvar_dec_options);
  1074. var
  1075. sc : TFPObjectList;
  1076. i : longint;
  1077. old_block_type : tblock_type;
  1078. old_current_object_option : tsymoptions;
  1079. hs,sorg : string;
  1080. hdef,casetype : tdef;
  1081. { maxsize contains the max. size of a variant }
  1082. { startvarrec contains the start of the variant part of a record }
  1083. maxsize, startvarrecsize : longint;
  1084. usedalign,
  1085. maxalignment,startvarrecalign,
  1086. maxpadalign, startpadalign: shortint;
  1087. pt : tnode;
  1088. fieldvs : tfieldvarsym;
  1089. hstaticvs : tstaticvarsym;
  1090. vs : tabstractvarsym;
  1091. srsym : tsym;
  1092. srsymtable : TSymtable;
  1093. recst : tabstractrecordsymtable;
  1094. unionsymtable : trecordsymtable;
  1095. offset : longint;
  1096. uniondef : trecorddef;
  1097. hintsymoptions : tsymoptions;
  1098. semicoloneaten: boolean;
  1099. {$ifdef powerpc}
  1100. tempdef: tdef;
  1101. is_first_field: boolean;
  1102. {$endif powerpc}
  1103. begin
  1104. recst:=tabstractrecordsymtable(symtablestack.top);
  1105. {$ifdef powerpc}
  1106. is_first_field := true;
  1107. {$endif powerpc}
  1108. old_current_object_option:=current_object_option;
  1109. { all variables are public if not in a object declaration }
  1110. if not(vd_object in options) then
  1111. current_object_option:=[sp_public];
  1112. old_block_type:=block_type;
  1113. block_type:=bt_type;
  1114. { Force an expected ID error message }
  1115. if not (token in [_ID,_CASE,_END]) then
  1116. consume(_ID);
  1117. { read vars }
  1118. sc:=TFPObjectList.create(false);
  1119. while (token=_ID) and
  1120. not((vd_object in options) and
  1121. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1122. begin
  1123. semicoloneaten:=false;
  1124. sc.clear;
  1125. repeat
  1126. sorg:=orgpattern;
  1127. if token=_ID then
  1128. begin
  1129. vs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1130. sc.add(vs);
  1131. recst.insert(vs);
  1132. end;
  1133. consume(_ID);
  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. {$ifdef powerpc}
  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]) 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}
  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. {$ifdef powerpc}
  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]) and
  1332. is_first_field and
  1333. (recst.usefieldalignment = -1) and
  1334. (maxpadalign > recst.padalignment) then
  1335. recst.padalignment:=maxpadalign;
  1336. {$endif powerpc}
  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.