pdecvar.pas 72 KB

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