pdecvar.pas 59 KB

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