pdecvar.pas 57 KB

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