pdecvar.pas 58 KB

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