pdecvar.pas 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  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) and
  803. not is_objectpascal_helper(tprocdef(p.propaccesslist[palt_read].procdef).struct) then
  804. ImplIntf.IType:=etVirtualMethodResult
  805. else
  806. ImplIntf.IType:=etStaticMethodResult;
  807. end;
  808. fieldvarsym :
  809. begin
  810. ImplIntf.IType:=etFieldValue;
  811. { this must be done more sophisticated, here is also probably the wrong place }
  812. ImplIntf.IOffset:=tfieldvarsym(p.propaccesslist[palt_read].firstsym^.sym).fieldoffset;
  813. end
  814. else
  815. internalerror(200802161);
  816. end;
  817. if not is_interface(p.propdef) then
  818. case ImplIntf.IType of
  819. etVirtualMethodResult: ImplIntf.IType := etVirtualMethodClass;
  820. etStaticMethodResult: ImplIntf.IType := etStaticMethodClass;
  821. etFieldValue: ImplIntf.IType := etFieldValueClass;
  822. else
  823. internalerror(200912101);
  824. end;
  825. end
  826. else
  827. message1(parser_e_implements_uses_non_implemented_interface,def.typename);
  828. end;
  829. { remove unneeded procdefs }
  830. if readprocdef.proctypeoption<>potype_propgetter then
  831. readprocdef.owner.deletedef(readprocdef);
  832. if writeprocdef.proctypeoption<>potype_propsetter then
  833. writeprocdef.owner.deletedef(writeprocdef);
  834. result:=p;
  835. end;
  836. function maybe_parse_proc_directives(def:tdef):boolean;
  837. var
  838. newtype : ttypesym;
  839. begin
  840. result:=false;
  841. { Process procvar directives before = and ; }
  842. if (def.typ=procvardef) and
  843. (def.typesym=nil) and
  844. check_proc_directive(true) then
  845. begin
  846. newtype:=ttypesym.create('unnamed',def);
  847. parse_var_proc_directives(tsym(newtype));
  848. newtype.typedef:=nil;
  849. def.typesym:=nil;
  850. newtype.free;
  851. result:=true;
  852. end;
  853. end;
  854. const
  855. variantrecordlevel : longint = 0;
  856. procedure read_public_and_external_sc(sc:TFPObjectList);
  857. var
  858. vs: tabstractvarsym;
  859. begin
  860. { only allowed for one var }
  861. vs:=tabstractvarsym(sc[0]);
  862. if sc.count>1 then
  863. Message(parser_e_absolute_only_one_var);
  864. read_public_and_external(vs);
  865. end;
  866. procedure read_public_and_external(vs: tabstractvarsym);
  867. var
  868. is_dll,
  869. is_cdecl,
  870. is_external_var,
  871. is_weak_external,
  872. is_public_var : boolean;
  873. dll_name,
  874. C_name : string;
  875. begin
  876. { only allowed for one var }
  877. { only allow external and public on global symbols }
  878. if vs.typ<>staticvarsym then
  879. begin
  880. Message(parser_e_no_local_var_external);
  881. exit;
  882. end;
  883. { defaults }
  884. is_dll:=false;
  885. is_cdecl:=false;
  886. is_external_var:=false;
  887. is_public_var:=false;
  888. C_name:=vs.realname;
  889. { macpas specific handling due to some switches}
  890. if (m_mac in current_settings.modeswitches) then
  891. begin
  892. if (cs_external_var in current_settings.localswitches) then
  893. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  894. is_cdecl:=true;
  895. is_external_var:=true;
  896. end
  897. else if (cs_externally_visible in current_settings.localswitches) then
  898. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  899. is_cdecl:=true;
  900. is_public_var:=true;
  901. end;
  902. end;
  903. { cdecl }
  904. if try_to_consume(_CVAR) then
  905. begin
  906. consume(_SEMICOLON);
  907. is_cdecl:=true;
  908. end;
  909. { external }
  910. is_weak_external:=try_to_consume(_WEAKEXTERNAL);
  911. if is_weak_external or
  912. try_to_consume(_EXTERNAL) then
  913. begin
  914. is_external_var:=true;
  915. if (idtoken<>_NAME) and (token<>_SEMICOLON) then
  916. begin
  917. is_dll:=true;
  918. dll_name:=get_stringconst;
  919. if ExtractFileExt(dll_name)='' then
  920. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  921. end;
  922. if not(is_cdecl) and try_to_consume(_NAME) then
  923. C_name:=get_stringconst;
  924. consume(_SEMICOLON);
  925. end;
  926. { export or public }
  927. if idtoken in [_EXPORT,_PUBLIC] then
  928. begin
  929. consume(_ID);
  930. if is_external_var then
  931. Message(parser_e_not_external_and_export)
  932. else
  933. is_public_var:=true;
  934. if try_to_consume(_NAME) then
  935. C_name:=get_stringconst;
  936. consume(_SEMICOLON);
  937. end;
  938. { Windows uses an indirect reference using import tables }
  939. if is_dll and
  940. (target_info.system in systems_all_windows) then
  941. include(vs.varoptions,vo_is_dll_var);
  942. { Add C _ prefix }
  943. if is_cdecl or
  944. (
  945. is_dll and
  946. (target_info.system in systems_darwin)
  947. ) then
  948. C_Name := target_info.Cprefix+C_Name;
  949. if is_public_var then
  950. begin
  951. include(vs.varoptions,vo_is_public);
  952. vs.varregable := vr_none;
  953. { mark as referenced }
  954. inc(vs.refs);
  955. end;
  956. { now we can insert it in the import lib if its a dll, or
  957. add it to the externals }
  958. if is_external_var then
  959. begin
  960. if vo_is_typed_const in vs.varoptions then
  961. Message(parser_e_initialized_not_for_external);
  962. include(vs.varoptions,vo_is_external);
  963. if (is_weak_external) then
  964. begin
  965. if not(target_info.system in systems_weak_linking) then
  966. message(parser_e_weak_external_not_supported);
  967. include(vs.varoptions,vo_is_weak_external);
  968. end;
  969. vs.varregable := vr_none;
  970. if is_dll then
  971. current_module.AddExternalImport(dll_name,C_Name,0,true,false)
  972. else
  973. if tf_has_dllscanner in target_info.flags then
  974. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  975. end;
  976. { Set the assembler name }
  977. tstaticvarsym(vs).set_mangledname(C_Name);
  978. end;
  979. procedure read_var_decls(options:Tvar_dec_options);
  980. procedure read_default_value(sc : TFPObjectList);
  981. var
  982. vs : tabstractnormalvarsym;
  983. tcsym : tstaticvarsym;
  984. begin
  985. vs:=tabstractnormalvarsym(sc[0]);
  986. if sc.count>1 then
  987. Message(parser_e_initialized_only_one_var);
  988. if vo_is_thread_var in vs.varoptions then
  989. Message(parser_e_initialized_not_for_threadvar);
  990. consume(_EQ);
  991. case vs.typ of
  992. localvarsym :
  993. begin
  994. tcsym:=tstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  995. include(tcsym.symoptions,sp_internal);
  996. vs.defaultconstsym:=tcsym;
  997. symtablestack.top.insert(tcsym);
  998. read_typed_const(current_asmdata.asmlists[al_typedconsts],tcsym,false);
  999. end;
  1000. staticvarsym :
  1001. begin
  1002. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs),false);
  1003. end;
  1004. else
  1005. internalerror(200611051);
  1006. end;
  1007. vs.varstate:=vs_initialised;
  1008. end;
  1009. {$ifdef gpc_mode}
  1010. procedure read_gpc_name(sc : TFPObjectList);
  1011. var
  1012. vs : tabstractnormalvarsym;
  1013. C_Name : string;
  1014. begin
  1015. consume(_ID);
  1016. C_Name:=get_stringconst;
  1017. vs:=tabstractnormalvarsym(sc[0]);
  1018. if sc.count>1 then
  1019. Message(parser_e_absolute_only_one_var);
  1020. if vs.typ=staticvarsym then
  1021. begin
  1022. tstaticvarsym(vs).set_mangledname(C_Name);
  1023. include(vs.varoptions,vo_is_external);
  1024. end
  1025. else
  1026. Message(parser_e_no_local_var_external);
  1027. end;
  1028. {$endif}
  1029. procedure read_absolute(sc : TFPObjectList);
  1030. var
  1031. vs : tabstractvarsym;
  1032. abssym : tabsolutevarsym;
  1033. pt,hp : tnode;
  1034. st : tsymtable;
  1035. {$ifdef i386}
  1036. tmpaddr : int64;
  1037. {$endif}
  1038. begin
  1039. abssym:=nil;
  1040. { only allowed for one var }
  1041. vs:=tabstractvarsym(sc[0]);
  1042. if sc.count>1 then
  1043. Message(parser_e_absolute_only_one_var);
  1044. if vo_is_typed_const in vs.varoptions then
  1045. Message(parser_e_initialized_not_for_external);
  1046. { parse the rest }
  1047. pt:=expr(true);
  1048. { check allowed absolute types }
  1049. if (pt.nodetype=stringconstn) or
  1050. (is_constcharnode(pt)) then
  1051. begin
  1052. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1053. abssym.fileinfo:=vs.fileinfo;
  1054. if pt.nodetype=stringconstn then
  1055. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  1056. else
  1057. abssym.asmname:=stringdup(chr(tordconstnode(pt).value.svalue));
  1058. consume(token);
  1059. abssym.abstyp:=toasm;
  1060. end
  1061. { address }
  1062. else if is_constintnode(pt) then
  1063. begin
  1064. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1065. abssym.fileinfo:=vs.fileinfo;
  1066. abssym.abstyp:=toaddr;
  1067. {$ifndef cpu64bitaddr}
  1068. { on 64 bit systems, abssym.addroffset is a qword and hence this
  1069. test is useless (value is a 64 bit entity) and will always fail
  1070. for positive values (since int64(high(abssym.addroffset))=-1
  1071. }
  1072. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  1073. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  1074. message(parser_e_range_check_error)
  1075. else
  1076. {$endif}
  1077. abssym.addroffset:=Tordconstnode(pt).value.svalue;
  1078. {$ifdef i386}
  1079. abssym.absseg:=false;
  1080. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1081. try_to_consume(_COLON) then
  1082. begin
  1083. pt.free;
  1084. pt:=expr(true);
  1085. if is_constintnode(pt) then
  1086. begin
  1087. tmpaddr:=abssym.addroffset shl 4+tordconstnode(pt).value.svalue;
  1088. if (tmpaddr<int64(low(abssym.addroffset))) or
  1089. (tmpaddr>int64(high(abssym.addroffset))) then
  1090. message(parser_e_range_check_error)
  1091. else
  1092. abssym.addroffset:=tmpaddr;
  1093. abssym.absseg:=true;
  1094. end
  1095. else
  1096. Message(type_e_ordinal_expr_expected);
  1097. end;
  1098. {$endif i386}
  1099. end
  1100. { variable }
  1101. else
  1102. begin
  1103. { we have to be able to take the address of the absolute
  1104. expression
  1105. }
  1106. valid_for_addr(pt,true);
  1107. { remove subscriptn before checking for loadn }
  1108. hp:=pt;
  1109. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  1110. begin
  1111. { check for implicit dereferencing and reject it }
  1112. if (hp.nodetype in [subscriptn,vecn]) then
  1113. begin
  1114. if (tunarynode(hp).left.resultdef.typ in [pointerdef,classrefdef]) then
  1115. break;
  1116. { catch, e.g., 'var b: char absolute pchar_var[5];"
  1117. (pchar_var[5] is a pchar_2_string typeconv ->
  1118. the vecn only sees an array of char)
  1119. I don't know if all of these type conversions are
  1120. possible, but they're definitely all bad.
  1121. }
  1122. if (tunarynode(hp).left.nodetype=typeconvn) and
  1123. (ttypeconvnode(tunarynode(hp).left).convtype in
  1124. [tc_pchar_2_string,tc_pointer_2_array,
  1125. tc_intf_2_string,tc_intf_2_guid,
  1126. tc_dynarray_2_variant,tc_interface_2_variant,
  1127. tc_array_2_dynarray]) then
  1128. break;
  1129. if (tunarynode(hp).left.resultdef.typ=stringdef) and
  1130. not(tstringdef(tunarynode(hp).left.resultdef).stringtype in [st_shortstring,st_longstring]) then
  1131. break;
  1132. if (tunarynode(hp).left.resultdef.typ=objectdef) and
  1133. (tobjectdef(tunarynode(hp).left.resultdef).objecttype<>odt_object) then
  1134. break;
  1135. if is_dynamic_array(tunarynode(hp).left.resultdef) then
  1136. break;
  1137. end;
  1138. hp:=tunarynode(hp).left;
  1139. end;
  1140. if (hp.nodetype=loadn) then
  1141. begin
  1142. { we should check the result type of loadn }
  1143. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym]) then
  1144. Message(parser_e_absolute_only_to_var_or_const);
  1145. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1146. abssym.fileinfo:=vs.fileinfo;
  1147. abssym.abstyp:=tovar;
  1148. abssym.ref:=node_to_propaccesslist(pt);
  1149. { if the sizes are different, can't be a regvar since you }
  1150. { can't be "absolute upper 8 bits of a register" (except }
  1151. { if its a record field of the same size of a record }
  1152. { regvar, but in that case pt.resultdef.size will have }
  1153. { the same size since it refers to the field and not to }
  1154. { the whole record -- which is why we use pt and not hp) }
  1155. { we can't take the size of an open array }
  1156. if is_open_array(pt.resultdef) or
  1157. (vs.vardef.size <> pt.resultdef.size) then
  1158. make_not_regable(pt,[ra_addr_regable]);
  1159. end
  1160. else
  1161. Message(parser_e_absolute_only_to_var_or_const);
  1162. end;
  1163. pt.free;
  1164. { replace old varsym with the new absolutevarsym }
  1165. if assigned(abssym) then
  1166. begin
  1167. st:=vs.owner;
  1168. vs.owner.Delete(vs);
  1169. st.insert(abssym);
  1170. sc[0]:=abssym;
  1171. end;
  1172. end;
  1173. var
  1174. sc : TFPObjectList;
  1175. vs : tabstractvarsym;
  1176. hdef : tdef;
  1177. i : longint;
  1178. semicoloneaten,
  1179. allowdefaultvalue,
  1180. hasdefaultvalue : boolean;
  1181. hintsymoptions : tsymoptions;
  1182. deprecatedmsg : pshortstring;
  1183. old_block_type : tblock_type;
  1184. begin
  1185. old_block_type:=block_type;
  1186. block_type:=bt_var;
  1187. { Force an expected ID error message }
  1188. if not (token in [_ID,_CASE,_END]) then
  1189. consume(_ID);
  1190. { read vars }
  1191. sc:=TFPObjectList.create(false);
  1192. while (token=_ID) do
  1193. begin
  1194. semicoloneaten:=false;
  1195. hasdefaultvalue:=false;
  1196. allowdefaultvalue:=true;
  1197. sc.clear;
  1198. repeat
  1199. if (token = _ID) then
  1200. begin
  1201. case symtablestack.top.symtabletype of
  1202. localsymtable :
  1203. vs:=tlocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  1204. staticsymtable,
  1205. globalsymtable :
  1206. begin
  1207. vs:=tstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  1208. if vd_threadvar in options then
  1209. include(vs.varoptions,vo_is_thread_var);
  1210. end;
  1211. else
  1212. internalerror(200411064);
  1213. end;
  1214. sc.add(vs);
  1215. symtablestack.top.insert(vs);
  1216. end;
  1217. consume(_ID);
  1218. until not try_to_consume(_COMMA);
  1219. { read variable type def }
  1220. block_type:=bt_var_type;
  1221. consume(_COLON);
  1222. {$ifdef gpc_mode}
  1223. if (m_gpc in current_settings.modeswitches) and
  1224. (token=_ID) and
  1225. (orgpattern='__asmname__') then
  1226. read_gpc_name(sc);
  1227. {$endif}
  1228. read_anon_type(hdef,false);
  1229. for i:=0 to sc.count-1 do
  1230. begin
  1231. vs:=tabstractvarsym(sc[i]);
  1232. vs.vardef:=hdef;
  1233. end;
  1234. block_type:=bt_var;
  1235. { Process procvar directives }
  1236. if maybe_parse_proc_directives(hdef) then
  1237. semicoloneaten:=true;
  1238. { check for absolute }
  1239. if try_to_consume(_ABSOLUTE) then
  1240. begin
  1241. read_absolute(sc);
  1242. allowdefaultvalue:=false;
  1243. end;
  1244. { Check for EXTERNAL etc directives before a semicolon }
  1245. if (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) then
  1246. begin
  1247. read_public_and_external_sc(sc);
  1248. allowdefaultvalue:=false;
  1249. semicoloneaten:=true;
  1250. end;
  1251. { try to parse the hint directives }
  1252. hintsymoptions:=[];
  1253. deprecatedmsg:=nil;
  1254. try_consume_hintdirective(hintsymoptions,deprecatedmsg);
  1255. for i:=0 to sc.count-1 do
  1256. begin
  1257. vs:=tabstractvarsym(sc[i]);
  1258. vs.symoptions := vs.symoptions + hintsymoptions;
  1259. if deprecatedmsg<>nil then
  1260. vs.deprecatedmsg:=stringdup(deprecatedmsg^);
  1261. end;
  1262. stringdispose(deprecatedmsg);
  1263. { Handling of Delphi typed const = initialized vars }
  1264. if allowdefaultvalue and
  1265. (token=_EQ) and
  1266. not(m_tp7 in current_settings.modeswitches) and
  1267. (symtablestack.top.symtabletype<>parasymtable) then
  1268. begin
  1269. { Add calling convention for procvar }
  1270. if (hdef.typ=procvardef) and
  1271. (hdef.typesym=nil) then
  1272. handle_calling_convention(tprocvardef(hdef));
  1273. read_default_value(sc);
  1274. hasdefaultvalue:=true;
  1275. end
  1276. else
  1277. begin
  1278. if not(semicoloneaten) then
  1279. consume(_SEMICOLON);
  1280. end;
  1281. { Support calling convention for procvars after semicolon }
  1282. if not(hasdefaultvalue) and
  1283. (hdef.typ=procvardef) and
  1284. (hdef.typesym=nil) then
  1285. begin
  1286. { Parse procvar directives after ; }
  1287. maybe_parse_proc_directives(hdef);
  1288. { Add calling convention for procvar }
  1289. handle_calling_convention(tprocvardef(hdef));
  1290. { Handling of Delphi typed const = initialized vars }
  1291. if (token=_EQ) and
  1292. not(m_tp7 in current_settings.modeswitches) and
  1293. (symtablestack.top.symtabletype<>parasymtable) then
  1294. begin
  1295. read_default_value(sc);
  1296. hasdefaultvalue:=true;
  1297. end;
  1298. end;
  1299. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1300. if (
  1301. (
  1302. (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) and
  1303. (m_cvar_support in current_settings.modeswitches)
  1304. ) or
  1305. (
  1306. (m_mac in current_settings.modeswitches) and
  1307. (
  1308. (cs_external_var in current_settings.localswitches) or
  1309. (cs_externally_visible in current_settings.localswitches)
  1310. )
  1311. )
  1312. ) then
  1313. read_public_and_external_sc(sc);
  1314. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1315. for i:=0 to sc.count-1 do
  1316. begin
  1317. vs:=tabstractvarsym(sc[i]);
  1318. if (vs.typ=staticvarsym) and
  1319. not(vo_is_typed_const in vs.varoptions) and
  1320. not(vo_is_external in vs.varoptions) then
  1321. insertbssdata(tstaticvarsym(vs));
  1322. end;
  1323. end;
  1324. block_type:=old_block_type;
  1325. { free the list }
  1326. sc.free;
  1327. end;
  1328. procedure read_record_fields(options:Tvar_dec_options);
  1329. var
  1330. sc : TFPObjectList;
  1331. i : longint;
  1332. hs,sorg,static_name : string;
  1333. hdef,casetype : tdef;
  1334. { maxsize contains the max. size of a variant }
  1335. { startvarrec contains the start of the variant part of a record }
  1336. maxsize, startvarrecsize : longint;
  1337. usedalign,
  1338. maxalignment,startvarrecalign,
  1339. maxpadalign, startpadalign: shortint;
  1340. pt : tnode;
  1341. fieldvs : tfieldvarsym;
  1342. hstaticvs : tstaticvarsym;
  1343. vs : tabstractvarsym;
  1344. srsym : tsym;
  1345. srsymtable : TSymtable;
  1346. visibility : tvisibility;
  1347. recst : tabstractrecordsymtable;
  1348. recstlist : tfpobjectlist;
  1349. unionsymtable : trecordsymtable;
  1350. offset : longint;
  1351. uniondef : trecorddef;
  1352. hintsymoptions : tsymoptions;
  1353. deprecatedmsg : pshortstring;
  1354. semicoloneaten: boolean;
  1355. {$if defined(powerpc) or defined(powerpc64)}
  1356. tempdef: tdef;
  1357. is_first_type: boolean;
  1358. {$endif powerpc or powerpc64}
  1359. sl: tpropaccesslist;
  1360. old_block_type: tblock_type;
  1361. begin
  1362. old_block_type:=block_type;
  1363. block_type:=bt_var;
  1364. recst:=tabstractrecordsymtable(symtablestack.top);
  1365. {$if defined(powerpc) or defined(powerpc64)}
  1366. is_first_type:=true;
  1367. {$endif powerpc or powerpc64}
  1368. { Force an expected ID error message }
  1369. if not (token in [_ID,_CASE,_END]) then
  1370. consume(_ID);
  1371. { read vars }
  1372. sc:=TFPObjectList.create(false);
  1373. recstlist:=TFPObjectList.create(false);;
  1374. while (token=_ID) and
  1375. not(((vd_object in options) or
  1376. ((vd_record in options) and (m_advanced_records in current_settings.modeswitches))) and
  1377. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1378. begin
  1379. visibility:=symtablestack.top.currentvisibility;
  1380. semicoloneaten:=false;
  1381. sc.clear;
  1382. repeat
  1383. sorg:=orgpattern;
  1384. if token=_ID then
  1385. begin
  1386. vs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1387. sc.add(vs);
  1388. recst.insert(vs);
  1389. end;
  1390. consume(_ID);
  1391. until not try_to_consume(_COMMA);
  1392. if m_delphi in current_settings.modeswitches then
  1393. block_type:=bt_var_type
  1394. else
  1395. block_type:=old_block_type;
  1396. consume(_COLON);
  1397. { Don't search for types where they can't be:
  1398. types can be only in objects, classes and records.
  1399. This just speedup the search a bit. }
  1400. recstlist.count:=0;
  1401. if not is_class_or_object(tdef(recst.defowner)) and
  1402. not is_record(tdef(recst.defowner)) then
  1403. begin
  1404. recstlist.add(recst);
  1405. symtablestack.pop(recst);
  1406. end;
  1407. read_anon_type(hdef,false);
  1408. block_type:=bt_var;
  1409. { allow only static fields reference to struct where they are declared }
  1410. if not (vd_class in options) and
  1411. (is_object(hdef) or is_record(hdef)) and
  1412. is_owned_by(tabstractrecorddef(recst.defowner),tabstractrecorddef(hdef)) then
  1413. begin
  1414. Message1(type_e_type_is_not_completly_defined, tabstractrecorddef(hdef).RttiName);
  1415. { for error recovery or compiler will crash later }
  1416. hdef:=generrordef;
  1417. end;
  1418. { restore stack }
  1419. for i:=recstlist.count-1 downto 0 do
  1420. begin
  1421. recst:=tabstractrecordsymtable(recstlist[i]);
  1422. symtablestack.push(recst);
  1423. end;
  1424. { Process procvar directives }
  1425. if maybe_parse_proc_directives(hdef) then
  1426. semicoloneaten:=true;
  1427. {$if defined(powerpc) or defined(powerpc64)}
  1428. { from gcc/gcc/config/rs6000/rs6000.h:
  1429. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1430. /* Return the alignment of a struct based on the Macintosh PowerPC
  1431. alignment rules. In general the alignment of a struct is
  1432. determined by the greatest alignment of its elements. However, the
  1433. PowerPC rules cause the alignment of a struct to peg at word
  1434. alignment except when the first field has greater than word
  1435. (32-bit) alignment, in which case the alignment is determined by
  1436. the alignment of the first field. */
  1437. }
  1438. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1439. is_first_type and
  1440. (symtablestack.top.symtabletype=recordsymtable) and
  1441. (trecordsymtable(symtablestack.top).usefieldalignment=C_alignment) then
  1442. begin
  1443. tempdef:=hdef;
  1444. while tempdef.typ=arraydef do
  1445. tempdef:=tarraydef(tempdef).elementdef;
  1446. if tempdef.typ<>recorddef then
  1447. maxpadalign:=tempdef.alignment
  1448. else
  1449. maxpadalign:=trecorddef(tempdef).padalignment;
  1450. if (maxpadalign>4) and
  1451. (maxpadalign>trecordsymtable(symtablestack.top).padalignment) then
  1452. trecordsymtable(symtablestack.top).padalignment:=maxpadalign;
  1453. is_first_type:=false;
  1454. end;
  1455. {$endif powerpc or powerpc64}
  1456. { types that use init/final are not allowed in variant parts, but
  1457. classes are allowed }
  1458. if (variantrecordlevel>0) then
  1459. if is_managed_type(hdef) then
  1460. Message(parser_e_cant_use_inittable_here)
  1461. else
  1462. if hdef.typ=undefineddef then
  1463. Message(parser_e_cant_use_type_parameters_here);
  1464. { try to parse the hint directives }
  1465. hintsymoptions:=[];
  1466. deprecatedmsg:=nil;
  1467. try_consume_hintdirective(hintsymoptions,deprecatedmsg);
  1468. { update variable type and hints }
  1469. for i:=0 to sc.count-1 do
  1470. begin
  1471. fieldvs:=tfieldvarsym(sc[i]);
  1472. fieldvs.vardef:=hdef;
  1473. { insert any additional hint directives }
  1474. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1475. if deprecatedmsg<>nil then
  1476. fieldvs.deprecatedmsg:=stringdup(deprecatedmsg^);
  1477. end;
  1478. stringdispose(deprecatedmsg);
  1479. { Records and objects can't have default values }
  1480. { for a record there doesn't need to be a ; before the END or ) }
  1481. if not(token in [_END,_RKLAMMER]) and
  1482. not(semicoloneaten) then
  1483. consume(_SEMICOLON);
  1484. { Parse procvar directives after ; }
  1485. maybe_parse_proc_directives(hdef);
  1486. { Add calling convention for procvar }
  1487. if (hdef.typ=procvardef) and
  1488. (hdef.typesym=nil) then
  1489. handle_calling_convention(tprocvardef(hdef));
  1490. { check if it is a class field }
  1491. if (vd_object in options) then
  1492. begin
  1493. { if it is not a class var section and token=STATIC then it is a class field too }
  1494. if not (vd_class in options) and try_to_consume(_STATIC) then
  1495. begin
  1496. consume(_SEMICOLON);
  1497. include(options, vd_class);
  1498. end;
  1499. end;
  1500. if vd_class in options then
  1501. begin
  1502. { add static flag and staticvarsyms }
  1503. for i:=0 to sc.count-1 do
  1504. begin
  1505. fieldvs:=tfieldvarsym(sc[i]);
  1506. include(fieldvs.symoptions,sp_static);
  1507. { generate the symbol which reserves the space }
  1508. static_name:=lower(generate_nested_name(recst,'_'))+'_'+fieldvs.name;
  1509. hstaticvs:=tstaticvarsym.create('$_static_'+static_name,vs_value,hdef,[]);
  1510. include(hstaticvs.symoptions,sp_internal);
  1511. recst.get_unit_symtable.insert(hstaticvs);
  1512. insertbssdata(hstaticvs);
  1513. { generate the symbol for the access }
  1514. sl:=tpropaccesslist.create;
  1515. sl.addsym(sl_load,hstaticvs);
  1516. recst.insert(tabsolutevarsym.create_ref('$'+static_name,hdef,sl));
  1517. end;
  1518. end;
  1519. if (visibility=vis_published) and
  1520. not(is_class(hdef)) then
  1521. begin
  1522. Message(parser_e_cant_publish_that);
  1523. visibility:=vis_public;
  1524. end;
  1525. if (visibility=vis_published) and
  1526. not(oo_can_have_published in tobjectdef(hdef).objectoptions) and
  1527. not(m_delphi in current_settings.modeswitches) then
  1528. begin
  1529. Message(parser_e_only_publishable_classes_can_be_published);
  1530. visibility:=vis_public;
  1531. end;
  1532. { Generate field in the recordsymtable }
  1533. for i:=0 to sc.count-1 do
  1534. begin
  1535. fieldvs:=tfieldvarsym(sc[i]);
  1536. { static data fields are already inserted in the globalsymtable }
  1537. if not(sp_static in fieldvs.symoptions) then
  1538. recst.addfield(fieldvs,visibility);
  1539. end;
  1540. end;
  1541. recstlist.free;
  1542. if m_delphi in current_settings.modeswitches then
  1543. block_type:=bt_var_type
  1544. else
  1545. block_type:=old_block_type;
  1546. { Check for Case }
  1547. if (vd_record in options) and
  1548. try_to_consume(_CASE) then
  1549. begin
  1550. maxsize:=0;
  1551. maxalignment:=0;
  1552. maxpadalign:=0;
  1553. { including a field declaration? }
  1554. fieldvs:=nil;
  1555. sorg:=orgpattern;
  1556. hs:=pattern;
  1557. searchsym(hs,srsym,srsymtable);
  1558. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1559. begin
  1560. consume(_ID);
  1561. consume(_COLON);
  1562. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1563. symtablestack.top.insert(fieldvs);
  1564. end;
  1565. read_anon_type(casetype,true);
  1566. block_type:=bt_var;
  1567. if assigned(fieldvs) then
  1568. begin
  1569. fieldvs.vardef:=casetype;
  1570. recst.addfield(fieldvs,recst.currentvisibility);
  1571. end;
  1572. if not(is_ordinal(casetype))
  1573. {$ifndef cpu64bitaddr}
  1574. or is_64bitint(casetype)
  1575. {$endif cpu64bitaddr}
  1576. then
  1577. Message(type_e_ordinal_expr_expected);
  1578. consume(_OF);
  1579. UnionSymtable:=trecordsymtable.create('',current_settings.packrecords);
  1580. UnionDef:=trecorddef.create('',unionsymtable);
  1581. uniondef.isunion:=true;
  1582. startvarrecsize:=UnionSymtable.datasize;
  1583. { align the bitpacking to the next byte }
  1584. UnionSymtable.datasize:=startvarrecsize;
  1585. startvarrecalign:=UnionSymtable.fieldalignment;
  1586. startpadalign:=Unionsymtable.padalignment;
  1587. symtablestack.push(UnionSymtable);
  1588. repeat
  1589. repeat
  1590. pt:=comp_expr(true,false);
  1591. if not(pt.nodetype=ordconstn) then
  1592. Message(parser_e_illegal_expression);
  1593. if try_to_consume(_POINTPOINT) then
  1594. pt:=crangenode.create(pt,comp_expr(true,false));
  1595. pt.free;
  1596. if token=_COMMA then
  1597. consume(_COMMA)
  1598. else
  1599. break;
  1600. until false;
  1601. if m_delphi in current_settings.modeswitches then
  1602. block_type:=bt_var_type
  1603. else
  1604. block_type:=old_block_type;
  1605. consume(_COLON);
  1606. { read the vars }
  1607. consume(_LKLAMMER);
  1608. inc(variantrecordlevel);
  1609. if token<>_RKLAMMER then
  1610. read_record_fields([vd_record]);
  1611. dec(variantrecordlevel);
  1612. consume(_RKLAMMER);
  1613. { calculates maximal variant size }
  1614. maxsize:=max(maxsize,unionsymtable.datasize);
  1615. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1616. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1617. { the items of the next variant are overlayed }
  1618. unionsymtable.datasize:=startvarrecsize;
  1619. unionsymtable.fieldalignment:=startvarrecalign;
  1620. unionsymtable.padalignment:=startpadalign;
  1621. if (token<>_END) and (token<>_RKLAMMER) then
  1622. consume(_SEMICOLON)
  1623. else
  1624. break;
  1625. until (token=_END) or (token=_RKLAMMER);
  1626. symtablestack.pop(UnionSymtable);
  1627. { at last set the record size to that of the biggest variant }
  1628. unionsymtable.datasize:=maxsize;
  1629. unionsymtable.fieldalignment:=maxalignment;
  1630. unionsymtable.addalignmentpadding;
  1631. {$if defined(powerpc) or defined(powerpc64)}
  1632. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1633. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1634. is_first_type and
  1635. (recst.usefieldalignment=C_alignment) and
  1636. (maxpadalign>recst.padalignment) then
  1637. recst.padalignment:=maxpadalign;
  1638. {$endif powerpc or powerpc64}
  1639. { Align the offset where the union symtable is added }
  1640. case recst.usefieldalignment of
  1641. { allow the unionsymtable to be aligned however it wants }
  1642. { (within the global min/max limits) }
  1643. 0, { default }
  1644. C_alignment:
  1645. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  1646. { 1 byte alignment if we are bitpacked }
  1647. bit_alignment:
  1648. usedalign:=1;
  1649. mac68k_alignment:
  1650. usedalign:=2;
  1651. { otherwise alignment at the packrecords alignment of the }
  1652. { current record }
  1653. else
  1654. usedalign:=used_align(recst.fieldalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1655. end;
  1656. offset:=align(recst.datasize,usedalign);
  1657. recst.datasize:=offset+unionsymtable.datasize;
  1658. if unionsymtable.recordalignment>recst.fieldalignment then
  1659. recst.fieldalignment:=unionsymtable.recordalignment;
  1660. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1661. uniondef.owner.deletedef(uniondef);
  1662. end;
  1663. { free the list }
  1664. sc.free;
  1665. {$ifdef powerpc}
  1666. is_first_type := false;
  1667. {$endif powerpc}
  1668. block_type:=old_block_type;
  1669. end;
  1670. end.