pdecvar.pas 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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,vd_class);
  25. tvar_dec_options=set of tvar_dec_option;
  26. function read_property_dec(is_classproperty:boolean; aclass:tobjectdef):tpropertysym;
  27. procedure read_var_decls(options:Tvar_dec_options);
  28. procedure read_record_fields(options:Tvar_dec_options);
  29. procedure read_public_and_external(vs: tabstractvarsym);
  30. implementation
  31. uses
  32. SysUtils,
  33. { common }
  34. cutils,cclasses,
  35. { global }
  36. globtype,globals,tokens,verbose,constexp,
  37. systems,
  38. { symtable }
  39. symconst,symbase,symtype,symtable,defutil,defcmp,
  40. fmodule,htypechk,
  41. { pass 1 }
  42. node,pass_1,aasmdata,
  43. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,nutils,
  44. { codegen }
  45. ncgutil,
  46. { parser }
  47. scanner,
  48. pbase,pexpr,ptype,ptconst,pdecsub,
  49. { link }
  50. import
  51. ;
  52. function read_property_dec(is_classproperty:boolean; aclass:tobjectdef):tpropertysym;
  53. { convert a node tree to symlist and return the last
  54. symbol }
  55. function parse_symlist(pl:tpropaccesslist;var def:tdef):boolean;
  56. var
  57. idx : longint;
  58. sym : tsym;
  59. srsymtable : TSymtable;
  60. st : TSymtable;
  61. p : tnode;
  62. begin
  63. result:=true;
  64. def:=nil;
  65. if token=_ID then
  66. begin
  67. if assigned(aclass) then
  68. sym:=search_class_member(aclass,pattern)
  69. else
  70. searchsym(pattern,sym,srsymtable);
  71. if assigned(sym) then
  72. begin
  73. if assigned(aclass) and
  74. not is_visible_for_object(sym,aclass) then
  75. Message(parser_e_cant_access_private_member);
  76. case sym.typ of
  77. fieldvarsym :
  78. begin
  79. if (symtablestack.top.currentvisibility<>vis_private) then
  80. addsymref(sym);
  81. pl.addsym(sl_load,sym);
  82. def:=tfieldvarsym(sym).vardef;
  83. end;
  84. procsym :
  85. begin
  86. if (symtablestack.top.currentvisibility<>vis_private) then
  87. addsymref(sym);
  88. pl.addsym(sl_call,sym);
  89. end;
  90. else
  91. begin
  92. Message1(parser_e_illegal_field_or_method,orgpattern);
  93. def:=generrordef;
  94. result:=false;
  95. end;
  96. end;
  97. end
  98. else
  99. begin
  100. Message1(parser_e_illegal_field_or_method,orgpattern);
  101. def:=generrordef;
  102. result:=false;
  103. end;
  104. consume(_ID);
  105. repeat
  106. case token of
  107. _ID,
  108. _SEMICOLON :
  109. begin
  110. break;
  111. end;
  112. _POINT :
  113. begin
  114. consume(_POINT);
  115. if assigned(def) then
  116. begin
  117. st:=def.GetSymtable(gs_record);
  118. if assigned(st) then
  119. begin
  120. sym:=tsym(st.Find(pattern));
  121. if not(assigned(sym)) and is_object(def) then
  122. sym:=search_class_member(tobjectdef(def),pattern);
  123. if assigned(sym) then
  124. begin
  125. pl.addsym(sl_subscript,sym);
  126. case sym.typ of
  127. fieldvarsym :
  128. def:=tfieldvarsym(sym).vardef;
  129. else
  130. begin
  131. Message1(sym_e_illegal_field,orgpattern);
  132. result:=false;
  133. end;
  134. end;
  135. end
  136. else
  137. begin
  138. Message1(sym_e_illegal_field,orgpattern);
  139. result:=false;
  140. end;
  141. end
  142. else
  143. begin
  144. Message(parser_e_invalid_qualifier);
  145. result:=false;
  146. end;
  147. end
  148. else
  149. begin
  150. Message(parser_e_invalid_qualifier);
  151. result:=false;
  152. end;
  153. consume(_ID);
  154. end;
  155. _LECKKLAMMER :
  156. begin
  157. consume(_LECKKLAMMER);
  158. repeat
  159. if def.typ=arraydef then
  160. begin
  161. idx:=0;
  162. p:=comp_expr(true);
  163. if (not codegenerror) then
  164. begin
  165. if (p.nodetype=ordconstn) then
  166. begin
  167. { type/range checking }
  168. inserttypeconv(p,tarraydef(def).rangedef);
  169. if (Tordconstnode(p).value<int64(low(longint))) or
  170. (Tordconstnode(p).value>int64(high(longint))) then
  171. message(parser_e_array_range_out_of_bounds)
  172. else
  173. idx:=Tordconstnode(p).value.svalue
  174. end
  175. else
  176. Message(type_e_ordinal_expr_expected)
  177. end;
  178. pl.addconst(sl_vec,idx,p.resultdef);
  179. p.free;
  180. def:=tarraydef(def).elementdef;
  181. end
  182. else
  183. begin
  184. Message(parser_e_invalid_qualifier);
  185. result:=false;
  186. end;
  187. until not try_to_consume(_COMMA);
  188. consume(_RECKKLAMMER);
  189. end;
  190. else
  191. begin
  192. Message(parser_e_ill_property_access_sym);
  193. result:=false;
  194. break;
  195. end;
  196. end;
  197. until false;
  198. end
  199. else
  200. begin
  201. Message(parser_e_ill_property_access_sym);
  202. result:=false;
  203. end;
  204. end;
  205. function allow_default_property(p : tpropertysym) : boolean;
  206. begin
  207. allow_default_property:=
  208. (is_ordinal(p.propdef) or
  209. {$ifndef cpu64bitaddr}
  210. is_64bitint(p.propdef) or
  211. {$endif cpu64bitaddr}
  212. is_class(p.propdef) or
  213. is_single(p.propdef) or
  214. (p.propdef.typ in [classrefdef,pointerdef]) or
  215. is_smallset(p.propdef)
  216. ) and not
  217. (
  218. (p.propdef.typ=arraydef) and
  219. (ppo_indexed in p.propoptions)
  220. ) and not
  221. (ppo_hasparameters in p.propoptions);
  222. end;
  223. procedure parse_dispinterface(p : tpropertysym);
  224. var
  225. {procsym: tprocsym;
  226. procdef: tprocdef;
  227. valuepara: tparavarsym;}
  228. hasread, haswrite: boolean;
  229. pt: tnode;
  230. begin
  231. p.propaccesslist[palt_read].clear;
  232. p.propaccesslist[palt_write].clear;
  233. hasread:=true;
  234. haswrite:=true;
  235. if try_to_consume(_READONLY) then
  236. haswrite:=false
  237. else if try_to_consume(_WRITEONLY) then
  238. hasread:=false;
  239. if hasread then
  240. include(p.propoptions, ppo_dispid_read);
  241. if haswrite then
  242. include(p.propoptions, ppo_dispid_write);
  243. if try_to_consume(_DISPID) then
  244. begin
  245. pt:=comp_expr(true);
  246. if is_constintnode(pt) then
  247. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  248. message(parser_e_range_check_error)
  249. else
  250. p.dispid:=Tordconstnode(pt).value.svalue
  251. else
  252. Message(parser_e_dispid_must_be_ord_const);
  253. pt.free;
  254. end
  255. else
  256. p.dispid:=aclass.get_next_dispid;
  257. end;
  258. procedure add_index_parameter(var paranr: word; p: tpropertysym; readprocdef, writeprocdef, storedprocdef: tprocvardef);
  259. var
  260. hparavs: tparavarsym;
  261. begin
  262. inc(paranr);
  263. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  264. readprocdef.parast.insert(hparavs);
  265. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  266. writeprocdef.parast.insert(hparavs);
  267. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  268. storedprocdef.parast.insert(hparavs);
  269. end;
  270. var
  271. sym : tsym;
  272. srsymtable: tsymtable;
  273. p : tpropertysym;
  274. overridden : tsym;
  275. varspez : tvarspez;
  276. hdef : tdef;
  277. arraytype : tdef;
  278. def : tdef;
  279. pt : tnode;
  280. sc : TFPObjectList;
  281. paranr : word;
  282. i : longint;
  283. ImplIntf : TImplementedInterface;
  284. found : boolean;
  285. hreadparavs,
  286. hparavs : tparavarsym;
  287. storedprocdef,
  288. readprocdef,
  289. writeprocdef : tprocvardef;
  290. begin
  291. { Generate temp procvardefs to search for matching read/write
  292. procedures. the readprocdef will store all definitions }
  293. paranr:=0;
  294. readprocdef:=tprocvardef.create(normal_function_level);
  295. writeprocdef:=tprocvardef.create(normal_function_level);
  296. storedprocdef:=tprocvardef.create(normal_function_level);
  297. { make them method pointers }
  298. if assigned(aclass) and not is_classproperty then
  299. begin
  300. include(readprocdef.procoptions,po_methodpointer);
  301. include(writeprocdef.procoptions,po_methodpointer);
  302. include(storedprocdef.procoptions,po_methodpointer);
  303. end;
  304. { method for stored must return boolean }
  305. storedprocdef.returndef:=booltype;
  306. if token<>_ID then
  307. begin
  308. consume(_ID);
  309. consume(_SEMICOLON);
  310. exit;
  311. end;
  312. { Generate propertysym and insert in symtablestack }
  313. p:=tpropertysym.create(orgpattern);
  314. p.visibility:=symtablestack.top.currentvisibility;
  315. p.default:=longint($80000000);
  316. if is_classproperty then
  317. include(p.symoptions, sp_static);
  318. symtablestack.top.insert(p);
  319. consume(_ID);
  320. { property parameters ? }
  321. if try_to_consume(_LECKKLAMMER) then
  322. begin
  323. if (p.visibility=vis_published) and
  324. not (m_delphi in current_settings.modeswitches) then
  325. Message(parser_e_cant_publish_that_property);
  326. { create a list of the parameters }
  327. symtablestack.push(readprocdef.parast);
  328. sc:=TFPObjectList.create(false);
  329. inc(testcurobject);
  330. repeat
  331. if try_to_consume(_VAR) then
  332. varspez:=vs_var
  333. else if try_to_consume(_CONST) then
  334. varspez:=vs_const
  335. else if try_to_consume(_CONSTREF) then
  336. varspez:=vs_constref
  337. else if (m_out in current_settings.modeswitches) and try_to_consume(_OUT) then
  338. varspez:=vs_out
  339. else
  340. varspez:=vs_value;
  341. sc.clear;
  342. repeat
  343. inc(paranr);
  344. hreadparavs:=tparavarsym.create(orgpattern,10*paranr,varspez,generrordef,[]);
  345. readprocdef.parast.insert(hreadparavs);
  346. sc.add(hreadparavs);
  347. consume(_ID);
  348. until not try_to_consume(_COMMA);
  349. if try_to_consume(_COLON) then
  350. begin
  351. if try_to_consume(_ARRAY) then
  352. begin
  353. consume(_OF);
  354. { define range and type of range }
  355. hdef:=tarraydef.create(0,-1,s32inttype);
  356. { define field type }
  357. single_type(arraytype,false,false);
  358. tarraydef(hdef).elementdef:=arraytype;
  359. end
  360. else
  361. single_type(hdef,false,false);
  362. end
  363. else
  364. hdef:=cformaltype;
  365. for i:=0 to sc.count-1 do
  366. begin
  367. hreadparavs:=tparavarsym(sc[i]);
  368. hreadparavs.vardef:=hdef;
  369. { also update the writeprocdef }
  370. hparavs:=tparavarsym.create(hreadparavs.realname,hreadparavs.paranr,vs_value,hdef,[]);
  371. writeprocdef.parast.insert(hparavs);
  372. end;
  373. until not try_to_consume(_SEMICOLON);
  374. sc.free;
  375. dec(testcurobject);
  376. symtablestack.pop(readprocdef.parast);
  377. consume(_RECKKLAMMER);
  378. { the parser need to know if a property has parameters, the
  379. index parameter doesn't count (PFV) }
  380. if paranr>0 then
  381. include(p.propoptions,ppo_hasparameters);
  382. end;
  383. { overridden property ? }
  384. { force property interface
  385. there is a property parameter
  386. a global property }
  387. if (token=_COLON) or (paranr>0) or (aclass=nil) then
  388. begin
  389. consume(_COLON);
  390. single_type(p.propdef,false,false);
  391. if (idtoken=_INDEX) then
  392. begin
  393. consume(_INDEX);
  394. pt:=comp_expr(true);
  395. { Only allow enum and integer indexes. Convert all integer
  396. values to s32int to be compatible with delphi, because the
  397. procedure matching requires equal parameters }
  398. if is_constnode(pt) and
  399. is_ordinal(pt.resultdef)
  400. {$ifndef cpu64bitaddr}
  401. and (not is_64bitint(pt.resultdef))
  402. {$endif cpu64bitaddr}
  403. then
  404. begin
  405. if is_integer(pt.resultdef) then
  406. inserttypeconv_internal(pt,s32inttype);
  407. p.index:=tordconstnode(pt).value.svalue;
  408. end
  409. else
  410. begin
  411. Message(parser_e_invalid_property_index_value);
  412. p.index:=0;
  413. end;
  414. p.indexdef:=pt.resultdef;
  415. include(p.propoptions,ppo_indexed);
  416. { concat a longint to the para templates }
  417. add_index_parameter(paranr,p,readprocdef,writeprocdef,storedprocdef);
  418. pt.free;
  419. end;
  420. end
  421. else
  422. begin
  423. { do an property override }
  424. overridden:=search_class_member(aclass.childof,p.name);
  425. if assigned(overridden) and
  426. (overridden.typ=propertysym) and
  427. not(is_dispinterface(aclass)) then
  428. begin
  429. p.overriddenpropsym:=tpropertysym(overridden);
  430. { inherit all type related entries }
  431. p.indexdef:=tpropertysym(overridden).indexdef;
  432. p.propdef:=tpropertysym(overridden).propdef;
  433. p.index:=tpropertysym(overridden).index;
  434. p.default:=tpropertysym(overridden).default;
  435. p.propoptions:=tpropertysym(overridden).propoptions;
  436. if ppo_indexed in p.propoptions then
  437. add_index_parameter(paranr,p,readprocdef,writeprocdef,storedprocdef);
  438. end
  439. else
  440. begin
  441. p.propdef:=generrordef;
  442. message(parser_e_no_property_found_to_override);
  443. end;
  444. end;
  445. if ((p.visibility=vis_published) or is_dispinterface(aclass)) and
  446. (not(p.propdef.is_publishable) or (sp_static in p.symoptions)) then
  447. begin
  448. Message(parser_e_cant_publish_that_property);
  449. p.visibility:=vis_public;
  450. end;
  451. if not(is_dispinterface(aclass)) then
  452. begin
  453. if try_to_consume(_READ) then
  454. begin
  455. p.propaccesslist[palt_read].clear;
  456. if parse_symlist(p.propaccesslist[palt_read],def) then
  457. begin
  458. sym:=p.propaccesslist[palt_read].firstsym^.sym;
  459. case sym.typ of
  460. procsym :
  461. begin
  462. { read is function returning the type of the property }
  463. readprocdef.returndef:=p.propdef;
  464. { Insert hidden parameters }
  465. handle_calling_convention(readprocdef);
  466. { search procdefs matching readprocdef }
  467. { we ignore hidden stuff here because the property access symbol might have
  468. non default calling conventions which might change the hidden stuff;
  469. see tw3216.pp (FK) }
  470. p.propaccesslist[palt_read].procdef:=Tprocsym(sym).Find_procdef_bypara(readprocdef.paras,p.propdef,[cpo_allowdefaults,cpo_ignorehidden]);
  471. if not assigned(p.propaccesslist[palt_read].procdef) or
  472. { because of cpo_ignorehidden we need to compare if it is a static class method and we have a class property }
  473. ((sp_static in p.symoptions) <> tprocdef(p.propaccesslist[palt_read].procdef).no_self_node) then
  474. Message(parser_e_ill_property_access_sym);
  475. end;
  476. fieldvarsym :
  477. begin
  478. if not assigned(def) then
  479. internalerror(200310071);
  480. if compare_defs(def,p.propdef,nothingn)>=te_equal then
  481. begin
  482. { property parameters are allowed if this is
  483. an indexed property, because the index is then
  484. the parameter.
  485. Note: In the help of Kylix it is written
  486. that it isn't allowed, but the compiler accepts it (PFV) }
  487. if (ppo_hasparameters in p.propoptions) or
  488. ((sp_static in p.symoptions) <> (sp_static in sym.symoptions)) then
  489. Message(parser_e_ill_property_access_sym);
  490. end
  491. else
  492. IncompatibleTypes(def,p.propdef);
  493. end;
  494. else
  495. Message(parser_e_ill_property_access_sym);
  496. end;
  497. end;
  498. end;
  499. if try_to_consume(_WRITE) then
  500. begin
  501. p.propaccesslist[palt_write].clear;
  502. if parse_symlist(p.propaccesslist[palt_write],def) then
  503. begin
  504. sym:=p.propaccesslist[palt_write].firstsym^.sym;
  505. case sym.typ of
  506. procsym :
  507. begin
  508. { write is a procedure with an extra value parameter
  509. of the of the property }
  510. writeprocdef.returndef:=voidtype;
  511. inc(paranr);
  512. hparavs:=tparavarsym.create('$value',10*paranr,vs_value,p.propdef,[]);
  513. writeprocdef.parast.insert(hparavs);
  514. { Insert hidden parameters }
  515. handle_calling_convention(writeprocdef);
  516. { search procdefs matching writeprocdef }
  517. if cs_varpropsetter in current_settings.localswitches then
  518. p.propaccesslist[palt_write].procdef:=Tprocsym(sym).Find_procdef_bypara(writeprocdef.paras,writeprocdef.returndef,[cpo_allowdefaults,cpo_ignorevarspez])
  519. else
  520. p.propaccesslist[palt_write].procdef:=Tprocsym(sym).Find_procdef_bypara(writeprocdef.paras,writeprocdef.returndef,[cpo_allowdefaults]);
  521. if not assigned(p.propaccesslist[palt_write].procdef) then
  522. Message(parser_e_ill_property_access_sym);
  523. end;
  524. fieldvarsym :
  525. begin
  526. if not assigned(def) then
  527. internalerror(200310072);
  528. if compare_defs(def,p.propdef,nothingn)>=te_equal then
  529. begin
  530. { property parameters are allowed if this is
  531. an indexed property, because the index is then
  532. the parameter.
  533. Note: In the help of Kylix it is written
  534. that it isn't allowed, but the compiler accepts it (PFV) }
  535. if (ppo_hasparameters in p.propoptions) or
  536. ((sp_static in p.symoptions) <> (sp_static in sym.symoptions)) then
  537. Message(parser_e_ill_property_access_sym);
  538. end
  539. else
  540. IncompatibleTypes(def,p.propdef);
  541. end;
  542. else
  543. Message(parser_e_ill_property_access_sym);
  544. end;
  545. end;
  546. end;
  547. end
  548. else
  549. parse_dispinterface(p);
  550. if assigned(aclass) and not(is_dispinterface(aclass)) and not is_classproperty then
  551. begin
  552. { ppo_stored is default on for not overridden properties }
  553. if not assigned(p.overriddenpropsym) then
  554. include(p.propoptions,ppo_stored);
  555. if try_to_consume(_STORED) then
  556. begin
  557. include(p.propoptions,ppo_stored);
  558. p.propaccesslist[palt_stored].clear;
  559. case token of
  560. _ID:
  561. begin
  562. { in the case that idtoken=_DEFAULT }
  563. { we have to do nothing except }
  564. { setting ppo_stored, it's the same }
  565. { as stored true }
  566. if idtoken<>_DEFAULT then
  567. begin
  568. { parse_symlist cannot deal with constsyms, and
  569. we also don't want to put constsyms in symlists
  570. since they have to be evaluated immediately rather
  571. than each time the property is accessed
  572. The proper fix would be to always create a parse tree
  573. and then convert that one, if appropriate, to a symlist.
  574. Currently, we e.g. don't support any constant expressions
  575. yet either here, while Delphi does.
  576. }
  577. { make sure we don't let constants mask class fields/
  578. methods
  579. }
  580. if (not assigned(aclass) or
  581. (search_class_member(aclass,pattern)=nil)) and
  582. searchsym(pattern,sym,srsymtable) and
  583. (sym.typ = constsym) then
  584. begin
  585. addsymref(sym);
  586. if not is_boolean(tconstsym(sym).constdef) then
  587. Message(parser_e_stored_property_must_be_boolean)
  588. else if (tconstsym(sym).value.valueord=0) then
  589. { same as for _FALSE }
  590. exclude(p.propoptions,ppo_stored)
  591. else
  592. { same as for _TRUE }
  593. p.default:=longint($80000000);
  594. consume(_ID);
  595. end
  596. else if parse_symlist(p.propaccesslist[palt_stored],def) then
  597. begin
  598. sym:=p.propaccesslist[palt_stored].firstsym^.sym;
  599. case sym.typ of
  600. procsym :
  601. begin
  602. { Insert hidden parameters }
  603. handle_calling_convention(storedprocdef);
  604. p.propaccesslist[palt_stored].procdef:=Tprocsym(sym).Find_procdef_bypara(storedprocdef.paras,storedprocdef.returndef,[cpo_allowdefaults,cpo_ignorehidden]);
  605. if not assigned(p.propaccesslist[palt_stored].procdef) then
  606. message(parser_e_ill_property_storage_sym);
  607. end;
  608. fieldvarsym :
  609. begin
  610. if not assigned(def) then
  611. internalerror(200310073);
  612. if (ppo_hasparameters in p.propoptions) or
  613. not(is_boolean(def)) then
  614. Message(parser_e_stored_property_must_be_boolean);
  615. end;
  616. else
  617. Message(parser_e_ill_property_access_sym);
  618. end;
  619. end;
  620. end;
  621. end;
  622. _FALSE:
  623. begin
  624. consume(_FALSE);
  625. exclude(p.propoptions,ppo_stored);
  626. end;
  627. _TRUE:
  628. begin
  629. p.default:=longint($80000000);
  630. consume(_TRUE);
  631. end;
  632. end;
  633. end;
  634. end;
  635. if try_to_consume(_DEFAULT) then
  636. begin
  637. if not allow_default_property(p) then
  638. begin
  639. Message(parser_e_property_cant_have_a_default_value);
  640. { Error recovery }
  641. pt:=comp_expr(true);
  642. pt.free;
  643. end
  644. else
  645. begin
  646. { Get the result of the default, the firstpass is
  647. needed to support values like -1 }
  648. pt:=comp_expr(true);
  649. if (p.propdef.typ=setdef) and
  650. (pt.nodetype=arrayconstructorn) then
  651. begin
  652. arrayconstructor_to_set(pt);
  653. do_typecheckpass(pt);
  654. end;
  655. inserttypeconv(pt,p.propdef);
  656. if not(is_constnode(pt)) then
  657. Message(parser_e_property_default_value_must_const);
  658. { Set default value }
  659. case pt.nodetype of
  660. setconstn :
  661. p.default:=plongint(tsetconstnode(pt).value_set)^;
  662. ordconstn :
  663. if (Tordconstnode(pt).value<int64(low(longint))) or
  664. (Tordconstnode(pt).value>int64(high(cardinal))) then
  665. message(parser_e_range_check_error)
  666. else
  667. p.default:=longint(tordconstnode(pt).value.svalue);
  668. niln :
  669. p.default:=0;
  670. realconstn:
  671. p.default:=longint(single(trealconstnode(pt).value_real));
  672. end;
  673. pt.free;
  674. end;
  675. end
  676. else if try_to_consume(_NODEFAULT) then
  677. begin
  678. p.default:=longint($80000000);
  679. end;
  680. (*
  681. else {if allow_default_property(p) then
  682. begin
  683. p.default:=longint($80000000);
  684. end;
  685. *)
  686. { Parse possible "implements" keyword }
  687. if try_to_consume(_IMPLEMENTS) then
  688. begin
  689. single_type(def,false,false);
  690. if not(is_interface(def)) then
  691. message(parser_e_class_implements_must_be_interface);
  692. if is_interface(p.propdef) then
  693. begin
  694. if compare_defs(def,p.propdef,nothingn)<te_equal then
  695. begin
  696. message2(parser_e_implements_must_have_correct_type,def.GetTypeName,p.propdef.GetTypeName);
  697. exit;
  698. end;
  699. end
  700. else if is_class(p.propdef) then
  701. begin
  702. ImplIntf:=tobjectdef(p.propdef).find_implemented_interface(tobjectdef(def));
  703. if assigned(ImplIntf) then
  704. begin
  705. if compare_defs(ImplIntf.IntfDef,def,nothingn)<te_equal then
  706. begin
  707. message2(parser_e_implements_must_have_correct_type,ImplIntf.IntfDef.GetTypeName,def.GetTypeName);
  708. exit;
  709. end;
  710. end
  711. else
  712. begin
  713. message2(parser_e_class_doesnt_implement_interface,p.propdef.GetTypeName,def.GetTypeName);
  714. exit;
  715. end;
  716. end
  717. else
  718. begin
  719. message(parser_e_implements_must_be_class_or_interface);
  720. exit;
  721. end;
  722. if not assigned(p.propaccesslist[palt_read].firstsym) then
  723. begin
  724. message(parser_e_implements_must_read_specifier);
  725. exit;
  726. end;
  727. if assigned(p.propaccesslist[palt_read].procdef) and
  728. (tprocdef(p.propaccesslist[palt_read].procdef).proccalloption<>pocall_default) then
  729. message(parser_e_implements_getter_not_default_cc);
  730. if assigned(p.propaccesslist[palt_write].firstsym) then
  731. begin
  732. message(parser_e_implements_must_not_have_write_specifier);
  733. exit;
  734. end;
  735. if assigned(p.propaccesslist[palt_stored].firstsym) then
  736. begin
  737. message(parser_e_implements_must_not_have_stored_specifier);
  738. exit;
  739. end;
  740. found:=false;
  741. for i:=0 to aclass.ImplementedInterfaces.Count-1 do
  742. begin
  743. ImplIntf:=TImplementedInterface(aclass.ImplementedInterfaces[i]);
  744. if compare_defs(def,ImplIntf.IntfDef,nothingn)>=te_equal then
  745. begin
  746. found:=true;
  747. break;
  748. end;
  749. end;
  750. if found then
  751. begin
  752. ImplIntf.ImplementsGetter:=p;
  753. ImplIntf.VtblImplIntf:=ImplIntf;
  754. case p.propaccesslist[palt_read].firstsym^.sym.typ of
  755. procsym :
  756. begin
  757. if (po_virtualmethod in tprocdef(p.propaccesslist[palt_read].procdef).procoptions) then
  758. ImplIntf.IType:=etVirtualMethodResult
  759. else
  760. ImplIntf.IType:=etStaticMethodResult;
  761. end;
  762. fieldvarsym :
  763. begin
  764. ImplIntf.IType:=etFieldValue;
  765. { this must be done more sophisticated, here is also probably the wrong place }
  766. ImplIntf.IOffset:=tfieldvarsym(p.propaccesslist[palt_read].firstsym^.sym).fieldoffset;
  767. end
  768. else
  769. internalerror(200802161);
  770. end;
  771. if not is_interface(p.propdef) then
  772. case ImplIntf.IType of
  773. etVirtualMethodResult: ImplIntf.IType := etVirtualMethodClass;
  774. etStaticMethodResult: ImplIntf.IType := etStaticMethodClass;
  775. etFieldValue: ImplIntf.IType := etFieldValueClass;
  776. else
  777. internalerror(200912101);
  778. end;
  779. end
  780. else
  781. message1(parser_e_implements_uses_non_implemented_interface,def.GetTypeName);
  782. end;
  783. { remove temporary procvardefs }
  784. readprocdef.owner.deletedef(readprocdef);
  785. writeprocdef.owner.deletedef(writeprocdef);
  786. result:=p;
  787. end;
  788. function maybe_parse_proc_directives(def:tdef):boolean;
  789. var
  790. newtype : ttypesym;
  791. begin
  792. result:=false;
  793. { Process procvar directives before = and ; }
  794. if (def.typ=procvardef) and
  795. (def.typesym=nil) and
  796. check_proc_directive(true) then
  797. begin
  798. newtype:=ttypesym.create('unnamed',def);
  799. parse_var_proc_directives(tsym(newtype));
  800. newtype.typedef:=nil;
  801. def.typesym:=nil;
  802. newtype.free;
  803. result:=true;
  804. end;
  805. end;
  806. const
  807. variantrecordlevel : longint = 0;
  808. procedure read_public_and_external_sc(sc:TFPObjectList);
  809. var
  810. vs: tabstractvarsym;
  811. begin
  812. { only allowed for one var }
  813. vs:=tabstractvarsym(sc[0]);
  814. if sc.count>1 then
  815. Message(parser_e_absolute_only_one_var);
  816. read_public_and_external(vs);
  817. end;
  818. procedure read_public_and_external(vs: tabstractvarsym);
  819. var
  820. is_dll,
  821. is_cdecl,
  822. is_external_var,
  823. is_weak_external,
  824. is_public_var : boolean;
  825. dll_name,
  826. C_name : string;
  827. begin
  828. { only allowed for one var }
  829. { only allow external and public on global symbols }
  830. if vs.typ<>staticvarsym then
  831. begin
  832. Message(parser_e_no_local_var_external);
  833. exit;
  834. end;
  835. { defaults }
  836. is_dll:=false;
  837. is_cdecl:=false;
  838. is_external_var:=false;
  839. is_public_var:=false;
  840. C_name:=vs.realname;
  841. { macpas specific handling due to some switches}
  842. if (m_mac in current_settings.modeswitches) then
  843. begin
  844. if (cs_external_var in current_settings.localswitches) then
  845. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  846. is_cdecl:=true;
  847. is_external_var:=true;
  848. end
  849. else if (cs_externally_visible in current_settings.localswitches) then
  850. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  851. is_cdecl:=true;
  852. is_public_var:=true;
  853. end;
  854. end;
  855. { cdecl }
  856. if try_to_consume(_CVAR) then
  857. begin
  858. consume(_SEMICOLON);
  859. is_cdecl:=true;
  860. end;
  861. { external }
  862. is_weak_external:=try_to_consume(_WEAKEXTERNAL);
  863. if is_weak_external or
  864. try_to_consume(_EXTERNAL) then
  865. begin
  866. is_external_var:=true;
  867. if (idtoken<>_NAME) and (token<>_SEMICOLON) then
  868. begin
  869. is_dll:=true;
  870. dll_name:=get_stringconst;
  871. if ExtractFileExt(dll_name)='' then
  872. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  873. end;
  874. if not(is_cdecl) and try_to_consume(_NAME) then
  875. C_name:=get_stringconst;
  876. consume(_SEMICOLON);
  877. end;
  878. { export or public }
  879. if idtoken in [_EXPORT,_PUBLIC] then
  880. begin
  881. consume(_ID);
  882. if is_external_var then
  883. Message(parser_e_not_external_and_export)
  884. else
  885. is_public_var:=true;
  886. if try_to_consume(_NAME) then
  887. C_name:=get_stringconst;
  888. consume(_SEMICOLON);
  889. end;
  890. { Windows uses an indirect reference using import tables }
  891. if is_dll and
  892. (target_info.system in systems_all_windows) then
  893. include(vs.varoptions,vo_is_dll_var);
  894. { Add C _ prefix }
  895. if is_cdecl or
  896. (
  897. is_dll and
  898. (target_info.system in systems_darwin)
  899. ) then
  900. C_Name := target_info.Cprefix+C_Name;
  901. if is_public_var then
  902. begin
  903. include(vs.varoptions,vo_is_public);
  904. vs.varregable := vr_none;
  905. { mark as referenced }
  906. inc(vs.refs);
  907. end;
  908. { now we can insert it in the import lib if its a dll, or
  909. add it to the externals }
  910. if is_external_var then
  911. begin
  912. if vo_is_typed_const in vs.varoptions then
  913. Message(parser_e_initialized_not_for_external);
  914. include(vs.varoptions,vo_is_external);
  915. if (is_weak_external) then
  916. begin
  917. if not(target_info.system in systems_weak_linking) then
  918. message(parser_e_weak_external_not_supported);
  919. include(vs.varoptions,vo_is_weak_external);
  920. end;
  921. vs.varregable := vr_none;
  922. if is_dll then
  923. current_module.AddExternalImport(dll_name,C_Name,0,true,false)
  924. else
  925. if tf_has_dllscanner in target_info.flags then
  926. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  927. end;
  928. { Set the assembler name }
  929. tstaticvarsym(vs).set_mangledname(C_Name);
  930. end;
  931. procedure read_var_decls(options:Tvar_dec_options);
  932. procedure read_default_value(sc : TFPObjectList);
  933. var
  934. vs : tabstractnormalvarsym;
  935. tcsym : tstaticvarsym;
  936. begin
  937. vs:=tabstractnormalvarsym(sc[0]);
  938. if sc.count>1 then
  939. Message(parser_e_initialized_only_one_var);
  940. if vo_is_thread_var in vs.varoptions then
  941. Message(parser_e_initialized_not_for_threadvar);
  942. consume(_EQUAL);
  943. case vs.typ of
  944. localvarsym :
  945. begin
  946. tcsym:=tstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  947. include(tcsym.symoptions,sp_internal);
  948. vs.defaultconstsym:=tcsym;
  949. symtablestack.top.insert(tcsym);
  950. read_typed_const(current_asmdata.asmlists[al_typedconsts],tcsym,false);
  951. end;
  952. staticvarsym :
  953. begin
  954. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs),false);
  955. end;
  956. else
  957. internalerror(200611051);
  958. end;
  959. vs.varstate:=vs_initialised;
  960. end;
  961. {$ifdef gpc_mode}
  962. procedure read_gpc_name(sc : TFPObjectList);
  963. var
  964. vs : tabstractnormalvarsym;
  965. C_Name : string;
  966. begin
  967. consume(_ID);
  968. C_Name:=get_stringconst;
  969. vs:=tabstractnormalvarsym(sc[0]);
  970. if sc.count>1 then
  971. Message(parser_e_absolute_only_one_var);
  972. if vs.typ=staticvarsym then
  973. begin
  974. tstaticvarsym(vs).set_mangledname(C_Name);
  975. include(vs.varoptions,vo_is_external);
  976. end
  977. else
  978. Message(parser_e_no_local_var_external);
  979. end;
  980. {$endif}
  981. procedure read_absolute(sc : TFPObjectList);
  982. var
  983. vs : tabstractvarsym;
  984. abssym : tabsolutevarsym;
  985. pt,hp : tnode;
  986. st : tsymtable;
  987. {$ifdef i386}
  988. tmpaddr : int64;
  989. {$endif}
  990. begin
  991. abssym:=nil;
  992. { only allowed for one var }
  993. vs:=tabstractvarsym(sc[0]);
  994. if sc.count>1 then
  995. Message(parser_e_absolute_only_one_var);
  996. if vo_is_typed_const in vs.varoptions then
  997. Message(parser_e_initialized_not_for_external);
  998. { parse the rest }
  999. pt:=expr(true);
  1000. { check allowed absolute types }
  1001. if (pt.nodetype=stringconstn) or
  1002. (is_constcharnode(pt)) then
  1003. begin
  1004. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1005. abssym.fileinfo:=vs.fileinfo;
  1006. if pt.nodetype=stringconstn then
  1007. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  1008. else
  1009. abssym.asmname:=stringdup(chr(tordconstnode(pt).value.svalue));
  1010. consume(token);
  1011. abssym.abstyp:=toasm;
  1012. end
  1013. { address }
  1014. else if is_constintnode(pt) then
  1015. begin
  1016. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1017. abssym.fileinfo:=vs.fileinfo;
  1018. abssym.abstyp:=toaddr;
  1019. {$ifndef cpu64bitaddr}
  1020. { on 64 bit systems, abssym.addroffset is a qword and hence this
  1021. test is useless (value is a 64 bit entity) and will always fail
  1022. for positive values (since int64(high(abssym.addroffset))=-1
  1023. }
  1024. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  1025. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  1026. message(parser_e_range_check_error)
  1027. else
  1028. {$endif}
  1029. abssym.addroffset:=Tordconstnode(pt).value.svalue;
  1030. {$ifdef i386}
  1031. abssym.absseg:=false;
  1032. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1033. try_to_consume(_COLON) then
  1034. begin
  1035. pt.free;
  1036. pt:=expr(true);
  1037. if is_constintnode(pt) then
  1038. begin
  1039. tmpaddr:=abssym.addroffset shl 4+tordconstnode(pt).value.svalue;
  1040. if (tmpaddr<int64(low(abssym.addroffset))) or
  1041. (tmpaddr>int64(high(abssym.addroffset))) then
  1042. message(parser_e_range_check_error)
  1043. else
  1044. abssym.addroffset:=tmpaddr;
  1045. abssym.absseg:=true;
  1046. end
  1047. else
  1048. Message(type_e_ordinal_expr_expected);
  1049. end;
  1050. {$endif i386}
  1051. end
  1052. { variable }
  1053. else
  1054. begin
  1055. { we have to be able to take the address of the absolute
  1056. expression
  1057. }
  1058. valid_for_addr(pt,true);
  1059. { remove subscriptn before checking for loadn }
  1060. hp:=pt;
  1061. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  1062. begin
  1063. { check for implicit dereferencing and reject it }
  1064. if (hp.nodetype in [subscriptn,vecn]) then
  1065. begin
  1066. if (tunarynode(hp).left.resultdef.typ in [pointerdef,classrefdef]) then
  1067. break;
  1068. { catch, e.g., 'var b: char absolute pchar_var[5];"
  1069. (pchar_var[5] is a pchar_2_string typeconv ->
  1070. the vecn only sees an array of char)
  1071. I don't know if all of these type conversions are
  1072. possible, but they're definitely all bad.
  1073. }
  1074. if (tunarynode(hp).left.nodetype=typeconvn) and
  1075. (ttypeconvnode(tunarynode(hp).left).convtype in
  1076. [tc_pchar_2_string,tc_pointer_2_array,
  1077. tc_intf_2_string,tc_intf_2_guid,
  1078. tc_dynarray_2_variant,tc_interface_2_variant,
  1079. tc_array_2_dynarray]) then
  1080. break;
  1081. if (tunarynode(hp).left.resultdef.typ=stringdef) and
  1082. not(tstringdef(tunarynode(hp).left.resultdef).stringtype in [st_shortstring,st_longstring]) then
  1083. break;
  1084. if (tunarynode(hp).left.resultdef.typ=objectdef) and
  1085. (tobjectdef(tunarynode(hp).left.resultdef).objecttype<>odt_object) then
  1086. break;
  1087. if is_dynamic_array(tunarynode(hp).left.resultdef) then
  1088. break;
  1089. end;
  1090. hp:=tunarynode(hp).left;
  1091. end;
  1092. if (hp.nodetype=loadn) then
  1093. begin
  1094. { we should check the result type of loadn }
  1095. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym]) then
  1096. Message(parser_e_absolute_only_to_var_or_const);
  1097. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1098. abssym.fileinfo:=vs.fileinfo;
  1099. abssym.abstyp:=tovar;
  1100. abssym.ref:=node_to_propaccesslist(pt);
  1101. { if the sizes are different, can't be a regvar since you }
  1102. { can't be "absolute upper 8 bits of a register" (except }
  1103. { if its a record field of the same size of a record }
  1104. { regvar, but in that case pt.resultdef.size will have }
  1105. { the same size since it refers to the field and not to }
  1106. { the whole record -- which is why we use pt and not hp) }
  1107. { we can't take the size of an open array }
  1108. if is_open_array(pt.resultdef) or
  1109. (vs.vardef.size <> pt.resultdef.size) then
  1110. make_not_regable(pt,[ra_addr_regable]);
  1111. end
  1112. else
  1113. Message(parser_e_absolute_only_to_var_or_const);
  1114. end;
  1115. pt.free;
  1116. { replace old varsym with the new absolutevarsym }
  1117. if assigned(abssym) then
  1118. begin
  1119. st:=vs.owner;
  1120. vs.owner.Delete(vs);
  1121. st.insert(abssym);
  1122. sc[0]:=abssym;
  1123. end;
  1124. end;
  1125. var
  1126. sc : TFPObjectList;
  1127. vs : tabstractvarsym;
  1128. hdef : tdef;
  1129. i : longint;
  1130. semicoloneaten,
  1131. allowdefaultvalue,
  1132. hasdefaultvalue : boolean;
  1133. hintsymoptions : tsymoptions;
  1134. deprecatedmsg : pshortstring;
  1135. old_block_type : tblock_type;
  1136. begin
  1137. old_block_type:=block_type;
  1138. block_type:=bt_var;
  1139. { Force an expected ID error message }
  1140. if not (token in [_ID,_CASE,_END]) then
  1141. consume(_ID);
  1142. { read vars }
  1143. sc:=TFPObjectList.create(false);
  1144. while (token=_ID) do
  1145. begin
  1146. semicoloneaten:=false;
  1147. hasdefaultvalue:=false;
  1148. allowdefaultvalue:=true;
  1149. sc.clear;
  1150. repeat
  1151. if (token = _ID) then
  1152. begin
  1153. case symtablestack.top.symtabletype of
  1154. localsymtable :
  1155. vs:=tlocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  1156. staticsymtable,
  1157. globalsymtable :
  1158. begin
  1159. vs:=tstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  1160. if vd_threadvar in options then
  1161. include(vs.varoptions,vo_is_thread_var);
  1162. end;
  1163. else
  1164. internalerror(200411064);
  1165. end;
  1166. sc.add(vs);
  1167. symtablestack.top.insert(vs);
  1168. end;
  1169. consume(_ID);
  1170. until not try_to_consume(_COMMA);
  1171. { read variable type def }
  1172. block_type:=bt_var_type;
  1173. consume(_COLON);
  1174. {$ifdef gpc_mode}
  1175. if (m_gpc in current_settings.modeswitches) and
  1176. (token=_ID) and
  1177. (orgpattern='__asmname__') then
  1178. read_gpc_name(sc);
  1179. {$endif}
  1180. read_anon_type(hdef,false);
  1181. for i:=0 to sc.count-1 do
  1182. begin
  1183. vs:=tabstractvarsym(sc[i]);
  1184. vs.vardef:=hdef;
  1185. end;
  1186. block_type:=bt_var;
  1187. { Process procvar directives }
  1188. if maybe_parse_proc_directives(hdef) then
  1189. semicoloneaten:=true;
  1190. { check for absolute }
  1191. if try_to_consume(_ABSOLUTE) then
  1192. begin
  1193. read_absolute(sc);
  1194. allowdefaultvalue:=false;
  1195. end;
  1196. { Check for EXTERNAL etc directives before a semicolon }
  1197. if (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) then
  1198. begin
  1199. read_public_and_external_sc(sc);
  1200. allowdefaultvalue:=false;
  1201. semicoloneaten:=true;
  1202. end;
  1203. { try to parse the hint directives }
  1204. hintsymoptions:=[];
  1205. deprecatedmsg:=nil;
  1206. try_consume_hintdirective(hintsymoptions,deprecatedmsg);
  1207. for i:=0 to sc.count-1 do
  1208. begin
  1209. vs:=tabstractvarsym(sc[i]);
  1210. vs.symoptions := vs.symoptions + hintsymoptions;
  1211. if deprecatedmsg<>nil then
  1212. vs.deprecatedmsg:=stringdup(deprecatedmsg^);
  1213. end;
  1214. stringdispose(deprecatedmsg);
  1215. { Handling of Delphi typed const = initialized vars }
  1216. if allowdefaultvalue and
  1217. (token=_EQUAL) and
  1218. not(m_tp7 in current_settings.modeswitches) and
  1219. (symtablestack.top.symtabletype<>parasymtable) then
  1220. begin
  1221. { Add calling convention for procvar }
  1222. if (hdef.typ=procvardef) and
  1223. (hdef.typesym=nil) then
  1224. handle_calling_convention(tprocvardef(hdef));
  1225. read_default_value(sc);
  1226. hasdefaultvalue:=true;
  1227. end
  1228. else
  1229. begin
  1230. if not(semicoloneaten) then
  1231. consume(_SEMICOLON);
  1232. end;
  1233. { Support calling convention for procvars after semicolon }
  1234. if not(hasdefaultvalue) and
  1235. (hdef.typ=procvardef) and
  1236. (hdef.typesym=nil) then
  1237. begin
  1238. { Parse procvar directives after ; }
  1239. maybe_parse_proc_directives(hdef);
  1240. { Add calling convention for procvar }
  1241. handle_calling_convention(tprocvardef(hdef));
  1242. { Handling of Delphi typed const = initialized vars }
  1243. if (token=_EQUAL) and
  1244. not(m_tp7 in current_settings.modeswitches) and
  1245. (symtablestack.top.symtabletype<>parasymtable) then
  1246. begin
  1247. read_default_value(sc);
  1248. hasdefaultvalue:=true;
  1249. end;
  1250. end;
  1251. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1252. if (
  1253. (
  1254. (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) and
  1255. (m_cvar_support in current_settings.modeswitches)
  1256. ) or
  1257. (
  1258. (m_mac in current_settings.modeswitches) and
  1259. (
  1260. (cs_external_var in current_settings.localswitches) or
  1261. (cs_externally_visible in current_settings.localswitches)
  1262. )
  1263. )
  1264. ) then
  1265. read_public_and_external_sc(sc);
  1266. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1267. for i:=0 to sc.count-1 do
  1268. begin
  1269. vs:=tabstractvarsym(sc[i]);
  1270. if (vs.typ=staticvarsym) and
  1271. not(vo_is_typed_const in vs.varoptions) and
  1272. not(vo_is_external in vs.varoptions) then
  1273. insertbssdata(tstaticvarsym(vs));
  1274. end;
  1275. end;
  1276. block_type:=old_block_type;
  1277. { free the list }
  1278. sc.free;
  1279. end;
  1280. procedure read_record_fields(options:Tvar_dec_options);
  1281. var
  1282. sc : TFPObjectList;
  1283. i : longint;
  1284. hs,sorg : string;
  1285. hdef,casetype : tdef;
  1286. { maxsize contains the max. size of a variant }
  1287. { startvarrec contains the start of the variant part of a record }
  1288. maxsize, startvarrecsize : longint;
  1289. usedalign,
  1290. maxalignment,startvarrecalign,
  1291. maxpadalign, startpadalign: shortint;
  1292. pt : tnode;
  1293. fieldvs : tfieldvarsym;
  1294. hstaticvs : tstaticvarsym;
  1295. vs : tabstractvarsym;
  1296. srsym : tsym;
  1297. srsymtable : TSymtable;
  1298. visibility : tvisibility;
  1299. recst : tabstractrecordsymtable;
  1300. recstlist : tfpobjectlist;
  1301. unionsymtable : trecordsymtable;
  1302. offset : longint;
  1303. uniondef : trecorddef;
  1304. hintsymoptions : tsymoptions;
  1305. deprecatedmsg : pshortstring;
  1306. semicoloneaten: boolean;
  1307. {$if defined(powerpc) or defined(powerpc64)}
  1308. tempdef: tdef;
  1309. is_first_type: boolean;
  1310. {$endif powerpc or powerpc64}
  1311. sl : tpropaccesslist;
  1312. begin
  1313. recst:=tabstractrecordsymtable(symtablestack.top);
  1314. {$if defined(powerpc) or defined(powerpc64)}
  1315. is_first_type:=true;
  1316. {$endif powerpc or powerpc64}
  1317. { Force an expected ID error message }
  1318. if not (token in [_ID,_CASE,_END]) then
  1319. consume(_ID);
  1320. { read vars }
  1321. sc:=TFPObjectList.create(false);
  1322. recstlist:=TFPObjectList.create(false);;
  1323. while (token=_ID) and
  1324. not((vd_object in options) and
  1325. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1326. begin
  1327. visibility:=symtablestack.top.currentvisibility;
  1328. semicoloneaten:=false;
  1329. sc.clear;
  1330. repeat
  1331. sorg:=orgpattern;
  1332. if token=_ID then
  1333. begin
  1334. vs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1335. sc.add(vs);
  1336. recst.insert(vs);
  1337. end;
  1338. consume(_ID);
  1339. until not try_to_consume(_COMMA);
  1340. consume(_COLON);
  1341. { Don't search in the recordsymtable for types (can be nested!) }
  1342. recstlist.count:=0;
  1343. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) and
  1344. not is_class_or_object(tdef(recst.defowner)) then
  1345. begin
  1346. recstlist.add(recst);
  1347. symtablestack.pop(recst);
  1348. while (symtablestack.top.symtabletype=recordsymtable) and
  1349. ([df_generic,df_specialization]*tdef(symtablestack.top.defowner).defoptions=[]) do
  1350. begin
  1351. recst:=tabstractrecordsymtable(symtablestack.top);
  1352. recstlist.add(recst);
  1353. symtablestack.pop(recst);
  1354. end;
  1355. end;
  1356. read_anon_type(hdef,false);
  1357. { restore stack }
  1358. for i:=recstlist.count-1 downto 0 do
  1359. begin
  1360. recst:=tabstractrecordsymtable(recstlist[i]);
  1361. symtablestack.push(recst);
  1362. end;
  1363. { Process procvar directives }
  1364. if maybe_parse_proc_directives(hdef) then
  1365. semicoloneaten:=true;
  1366. {$if defined(powerpc) or defined(powerpc64)}
  1367. { from gcc/gcc/config/rs6000/rs6000.h:
  1368. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1369. /* Return the alignment of a struct based on the Macintosh PowerPC
  1370. alignment rules. In general the alignment of a struct is
  1371. determined by the greatest alignment of its elements. However, the
  1372. PowerPC rules cause the alignment of a struct to peg at word
  1373. alignment except when the first field has greater than word
  1374. (32-bit) alignment, in which case the alignment is determined by
  1375. the alignment of the first field. */
  1376. }
  1377. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1378. is_first_type and
  1379. (symtablestack.top.symtabletype=recordsymtable) and
  1380. (trecordsymtable(symtablestack.top).usefieldalignment=C_alignment) then
  1381. begin
  1382. tempdef:=hdef;
  1383. while tempdef.typ=arraydef do
  1384. tempdef:=tarraydef(tempdef).elementdef;
  1385. if tempdef.typ<>recorddef then
  1386. maxpadalign:=tempdef.alignment
  1387. else
  1388. maxpadalign:=trecorddef(tempdef).padalignment;
  1389. if (maxpadalign>4) and
  1390. (maxpadalign>trecordsymtable(symtablestack.top).padalignment) then
  1391. trecordsymtable(symtablestack.top).padalignment:=maxpadalign;
  1392. is_first_type:=false;
  1393. end;
  1394. {$endif powerpc or powerpc64}
  1395. { types that use init/final are not allowed in variant parts, but
  1396. classes are allowed }
  1397. if (variantrecordlevel>0) and
  1398. is_managed_type(hdef) then
  1399. Message(parser_e_cant_use_inittable_here);
  1400. { try to parse the hint directives }
  1401. hintsymoptions:=[];
  1402. deprecatedmsg:=nil;
  1403. try_consume_hintdirective(hintsymoptions,deprecatedmsg);
  1404. { update variable type and hints }
  1405. for i:=0 to sc.count-1 do
  1406. begin
  1407. fieldvs:=tfieldvarsym(sc[i]);
  1408. fieldvs.vardef:=hdef;
  1409. { insert any additional hint directives }
  1410. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1411. if deprecatedmsg<>nil then
  1412. fieldvs.deprecatedmsg:=stringdup(deprecatedmsg^);
  1413. end;
  1414. stringdispose(deprecatedmsg);
  1415. { Records and objects can't have default values }
  1416. { for a record there doesn't need to be a ; before the END or ) }
  1417. if not(token in [_END,_RKLAMMER]) and
  1418. not(semicoloneaten) then
  1419. consume(_SEMICOLON);
  1420. { Parse procvar directives after ; }
  1421. maybe_parse_proc_directives(hdef);
  1422. { Add calling convention for procvar }
  1423. if (hdef.typ=procvardef) and
  1424. (hdef.typesym=nil) then
  1425. handle_calling_convention(tprocvardef(hdef));
  1426. { check if it is a class field }
  1427. if (vd_object in options) then
  1428. begin
  1429. { if it is not a class var section and token=STATIC then it is a class field too }
  1430. if not (vd_class in options) and try_to_consume(_STATIC) then
  1431. begin
  1432. consume(_SEMICOLON);
  1433. include(options, vd_class);
  1434. end;
  1435. if vd_class in options then
  1436. begin
  1437. { add static flag and staticvarsyms }
  1438. for i:=0 to sc.count-1 do
  1439. begin
  1440. fieldvs:=tfieldvarsym(sc[i]);
  1441. include(fieldvs.symoptions,sp_static);
  1442. { generate the symbol which reserves the space }
  1443. hstaticvs:=tstaticvarsym.create('$_static_'+lower(symtablestack.top.name^)+'_'+fieldvs.name,vs_value,hdef,[]);
  1444. include(hstaticvs.symoptions,sp_internal);
  1445. recst.defowner.owner.insert(hstaticvs);
  1446. insertbssdata(hstaticvs);
  1447. { generate the symbol for the access }
  1448. sl:=tpropaccesslist.create;
  1449. sl.addsym(sl_load,hstaticvs);
  1450. recst.insert(tabsolutevarsym.create_ref('$'+lower(symtablestack.top.name^)+'_'+fieldvs.name,hdef,sl));
  1451. end;
  1452. end;
  1453. end;
  1454. if (visibility=vis_published) and
  1455. not(is_class(hdef)) then
  1456. begin
  1457. Message(parser_e_cant_publish_that);
  1458. visibility:=vis_public;
  1459. end;
  1460. if (visibility=vis_published) and
  1461. not(oo_can_have_published in tobjectdef(hdef).objectoptions) and
  1462. not(m_delphi in current_settings.modeswitches) then
  1463. begin
  1464. Message(parser_e_only_publishable_classes_can_be_published);
  1465. visibility:=vis_public;
  1466. end;
  1467. { Generate field in the recordsymtable }
  1468. for i:=0 to sc.count-1 do
  1469. begin
  1470. fieldvs:=tfieldvarsym(sc[i]);
  1471. { static data fields are already inserted in the globalsymtable }
  1472. if not(sp_static in fieldvs.symoptions) then
  1473. recst.addfield(fieldvs,visibility);
  1474. end;
  1475. end;
  1476. recstlist.free;
  1477. { Check for Case }
  1478. if (vd_record in options) and
  1479. try_to_consume(_CASE) then
  1480. begin
  1481. maxsize:=0;
  1482. maxalignment:=0;
  1483. maxpadalign:=0;
  1484. { including a field declaration? }
  1485. fieldvs:=nil;
  1486. sorg:=orgpattern;
  1487. hs:=pattern;
  1488. searchsym(hs,srsym,srsymtable);
  1489. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1490. begin
  1491. consume(_ID);
  1492. consume(_COLON);
  1493. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1494. symtablestack.top.insert(fieldvs);
  1495. end;
  1496. read_anon_type(casetype,true);
  1497. if assigned(fieldvs) then
  1498. begin
  1499. fieldvs.vardef:=casetype;
  1500. recst.addfield(fieldvs,recst.currentvisibility);
  1501. end;
  1502. if not(is_ordinal(casetype))
  1503. {$ifndef cpu64bitaddr}
  1504. or is_64bitint(casetype)
  1505. {$endif cpu64bitaddr}
  1506. then
  1507. Message(type_e_ordinal_expr_expected);
  1508. consume(_OF);
  1509. UnionSymtable:=trecordsymtable.create(current_settings.packrecords);
  1510. UnionDef:=trecorddef.create(unionsymtable);
  1511. uniondef.isunion:=true;
  1512. startvarrecsize:=UnionSymtable.datasize;
  1513. { align the bitpacking to the next byte }
  1514. UnionSymtable.datasize:=startvarrecsize;
  1515. startvarrecalign:=UnionSymtable.fieldalignment;
  1516. startpadalign:=Unionsymtable.padalignment;
  1517. symtablestack.push(UnionSymtable);
  1518. repeat
  1519. repeat
  1520. pt:=comp_expr(true);
  1521. if not(pt.nodetype=ordconstn) then
  1522. Message(parser_e_illegal_expression);
  1523. if try_to_consume(_POINTPOINT) then
  1524. pt:=crangenode.create(pt,comp_expr(true));
  1525. pt.free;
  1526. if token=_COMMA then
  1527. consume(_COMMA)
  1528. else
  1529. break;
  1530. until false;
  1531. consume(_COLON);
  1532. { read the vars }
  1533. consume(_LKLAMMER);
  1534. inc(variantrecordlevel);
  1535. if token<>_RKLAMMER then
  1536. read_record_fields([vd_record]);
  1537. dec(variantrecordlevel);
  1538. consume(_RKLAMMER);
  1539. { calculates maximal variant size }
  1540. maxsize:=max(maxsize,unionsymtable.datasize);
  1541. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1542. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1543. { the items of the next variant are overlayed }
  1544. unionsymtable.datasize:=startvarrecsize;
  1545. unionsymtable.fieldalignment:=startvarrecalign;
  1546. unionsymtable.padalignment:=startpadalign;
  1547. if (token<>_END) and (token<>_RKLAMMER) then
  1548. consume(_SEMICOLON)
  1549. else
  1550. break;
  1551. until (token=_END) or (token=_RKLAMMER);
  1552. symtablestack.pop(UnionSymtable);
  1553. { at last set the record size to that of the biggest variant }
  1554. unionsymtable.datasize:=maxsize;
  1555. unionsymtable.fieldalignment:=maxalignment;
  1556. unionsymtable.addalignmentpadding;
  1557. {$if defined(powerpc) or defined(powerpc64)}
  1558. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1559. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1560. is_first_type and
  1561. (recst.usefieldalignment=C_alignment) and
  1562. (maxpadalign>recst.padalignment) then
  1563. recst.padalignment:=maxpadalign;
  1564. {$endif powerpc or powerpc64}
  1565. { Align the offset where the union symtable is added }
  1566. case recst.usefieldalignment of
  1567. { allow the unionsymtable to be aligned however it wants }
  1568. { (within the global min/max limits) }
  1569. 0, { default }
  1570. C_alignment:
  1571. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  1572. { 1 byte alignment if we are bitpacked }
  1573. bit_alignment:
  1574. usedalign:=1;
  1575. mac68k_alignment:
  1576. usedalign:=2;
  1577. { otherwise alignment at the packrecords alignment of the }
  1578. { current record }
  1579. else
  1580. usedalign:=used_align(recst.fieldalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1581. end;
  1582. offset:=align(recst.datasize,usedalign);
  1583. recst.datasize:=offset+unionsymtable.datasize;
  1584. if unionsymtable.recordalignment>recst.fieldalignment then
  1585. recst.fieldalignment:=unionsymtable.recordalignment;
  1586. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1587. uniondef.owner.deletedef(uniondef);
  1588. end;
  1589. { free the list }
  1590. sc.free;
  1591. {$ifdef powerpc}
  1592. is_first_type := false;
  1593. {$endif powerpc}
  1594. end;
  1595. end.