pdecvar.pas 64 KB

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