pdecvar.pas 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Parses variable declarations. Used for var statement and record
  4. definitions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecvar;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symsym,symdef;
  23. type
  24. tvar_dec_option=(vd_record,vd_object,vd_threadvar);
  25. tvar_dec_options=set of tvar_dec_option;
  26. function read_property_dec(aclass:tobjectdef):tpropertysym;
  27. procedure read_var_decls(options:Tvar_dec_options);
  28. procedure read_record_fields(options:Tvar_dec_options);
  29. procedure read_public_and_external(vs: tabstractvarsym);
  30. implementation
  31. uses
  32. SysUtils,
  33. { common }
  34. cutils,cclasses,
  35. { global }
  36. globtype,globals,tokens,verbose,constexp,
  37. systems,
  38. { symtable }
  39. symconst,symbase,symtype,symtable,defutil,defcmp,
  40. fmodule,htypechk,
  41. { pass 1 }
  42. node,pass_1,aasmdata,
  43. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,nutils,
  44. { codegen }
  45. ncgutil,
  46. { parser }
  47. scanner,
  48. pbase,pexpr,ptype,ptconst,pdecsub,
  49. { link }
  50. import
  51. ;
  52. function read_property_dec(aclass:tobjectdef):tpropertysym;
  53. { convert a node tree to symlist and return the last
  54. symbol }
  55. function parse_symlist(pl:tpropaccesslist;var def:tdef):boolean;
  56. var
  57. idx : longint;
  58. sym : tsym;
  59. srsymtable : TSymtable;
  60. st : TSymtable;
  61. p : tnode;
  62. begin
  63. result:=true;
  64. def:=nil;
  65. if token=_ID then
  66. begin
  67. if assigned(aclass) then
  68. sym:=search_class_member(aclass,pattern)
  69. else
  70. searchsym(pattern,sym,srsymtable);
  71. if assigned(sym) then
  72. begin
  73. 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 cpu64bitaddr}
  326. and (not is_64bitint(pt.resultdef))
  327. {$endif cpu64bitaddr}
  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 cpu64bitaddr}
  546. is_64bitint(p.propdef) or
  547. {$endif cpu64bitaddr}
  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. single_type(def,false);
  602. if compare_defs(def,p.propdef,nothingn)<te_equal then
  603. begin
  604. message2(parser_e_implements_must_have_correct_type,def.GetTypeName,p.propdef.GetTypeName);
  605. exit;
  606. end;
  607. if not is_class_or_interface(def) then
  608. begin
  609. message(parser_e_implements_must_be_class_or_interface);
  610. exit;
  611. end;
  612. if not assigned(p.propaccesslist[palt_read].firstsym) then
  613. begin
  614. message(parser_e_implements_must_read_specifier);
  615. exit;
  616. end;
  617. if assigned(p.propaccesslist[palt_write].firstsym) then
  618. begin
  619. message(parser_e_implements_must_not_have_write_specifier);
  620. exit;
  621. end;
  622. if assigned(p.propaccesslist[palt_stored].firstsym) then
  623. begin
  624. message(parser_e_implements_must_not_have_stored_specifier);
  625. exit;
  626. end;
  627. found:=false;
  628. for i:=0 to aclass.ImplementedInterfaces.Count-1 do
  629. begin
  630. ImplIntf:=TImplementedInterface(aclass.ImplementedInterfaces[i]);
  631. if compare_defs(def,ImplIntf.IntfDef,nothingn)>=te_equal then
  632. begin
  633. found:=true;
  634. break;
  635. end;
  636. end;
  637. if found then
  638. begin
  639. ImplIntf.ImplementsGetter:=p;
  640. case p.propaccesslist[palt_read].firstsym^.sym.typ of
  641. procsym :
  642. begin
  643. if (po_virtualmethod in tprocdef(p.propaccesslist[palt_read].procdef).procoptions) then
  644. ImplIntf.IType:=etVirtualMethodResult
  645. else
  646. ImplIntf.IType:=etStaticMethodResult;
  647. end;
  648. fieldvarsym :
  649. ImplIntf.IType:=etFieldValue;
  650. else
  651. internalerror(200802161);
  652. end;
  653. end
  654. else
  655. message1(parser_e_implements_uses_non_implemented_interface,def.GetTypeName);
  656. end;
  657. { remove temporary procvardefs }
  658. readprocdef.owner.deletedef(readprocdef);
  659. writeprocdef.owner.deletedef(writeprocdef);
  660. result:=p;
  661. end;
  662. function maybe_parse_proc_directives(def:tdef):boolean;
  663. var
  664. newtype : ttypesym;
  665. begin
  666. result:=false;
  667. { Process procvar directives before = and ; }
  668. if (def.typ=procvardef) and
  669. (def.typesym=nil) and
  670. check_proc_directive(true) then
  671. begin
  672. newtype:=ttypesym.create('unnamed',def);
  673. parse_var_proc_directives(tsym(newtype));
  674. newtype.typedef:=nil;
  675. def.typesym:=nil;
  676. newtype.free;
  677. result:=true;
  678. end;
  679. end;
  680. const
  681. variantrecordlevel : longint = 0;
  682. procedure read_public_and_external_sc(sc:TFPObjectList);
  683. var
  684. vs: tabstractvarsym;
  685. begin
  686. { only allowed for one var }
  687. vs:=tabstractvarsym(sc[0]);
  688. if sc.count>1 then
  689. Message(parser_e_absolute_only_one_var);
  690. read_public_and_external(vs);
  691. end;
  692. procedure read_public_and_external(vs: tabstractvarsym);
  693. var
  694. is_dll,
  695. is_cdecl,
  696. is_external_var,
  697. is_public_var : boolean;
  698. dll_name,
  699. C_name : string;
  700. begin
  701. { only allowed for one var }
  702. { only allow external and public on global symbols }
  703. if vs.typ<>staticvarsym then
  704. begin
  705. Message(parser_e_no_local_var_external);
  706. exit;
  707. end;
  708. { defaults }
  709. is_dll:=false;
  710. is_cdecl:=false;
  711. is_external_var:=false;
  712. is_public_var:=false;
  713. C_name:=vs.realname;
  714. { macpas specific handling due to some switches}
  715. if (m_mac in current_settings.modeswitches) then
  716. begin
  717. if (cs_external_var in current_settings.localswitches) then
  718. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  719. is_cdecl:=true;
  720. is_external_var:=true;
  721. end
  722. else if (cs_externally_visible in current_settings.localswitches) then
  723. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  724. is_cdecl:=true;
  725. is_public_var:=true;
  726. end;
  727. end;
  728. { cdecl }
  729. if try_to_consume(_CVAR) then
  730. begin
  731. consume(_SEMICOLON);
  732. is_cdecl:=true;
  733. end;
  734. { external }
  735. if try_to_consume(_EXTERNAL) then
  736. begin
  737. is_external_var:=true;
  738. if not is_cdecl then
  739. begin
  740. if idtoken<>_NAME then
  741. begin
  742. is_dll:=true;
  743. dll_name:=get_stringconst;
  744. if ExtractFileExt(dll_name)='' then
  745. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  746. end;
  747. if try_to_consume(_NAME) then
  748. C_name:=get_stringconst;
  749. end;
  750. consume(_SEMICOLON);
  751. end;
  752. { export or public }
  753. if idtoken in [_EXPORT,_PUBLIC] then
  754. begin
  755. consume(_ID);
  756. if is_external_var then
  757. Message(parser_e_not_external_and_export)
  758. else
  759. is_public_var:=true;
  760. if try_to_consume(_NAME) then
  761. C_name:=get_stringconst;
  762. consume(_SEMICOLON);
  763. end;
  764. { Windows uses an indirect reference using import tables }
  765. if is_dll and
  766. (target_info.system in system_all_windows) then
  767. include(vs.varoptions,vo_is_dll_var);
  768. { Add C _ prefix }
  769. if is_cdecl or
  770. (
  771. is_dll and
  772. (target_info.system in systems_darwin)
  773. ) then
  774. C_Name := target_info.Cprefix+C_Name;
  775. if is_public_var then
  776. begin
  777. include(vs.varoptions,vo_is_public);
  778. vs.varregable := vr_none;
  779. { mark as referenced }
  780. inc(vs.refs);
  781. end;
  782. { now we can insert it in the import lib if its a dll, or
  783. add it to the externals }
  784. if is_external_var then
  785. begin
  786. if vo_is_typed_const in vs.varoptions then
  787. Message(parser_e_initialized_not_for_external);
  788. include(vs.varoptions,vo_is_external);
  789. vs.varregable := vr_none;
  790. if is_dll then
  791. current_module.AddExternalImport(dll_name,C_Name,0,true,false)
  792. else
  793. if tf_has_dllscanner in target_info.flags then
  794. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  795. end;
  796. { Set the assembler name }
  797. tstaticvarsym(vs).set_mangledname(C_Name);
  798. end;
  799. procedure read_var_decls(options:Tvar_dec_options);
  800. procedure read_default_value(sc : TFPObjectList);
  801. var
  802. vs : tabstractnormalvarsym;
  803. tcsym : tstaticvarsym;
  804. begin
  805. vs:=tabstractnormalvarsym(sc[0]);
  806. if sc.count>1 then
  807. Message(parser_e_initialized_only_one_var);
  808. if vo_is_thread_var in vs.varoptions then
  809. Message(parser_e_initialized_not_for_threadvar);
  810. consume(_EQUAL);
  811. case vs.typ of
  812. localvarsym :
  813. begin
  814. tcsym:=tstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  815. include(tcsym.symoptions,sp_internal);
  816. vs.defaultconstsym:=tcsym;
  817. symtablestack.top.insert(tcsym);
  818. read_typed_const(current_asmdata.asmlists[al_typedconsts],tcsym);
  819. end;
  820. staticvarsym :
  821. begin
  822. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs));
  823. end;
  824. else
  825. internalerror(200611051);
  826. end;
  827. vs.varstate:=vs_initialised;
  828. end;
  829. {$ifdef gpc_mode}
  830. procedure read_gpc_name(sc : TFPObjectList);
  831. var
  832. vs : tabstractnormalvarsym;
  833. C_Name : string;
  834. begin
  835. consume(_ID);
  836. C_Name:=get_stringconst;
  837. vs:=tabstractnormalvarsym(sc[0]);
  838. if sc.count>1 then
  839. Message(parser_e_absolute_only_one_var);
  840. if vs.typ=staticvarsym then
  841. begin
  842. tstaticvarsym(vs).set_mangledname(C_Name);
  843. include(vs.varoptions,vo_is_external);
  844. end
  845. else
  846. Message(parser_e_no_local_var_external);
  847. end;
  848. {$endif}
  849. procedure read_absolute(sc : TFPObjectList);
  850. var
  851. vs : tabstractvarsym;
  852. abssym : tabsolutevarsym;
  853. pt,hp : tnode;
  854. st : tsymtable;
  855. begin
  856. abssym:=nil;
  857. { only allowed for one var }
  858. vs:=tabstractvarsym(sc[0]);
  859. if sc.count>1 then
  860. Message(parser_e_absolute_only_one_var);
  861. if vo_is_typed_const in vs.varoptions then
  862. Message(parser_e_initialized_not_for_external);
  863. { parse the rest }
  864. pt:=expr;
  865. { check allowed absolute types }
  866. if (pt.nodetype=stringconstn) or
  867. (is_constcharnode(pt)) then
  868. begin
  869. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  870. abssym.fileinfo:=vs.fileinfo;
  871. if pt.nodetype=stringconstn then
  872. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  873. else
  874. abssym.asmname:=stringdup(chr(tordconstnode(pt).value.svalue));
  875. consume(token);
  876. abssym.abstyp:=toasm;
  877. end
  878. { address }
  879. else if is_constintnode(pt) then
  880. begin
  881. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  882. abssym.fileinfo:=vs.fileinfo;
  883. abssym.abstyp:=toaddr;
  884. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  885. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  886. message(parser_e_range_check_error)
  887. else
  888. abssym.addroffset:=Tordconstnode(pt).value.svalue;
  889. {$ifdef i386}
  890. abssym.absseg:=false;
  891. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  892. try_to_consume(_COLON) then
  893. begin
  894. pt.free;
  895. pt:=expr;
  896. if is_constintnode(pt) then
  897. begin
  898. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  899. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  900. message(parser_e_range_check_error)
  901. else
  902. abssym.addroffset:=abssym.addroffset shl 4+tordconstnode(pt).value.svalue;
  903. abssym.absseg:=true;
  904. end
  905. else
  906. Message(type_e_ordinal_expr_expected);
  907. end;
  908. {$endif i386}
  909. end
  910. { variable }
  911. else
  912. begin
  913. { remove subscriptn before checking for loadn }
  914. hp:=pt;
  915. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  916. hp:=tunarynode(hp).left;
  917. if (hp.nodetype=loadn) then
  918. begin
  919. { we should check the result type of loadn }
  920. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym]) then
  921. Message(parser_e_absolute_only_to_var_or_const);
  922. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  923. abssym.fileinfo:=vs.fileinfo;
  924. abssym.abstyp:=tovar;
  925. abssym.ref:=node_to_propaccesslist(pt);
  926. { if the sizes are different, can't be a regvar since you }
  927. { can't be "absolute upper 8 bits of a register" (except }
  928. { if its a record field of the same size of a record }
  929. { regvar, but in that case pt.resultdef.size will have }
  930. { the same size since it refers to the field and not to }
  931. { the whole record -- which is why we use pt and not hp) }
  932. { we can't take the size of an open array }
  933. if is_open_array(pt.resultdef) or
  934. (vs.vardef.size <> pt.resultdef.size) then
  935. make_not_regable(pt,[ra_addr_regable]);
  936. end
  937. else
  938. Message(parser_e_absolute_only_to_var_or_const);
  939. end;
  940. pt.free;
  941. { replace old varsym with the new absolutevarsym }
  942. if assigned(abssym) then
  943. begin
  944. st:=vs.owner;
  945. vs.owner.Delete(vs);
  946. st.insert(abssym);
  947. sc[0]:=abssym;
  948. end;
  949. end;
  950. var
  951. sc : TFPObjectList;
  952. vs : tabstractvarsym;
  953. hdef : tdef;
  954. i : longint;
  955. semicoloneaten,
  956. allowdefaultvalue,
  957. hasdefaultvalue : boolean;
  958. old_current_object_option : tsymoptions;
  959. hintsymoptions : tsymoptions;
  960. old_block_type : tblock_type;
  961. begin
  962. old_current_object_option:=current_object_option;
  963. { all variables are public if not in a object declaration }
  964. current_object_option:=[sp_public];
  965. old_block_type:=block_type;
  966. block_type:=bt_type;
  967. { Force an expected ID error message }
  968. if not (token in [_ID,_CASE,_END]) then
  969. consume(_ID);
  970. { read vars }
  971. sc:=TFPObjectList.create(false);
  972. while (token=_ID) do
  973. begin
  974. semicoloneaten:=false;
  975. hasdefaultvalue:=false;
  976. allowdefaultvalue:=true;
  977. sc.clear;
  978. repeat
  979. if (token = _ID) then
  980. begin
  981. case symtablestack.top.symtabletype of
  982. localsymtable :
  983. vs:=tlocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  984. staticsymtable,
  985. globalsymtable :
  986. begin
  987. vs:=tstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  988. if vd_threadvar in options then
  989. include(vs.varoptions,vo_is_thread_var);
  990. end;
  991. else
  992. internalerror(200411064);
  993. end;
  994. sc.add(vs);
  995. symtablestack.top.insert(vs);
  996. end;
  997. consume(_ID);
  998. until not try_to_consume(_COMMA);
  999. consume(_COLON);
  1000. {$ifdef gpc_mode}
  1001. if (m_gpc in current_settings.modeswitches) and
  1002. (token=_ID) and
  1003. (orgpattern='__asmname__') then
  1004. read_gpc_name(sc);
  1005. {$endif}
  1006. { read variable type def }
  1007. read_anon_type(hdef,false);
  1008. for i:=0 to sc.count-1 do
  1009. begin
  1010. vs:=tabstractvarsym(sc[i]);
  1011. vs.vardef:=hdef;
  1012. end;
  1013. { Process procvar directives }
  1014. if maybe_parse_proc_directives(hdef) then
  1015. semicoloneaten:=true;
  1016. { check for absolute }
  1017. if try_to_consume(_ABSOLUTE) then
  1018. begin
  1019. read_absolute(sc);
  1020. allowdefaultvalue:=false;
  1021. end;
  1022. { Check for EXTERNAL etc directives before a semicolon }
  1023. if (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  1024. begin
  1025. read_public_and_external_sc(sc);
  1026. allowdefaultvalue:=false;
  1027. semicoloneaten:=true;
  1028. end;
  1029. { try to parse the hint directives }
  1030. hintsymoptions:=[];
  1031. try_consume_hintdirective(hintsymoptions);
  1032. for i:=0 to sc.count-1 do
  1033. begin
  1034. vs:=tabstractvarsym(sc[i]);
  1035. vs.symoptions := vs.symoptions + hintsymoptions;
  1036. end;
  1037. { Handling of Delphi typed const = initialized vars }
  1038. if allowdefaultvalue and
  1039. (token=_EQUAL) and
  1040. not(m_tp7 in current_settings.modeswitches) and
  1041. (symtablestack.top.symtabletype<>parasymtable) then
  1042. begin
  1043. { Add calling convention for procvar }
  1044. if (hdef.typ=procvardef) and
  1045. (hdef.typesym=nil) then
  1046. handle_calling_convention(tprocvardef(hdef));
  1047. read_default_value(sc);
  1048. hasdefaultvalue:=true;
  1049. end
  1050. else
  1051. begin
  1052. if not(semicoloneaten) then
  1053. consume(_SEMICOLON);
  1054. end;
  1055. { Support calling convention for procvars after semicolon }
  1056. if not(hasdefaultvalue) and
  1057. (hdef.typ=procvardef) and
  1058. (hdef.typesym=nil) then
  1059. begin
  1060. { Parse procvar directives after ; }
  1061. maybe_parse_proc_directives(hdef);
  1062. { Add calling convention for procvar }
  1063. handle_calling_convention(tprocvardef(hdef));
  1064. { Handling of Delphi typed const = initialized vars }
  1065. if (token=_EQUAL) and
  1066. not(m_tp7 in current_settings.modeswitches) and
  1067. (symtablestack.top.symtabletype<>parasymtable) then
  1068. begin
  1069. read_default_value(sc);
  1070. hasdefaultvalue:=true;
  1071. end;
  1072. end;
  1073. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1074. if (
  1075. (
  1076. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) and
  1077. (m_cvar_support in current_settings.modeswitches)
  1078. ) or
  1079. (
  1080. (m_mac in current_settings.modeswitches) and
  1081. (
  1082. (cs_external_var in current_settings.localswitches) or
  1083. (cs_externally_visible in current_settings.localswitches)
  1084. )
  1085. )
  1086. ) then
  1087. read_public_and_external_sc(sc);
  1088. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1089. for i:=0 to sc.count-1 do
  1090. begin
  1091. vs:=tabstractvarsym(sc[i]);
  1092. if (vs.typ=staticvarsym) and
  1093. not(vo_is_typed_const in vs.varoptions) and
  1094. not(vo_is_external in vs.varoptions) then
  1095. insertbssdata(tstaticvarsym(vs));
  1096. end;
  1097. end;
  1098. block_type:=old_block_type;
  1099. current_object_option:=old_current_object_option;
  1100. { free the list }
  1101. sc.free;
  1102. end;
  1103. procedure read_record_fields(options:Tvar_dec_options);
  1104. var
  1105. sc : TFPObjectList;
  1106. i : longint;
  1107. old_block_type : tblock_type;
  1108. old_current_object_option : tsymoptions;
  1109. hs,sorg : string;
  1110. hdef,casetype : tdef;
  1111. { maxsize contains the max. size of a variant }
  1112. { startvarrec contains the start of the variant part of a record }
  1113. maxsize, startvarrecsize : longint;
  1114. usedalign,
  1115. maxalignment,startvarrecalign,
  1116. maxpadalign, startpadalign: shortint;
  1117. pt : tnode;
  1118. fieldvs : tfieldvarsym;
  1119. hstaticvs : tstaticvarsym;
  1120. vs : tabstractvarsym;
  1121. srsym : tsym;
  1122. srsymtable : TSymtable;
  1123. recst : tabstractrecordsymtable;
  1124. unionsymtable : trecordsymtable;
  1125. offset : longint;
  1126. uniondef : trecorddef;
  1127. hintsymoptions : tsymoptions;
  1128. semicoloneaten: boolean;
  1129. {$if defined(powerpc) or defined(powerpc64)}
  1130. tempdef: tdef;
  1131. is_first_field: boolean;
  1132. {$endif powerpc or powerpc64}
  1133. begin
  1134. recst:=tabstractrecordsymtable(symtablestack.top);
  1135. {$if defined(powerpc) or defined(powerpc64)}
  1136. is_first_field := true;
  1137. {$endif powerpc or powerpc64}
  1138. old_current_object_option:=current_object_option;
  1139. { all variables are public if not in a object declaration }
  1140. if not(vd_object in options) then
  1141. current_object_option:=[sp_public];
  1142. old_block_type:=block_type;
  1143. block_type:=bt_type;
  1144. { Force an expected ID error message }
  1145. if not (token in [_ID,_CASE,_END]) then
  1146. consume(_ID);
  1147. { read vars }
  1148. sc:=TFPObjectList.create(false);
  1149. while (token=_ID) and
  1150. not((vd_object in options) and
  1151. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1152. begin
  1153. semicoloneaten:=false;
  1154. sc.clear;
  1155. repeat
  1156. sorg:=orgpattern;
  1157. if token=_ID then
  1158. begin
  1159. vs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1160. sc.add(vs);
  1161. recst.insert(vs);
  1162. end;
  1163. consume(_ID);
  1164. until not try_to_consume(_COMMA);
  1165. consume(_COLON);
  1166. { Don't search in the recordsymtable for types }
  1167. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1168. symtablestack.pop(recst);
  1169. read_anon_type(hdef,false);
  1170. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1171. symtablestack.push(recst);
  1172. { Process procvar directives }
  1173. if maybe_parse_proc_directives(hdef) then
  1174. semicoloneaten:=true;
  1175. {$if defined(powerpc) or defined(powerpc64)}
  1176. { from gcc/gcc/config/rs6000/rs6000.h:
  1177. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1178. /* Return the alignment of a struct based on the Macintosh PowerPC
  1179. alignment rules. In general the alignment of a struct is
  1180. determined by the greatest alignment of its elements. However, the
  1181. PowerPC rules cause the alignment of a struct to peg at word
  1182. alignment except when the first field has greater than word
  1183. (32-bit) alignment, in which case the alignment is determined by
  1184. the alignment of the first field. */
  1185. }
  1186. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1187. is_first_field and
  1188. (symtablestack.top.symtabletype=recordsymtable) and
  1189. (trecordsymtable(symtablestack.top).usefieldalignment=C_alignment) then
  1190. begin
  1191. tempdef:=hdef;
  1192. while tempdef.typ=arraydef do
  1193. tempdef:=tarraydef(tempdef).elementdef;
  1194. if tempdef.typ<>recorddef then
  1195. maxpadalign:=tempdef.alignment
  1196. else
  1197. maxpadalign:=trecorddef(tempdef).padalignment;
  1198. if (maxpadalign>4) and
  1199. (maxpadalign>trecordsymtable(symtablestack.top).padalignment) then
  1200. trecordsymtable(symtablestack.top).padalignment:=maxpadalign;
  1201. is_first_field:=false;
  1202. end;
  1203. {$endif powerpc or powerpc64}
  1204. { types that use init/final are not allowed in variant parts, but
  1205. classes are allowed }
  1206. if (variantrecordlevel>0) and
  1207. (hdef.needs_inittable and not is_class(hdef)) then
  1208. Message(parser_e_cant_use_inittable_here);
  1209. { try to parse the hint directives }
  1210. hintsymoptions:=[];
  1211. try_consume_hintdirective(hintsymoptions);
  1212. { update variable type and hints }
  1213. for i:=0 to sc.count-1 do
  1214. begin
  1215. fieldvs:=tfieldvarsym(sc[i]);
  1216. fieldvs.vardef:=hdef;
  1217. { insert any additional hint directives }
  1218. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1219. end;
  1220. { Records and objects can't have default values }
  1221. { for a record there doesn't need to be a ; before the END or ) }
  1222. if not(token in [_END,_RKLAMMER]) and
  1223. not(semicoloneaten) then
  1224. consume(_SEMICOLON);
  1225. { Parse procvar directives after ; }
  1226. maybe_parse_proc_directives(hdef);
  1227. { Add calling convention for procvar }
  1228. if (hdef.typ=procvardef) and
  1229. (hdef.typesym=nil) then
  1230. handle_calling_convention(tprocvardef(hdef));
  1231. { Check for STATIC directive }
  1232. if (vd_object in options) and
  1233. (cs_static_keyword in current_settings.moduleswitches) and
  1234. (try_to_consume(_STATIC)) then
  1235. begin
  1236. { add static flag and staticvarsyms }
  1237. for i:=0 to sc.count-1 do
  1238. begin
  1239. fieldvs:=tfieldvarsym(sc[i]);
  1240. include(fieldvs.symoptions,sp_static);
  1241. hstaticvs:=tstaticvarsym.create('$'+lower(symtablestack.top.name^)+'_'+fieldvs.name,vs_value,hdef,[]);
  1242. recst.defowner.owner.insert(hstaticvs);
  1243. insertbssdata(hstaticvs);
  1244. end;
  1245. consume(_SEMICOLON);
  1246. end;
  1247. if (sp_published in current_object_option) and
  1248. not(is_class(hdef)) then
  1249. begin
  1250. Message(parser_e_cant_publish_that);
  1251. exclude(current_object_option,sp_published);
  1252. { recover by changing access type to public }
  1253. for i:=0 to sc.count-1 do
  1254. begin
  1255. fieldvs:=tfieldvarsym(sc[i]);
  1256. exclude(fieldvs.symoptions,sp_published);
  1257. include(fieldvs.symoptions,sp_public);
  1258. end;
  1259. end
  1260. else
  1261. if (sp_published in current_object_option) and
  1262. not(oo_can_have_published in tobjectdef(hdef).objectoptions) and
  1263. not(m_delphi in current_settings.modeswitches) then
  1264. begin
  1265. Message(parser_e_only_publishable_classes_can_be_published);
  1266. exclude(current_object_option,sp_published);
  1267. end;
  1268. { Generate field in the recordsymtable }
  1269. for i:=0 to sc.count-1 do
  1270. begin
  1271. fieldvs:=tfieldvarsym(sc[i]);
  1272. { static data fields are already inserted in the globalsymtable }
  1273. if not(sp_static in current_object_option) then
  1274. recst.addfield(fieldvs);
  1275. end;
  1276. { restore current_object_option, it can be changed for
  1277. publishing or static }
  1278. current_object_option:=old_current_object_option;
  1279. end;
  1280. { Check for Case }
  1281. if (vd_record in options) and
  1282. try_to_consume(_CASE) then
  1283. begin
  1284. maxsize:=0;
  1285. maxalignment:=0;
  1286. maxpadalign:=0;
  1287. { including a field declaration? }
  1288. fieldvs:=nil;
  1289. sorg:=orgpattern;
  1290. hs:=pattern;
  1291. searchsym(hs,srsym,srsymtable);
  1292. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1293. begin
  1294. consume(_ID);
  1295. consume(_COLON);
  1296. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1297. symtablestack.top.insert(fieldvs);
  1298. end;
  1299. read_anon_type(casetype,true);
  1300. if assigned(fieldvs) then
  1301. begin
  1302. fieldvs.vardef:=casetype;
  1303. recst.addfield(fieldvs);
  1304. end;
  1305. if not(is_ordinal(casetype))
  1306. {$ifndef cpu64bitaddr}
  1307. or is_64bitint(casetype)
  1308. {$endif cpu64bitaddr}
  1309. then
  1310. Message(type_e_ordinal_expr_expected);
  1311. consume(_OF);
  1312. UnionSymtable:=trecordsymtable.create(current_settings.packrecords);
  1313. UnionDef:=trecorddef.create(unionsymtable);
  1314. uniondef.isunion:=true;
  1315. startvarrecsize:=UnionSymtable.datasize;
  1316. { align the bitpacking to the next byte }
  1317. UnionSymtable.datasize:=startvarrecsize;
  1318. startvarrecalign:=UnionSymtable.fieldalignment;
  1319. startpadalign:=Unionsymtable.padalignment;
  1320. symtablestack.push(UnionSymtable);
  1321. repeat
  1322. repeat
  1323. pt:=comp_expr(true);
  1324. if not(pt.nodetype=ordconstn) then
  1325. Message(parser_e_illegal_expression);
  1326. if try_to_consume(_POINTPOINT) then
  1327. pt:=crangenode.create(pt,comp_expr(true));
  1328. pt.free;
  1329. if token=_COMMA then
  1330. consume(_COMMA)
  1331. else
  1332. break;
  1333. until false;
  1334. consume(_COLON);
  1335. { read the vars }
  1336. consume(_LKLAMMER);
  1337. inc(variantrecordlevel);
  1338. if token<>_RKLAMMER then
  1339. read_record_fields([vd_record]);
  1340. dec(variantrecordlevel);
  1341. consume(_RKLAMMER);
  1342. { calculates maximal variant size }
  1343. maxsize:=max(maxsize,unionsymtable.datasize);
  1344. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1345. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1346. { the items of the next variant are overlayed }
  1347. unionsymtable.datasize:=startvarrecsize;
  1348. unionsymtable.fieldalignment:=startvarrecalign;
  1349. unionsymtable.padalignment:=startpadalign;
  1350. if (token<>_END) and (token<>_RKLAMMER) then
  1351. consume(_SEMICOLON)
  1352. else
  1353. break;
  1354. until (token=_END) or (token=_RKLAMMER);
  1355. symtablestack.pop(UnionSymtable);
  1356. { at last set the record size to that of the biggest variant }
  1357. unionsymtable.datasize:=maxsize;
  1358. unionsymtable.fieldalignment:=maxalignment;
  1359. unionsymtable.addalignmentpadding;
  1360. {$if defined(powerpc) or defined(powerpc64)}
  1361. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1362. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1363. is_first_field and
  1364. (recst.usefieldalignment=C_alignment) and
  1365. (maxpadalign>recst.padalignment) then
  1366. recst.padalignment:=maxpadalign;
  1367. {$endif powerpc or powerpc64}
  1368. { Align the offset where the union symtable is added }
  1369. case recst.usefieldalignment of
  1370. { allow the unionsymtable to be aligned however it wants }
  1371. { (within the global min/max limits) }
  1372. 0, { default }
  1373. C_alignment:
  1374. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  1375. { 1 byte alignment if we are bitpacked }
  1376. bit_alignment:
  1377. usedalign:=1;
  1378. { otherwise alignment at the packrecords alignment of the }
  1379. { current record }
  1380. else
  1381. usedalign:=used_align(recst.fieldalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1382. end;
  1383. offset:=align(recst.datasize,usedalign);
  1384. recst.datasize:=offset+unionsymtable.datasize;
  1385. if unionsymtable.recordalignment>recst.fieldalignment then
  1386. recst.fieldalignment:=unionsymtable.recordalignment;
  1387. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1388. uniondef.owner.deletedef(uniondef);
  1389. end;
  1390. block_type:=old_block_type;
  1391. current_object_option:=old_current_object_option;
  1392. { free the list }
  1393. sc.free;
  1394. {$ifdef powerpc}
  1395. is_first_field := false;
  1396. {$endif powerpc}
  1397. end;
  1398. end.