pdecvar.pas 59 KB

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