pdecvar.pas 56 KB

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