pdecvar.pas 59 KB

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