pdecvar.pas 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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_weak_external,
  731. is_public_var : boolean;
  732. dll_name,
  733. C_name : string;
  734. begin
  735. { only allowed for one var }
  736. { only allow external and public on global symbols }
  737. if vs.typ<>staticvarsym then
  738. begin
  739. Message(parser_e_no_local_var_external);
  740. exit;
  741. end;
  742. { defaults }
  743. is_dll:=false;
  744. is_cdecl:=false;
  745. is_external_var:=false;
  746. is_public_var:=false;
  747. C_name:=vs.realname;
  748. { macpas specific handling due to some switches}
  749. if (m_mac in current_settings.modeswitches) then
  750. begin
  751. if (cs_external_var in current_settings.localswitches) then
  752. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  753. is_cdecl:=true;
  754. is_external_var:=true;
  755. end
  756. else if (cs_externally_visible in current_settings.localswitches) then
  757. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  758. is_cdecl:=true;
  759. is_public_var:=true;
  760. end;
  761. end;
  762. { cdecl }
  763. if try_to_consume(_CVAR) then
  764. begin
  765. consume(_SEMICOLON);
  766. is_cdecl:=true;
  767. end;
  768. { external }
  769. is_weak_external:=try_to_consume(_WEAKEXTERNAL);
  770. if is_weak_external or
  771. try_to_consume(_EXTERNAL) then
  772. begin
  773. is_external_var:=true;
  774. if not is_cdecl then
  775. begin
  776. if idtoken<>_NAME then
  777. begin
  778. is_dll:=true;
  779. dll_name:=get_stringconst;
  780. if ExtractFileExt(dll_name)='' then
  781. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  782. end;
  783. if try_to_consume(_NAME) then
  784. C_name:=get_stringconst;
  785. end;
  786. consume(_SEMICOLON);
  787. end;
  788. { export or public }
  789. if idtoken in [_EXPORT,_PUBLIC] then
  790. begin
  791. consume(_ID);
  792. if is_external_var then
  793. Message(parser_e_not_external_and_export)
  794. else
  795. is_public_var:=true;
  796. if try_to_consume(_NAME) then
  797. C_name:=get_stringconst;
  798. consume(_SEMICOLON);
  799. end;
  800. { Windows uses an indirect reference using import tables }
  801. if is_dll and
  802. (target_info.system in system_all_windows) then
  803. include(vs.varoptions,vo_is_dll_var);
  804. { Add C _ prefix }
  805. if is_cdecl or
  806. (
  807. is_dll and
  808. (target_info.system in systems_darwin)
  809. ) then
  810. C_Name := target_info.Cprefix+C_Name;
  811. if is_public_var then
  812. begin
  813. include(vs.varoptions,vo_is_public);
  814. vs.varregable := vr_none;
  815. { mark as referenced }
  816. inc(vs.refs);
  817. end;
  818. { now we can insert it in the import lib if its a dll, or
  819. add it to the externals }
  820. if is_external_var then
  821. begin
  822. if vo_is_typed_const in vs.varoptions then
  823. Message(parser_e_initialized_not_for_external);
  824. include(vs.varoptions,vo_is_external);
  825. if (is_weak_external) then
  826. begin
  827. if not(target_info.system in system_weak_linking) then
  828. message(parser_e_weak_external_not_supported);
  829. include(vs.varoptions,vo_is_weak_external);
  830. end;
  831. vs.varregable := vr_none;
  832. if is_dll then
  833. current_module.AddExternalImport(dll_name,C_Name,0,true,false)
  834. else
  835. if tf_has_dllscanner in target_info.flags then
  836. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  837. end;
  838. { Set the assembler name }
  839. tstaticvarsym(vs).set_mangledname(C_Name);
  840. end;
  841. procedure read_var_decls(options:Tvar_dec_options);
  842. procedure read_default_value(sc : TFPObjectList);
  843. var
  844. vs : tabstractnormalvarsym;
  845. tcsym : tstaticvarsym;
  846. begin
  847. vs:=tabstractnormalvarsym(sc[0]);
  848. if sc.count>1 then
  849. Message(parser_e_initialized_only_one_var);
  850. if vo_is_thread_var in vs.varoptions then
  851. Message(parser_e_initialized_not_for_threadvar);
  852. consume(_EQUAL);
  853. case vs.typ of
  854. localvarsym :
  855. begin
  856. tcsym:=tstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  857. include(tcsym.symoptions,sp_internal);
  858. vs.defaultconstsym:=tcsym;
  859. symtablestack.top.insert(tcsym);
  860. read_typed_const(current_asmdata.asmlists[al_typedconsts],tcsym);
  861. end;
  862. staticvarsym :
  863. begin
  864. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs));
  865. end;
  866. else
  867. internalerror(200611051);
  868. end;
  869. vs.varstate:=vs_initialised;
  870. end;
  871. {$ifdef gpc_mode}
  872. procedure read_gpc_name(sc : TFPObjectList);
  873. var
  874. vs : tabstractnormalvarsym;
  875. C_Name : string;
  876. begin
  877. consume(_ID);
  878. C_Name:=get_stringconst;
  879. vs:=tabstractnormalvarsym(sc[0]);
  880. if sc.count>1 then
  881. Message(parser_e_absolute_only_one_var);
  882. if vs.typ=staticvarsym then
  883. begin
  884. tstaticvarsym(vs).set_mangledname(C_Name);
  885. include(vs.varoptions,vo_is_external);
  886. end
  887. else
  888. Message(parser_e_no_local_var_external);
  889. end;
  890. {$endif}
  891. procedure read_absolute(sc : TFPObjectList);
  892. var
  893. vs : tabstractvarsym;
  894. abssym : tabsolutevarsym;
  895. pt,hp : tnode;
  896. st : tsymtable;
  897. begin
  898. abssym:=nil;
  899. { only allowed for one var }
  900. vs:=tabstractvarsym(sc[0]);
  901. if sc.count>1 then
  902. Message(parser_e_absolute_only_one_var);
  903. if vo_is_typed_const in vs.varoptions then
  904. Message(parser_e_initialized_not_for_external);
  905. { parse the rest }
  906. pt:=expr;
  907. { check allowed absolute types }
  908. if (pt.nodetype=stringconstn) or
  909. (is_constcharnode(pt)) then
  910. begin
  911. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  912. abssym.fileinfo:=vs.fileinfo;
  913. if pt.nodetype=stringconstn then
  914. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  915. else
  916. abssym.asmname:=stringdup(chr(tordconstnode(pt).value.svalue));
  917. consume(token);
  918. abssym.abstyp:=toasm;
  919. end
  920. { address }
  921. else if is_constintnode(pt) then
  922. begin
  923. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  924. abssym.fileinfo:=vs.fileinfo;
  925. abssym.abstyp:=toaddr;
  926. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  927. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  928. message(parser_e_range_check_error)
  929. else
  930. abssym.addroffset:=Tordconstnode(pt).value.svalue;
  931. {$ifdef i386}
  932. abssym.absseg:=false;
  933. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  934. try_to_consume(_COLON) then
  935. begin
  936. pt.free;
  937. pt:=expr;
  938. if is_constintnode(pt) then
  939. begin
  940. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  941. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  942. message(parser_e_range_check_error)
  943. else
  944. abssym.addroffset:=abssym.addroffset shl 4+tordconstnode(pt).value.svalue;
  945. abssym.absseg:=true;
  946. end
  947. else
  948. Message(type_e_ordinal_expr_expected);
  949. end;
  950. {$endif i386}
  951. end
  952. { variable }
  953. else
  954. begin
  955. { remove subscriptn before checking for loadn }
  956. hp:=pt;
  957. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  958. hp:=tunarynode(hp).left;
  959. if (hp.nodetype=loadn) then
  960. begin
  961. { we should check the result type of loadn }
  962. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym]) then
  963. Message(parser_e_absolute_only_to_var_or_const);
  964. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  965. abssym.fileinfo:=vs.fileinfo;
  966. abssym.abstyp:=tovar;
  967. abssym.ref:=node_to_propaccesslist(pt);
  968. { if the sizes are different, can't be a regvar since you }
  969. { can't be "absolute upper 8 bits of a register" (except }
  970. { if its a record field of the same size of a record }
  971. { regvar, but in that case pt.resultdef.size will have }
  972. { the same size since it refers to the field and not to }
  973. { the whole record -- which is why we use pt and not hp) }
  974. { we can't take the size of an open array }
  975. if is_open_array(pt.resultdef) or
  976. (vs.vardef.size <> pt.resultdef.size) then
  977. make_not_regable(pt,[ra_addr_regable]);
  978. end
  979. else
  980. Message(parser_e_absolute_only_to_var_or_const);
  981. end;
  982. pt.free;
  983. { replace old varsym with the new absolutevarsym }
  984. if assigned(abssym) then
  985. begin
  986. st:=vs.owner;
  987. vs.owner.Delete(vs);
  988. st.insert(abssym);
  989. sc[0]:=abssym;
  990. end;
  991. end;
  992. var
  993. sc : TFPObjectList;
  994. vs : tabstractvarsym;
  995. hdef : tdef;
  996. i : longint;
  997. semicoloneaten,
  998. allowdefaultvalue,
  999. hasdefaultvalue : boolean;
  1000. old_current_object_option : tsymoptions;
  1001. hintsymoptions : tsymoptions;
  1002. old_block_type : tblock_type;
  1003. begin
  1004. old_current_object_option:=current_object_option;
  1005. { all variables are public if not in a object declaration }
  1006. current_object_option:=[sp_public];
  1007. old_block_type:=block_type;
  1008. block_type:=bt_type;
  1009. { Force an expected ID error message }
  1010. if not (token in [_ID,_CASE,_END]) then
  1011. consume(_ID);
  1012. { read vars }
  1013. sc:=TFPObjectList.create(false);
  1014. while (token=_ID) do
  1015. begin
  1016. semicoloneaten:=false;
  1017. hasdefaultvalue:=false;
  1018. allowdefaultvalue:=true;
  1019. sc.clear;
  1020. repeat
  1021. if (token = _ID) then
  1022. begin
  1023. case symtablestack.top.symtabletype of
  1024. localsymtable :
  1025. vs:=tlocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  1026. staticsymtable,
  1027. globalsymtable :
  1028. begin
  1029. vs:=tstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  1030. if vd_threadvar in options then
  1031. include(vs.varoptions,vo_is_thread_var);
  1032. end;
  1033. else
  1034. internalerror(200411064);
  1035. end;
  1036. sc.add(vs);
  1037. symtablestack.top.insert(vs);
  1038. end;
  1039. consume(_ID);
  1040. until not try_to_consume(_COMMA);
  1041. consume(_COLON);
  1042. {$ifdef gpc_mode}
  1043. if (m_gpc in current_settings.modeswitches) and
  1044. (token=_ID) and
  1045. (orgpattern='__asmname__') then
  1046. read_gpc_name(sc);
  1047. {$endif}
  1048. { read variable type def }
  1049. read_anon_type(hdef,false);
  1050. for i:=0 to sc.count-1 do
  1051. begin
  1052. vs:=tabstractvarsym(sc[i]);
  1053. vs.vardef:=hdef;
  1054. end;
  1055. { Process procvar directives }
  1056. if maybe_parse_proc_directives(hdef) then
  1057. semicoloneaten:=true;
  1058. { check for absolute }
  1059. if try_to_consume(_ABSOLUTE) then
  1060. begin
  1061. read_absolute(sc);
  1062. allowdefaultvalue:=false;
  1063. end;
  1064. { Check for EXTERNAL etc directives before a semicolon }
  1065. if (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) then
  1066. begin
  1067. read_public_and_external_sc(sc);
  1068. allowdefaultvalue:=false;
  1069. semicoloneaten:=true;
  1070. end;
  1071. { try to parse the hint directives }
  1072. hintsymoptions:=[];
  1073. try_consume_hintdirective(hintsymoptions);
  1074. for i:=0 to sc.count-1 do
  1075. begin
  1076. vs:=tabstractvarsym(sc[i]);
  1077. vs.symoptions := vs.symoptions + hintsymoptions;
  1078. end;
  1079. { Handling of Delphi typed const = initialized vars }
  1080. if allowdefaultvalue and
  1081. (token=_EQUAL) and
  1082. not(m_tp7 in current_settings.modeswitches) and
  1083. (symtablestack.top.symtabletype<>parasymtable) then
  1084. begin
  1085. { Add calling convention for procvar }
  1086. if (hdef.typ=procvardef) and
  1087. (hdef.typesym=nil) then
  1088. handle_calling_convention(tprocvardef(hdef));
  1089. read_default_value(sc);
  1090. hasdefaultvalue:=true;
  1091. end
  1092. else
  1093. begin
  1094. if not(semicoloneaten) then
  1095. consume(_SEMICOLON);
  1096. end;
  1097. { Support calling convention for procvars after semicolon }
  1098. if not(hasdefaultvalue) and
  1099. (hdef.typ=procvardef) and
  1100. (hdef.typesym=nil) then
  1101. begin
  1102. { Parse procvar directives after ; }
  1103. maybe_parse_proc_directives(hdef);
  1104. { Add calling convention for procvar }
  1105. handle_calling_convention(tprocvardef(hdef));
  1106. { Handling of Delphi typed const = initialized vars }
  1107. if (token=_EQUAL) and
  1108. not(m_tp7 in current_settings.modeswitches) and
  1109. (symtablestack.top.symtabletype<>parasymtable) then
  1110. begin
  1111. read_default_value(sc);
  1112. hasdefaultvalue:=true;
  1113. end;
  1114. end;
  1115. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1116. if (
  1117. (
  1118. (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) and
  1119. (m_cvar_support in current_settings.modeswitches)
  1120. ) or
  1121. (
  1122. (m_mac in current_settings.modeswitches) and
  1123. (
  1124. (cs_external_var in current_settings.localswitches) or
  1125. (cs_externally_visible in current_settings.localswitches)
  1126. )
  1127. )
  1128. ) then
  1129. read_public_and_external_sc(sc);
  1130. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1131. for i:=0 to sc.count-1 do
  1132. begin
  1133. vs:=tabstractvarsym(sc[i]);
  1134. if (vs.typ=staticvarsym) and
  1135. not(vo_is_typed_const in vs.varoptions) and
  1136. not(vo_is_external in vs.varoptions) then
  1137. insertbssdata(tstaticvarsym(vs));
  1138. end;
  1139. end;
  1140. block_type:=old_block_type;
  1141. current_object_option:=old_current_object_option;
  1142. { free the list }
  1143. sc.free;
  1144. end;
  1145. procedure read_record_fields(options:Tvar_dec_options);
  1146. var
  1147. sc : TFPObjectList;
  1148. i : longint;
  1149. old_block_type : tblock_type;
  1150. old_current_object_option : tsymoptions;
  1151. hs,sorg : string;
  1152. hdef,casetype : tdef;
  1153. { maxsize contains the max. size of a variant }
  1154. { startvarrec contains the start of the variant part of a record }
  1155. maxsize, startvarrecsize : longint;
  1156. usedalign,
  1157. maxalignment,startvarrecalign,
  1158. maxpadalign, startpadalign: shortint;
  1159. pt : tnode;
  1160. fieldvs : tfieldvarsym;
  1161. hstaticvs : tstaticvarsym;
  1162. vs : tabstractvarsym;
  1163. srsym : tsym;
  1164. srsymtable : TSymtable;
  1165. recst : tabstractrecordsymtable;
  1166. unionsymtable : trecordsymtable;
  1167. offset : longint;
  1168. uniondef : trecorddef;
  1169. hintsymoptions : tsymoptions;
  1170. semicoloneaten: boolean;
  1171. {$if defined(powerpc) or defined(powerpc64)}
  1172. tempdef: tdef;
  1173. is_first_field: boolean;
  1174. {$endif powerpc or powerpc64}
  1175. begin
  1176. recst:=tabstractrecordsymtable(symtablestack.top);
  1177. {$if defined(powerpc) or defined(powerpc64)}
  1178. is_first_field := true;
  1179. {$endif powerpc or powerpc64}
  1180. old_current_object_option:=current_object_option;
  1181. { all variables are public if not in a object declaration }
  1182. if not(vd_object in options) then
  1183. current_object_option:=[sp_public];
  1184. old_block_type:=block_type;
  1185. block_type:=bt_type;
  1186. { Force an expected ID error message }
  1187. if not (token in [_ID,_CASE,_END]) then
  1188. consume(_ID);
  1189. { read vars }
  1190. sc:=TFPObjectList.create(false);
  1191. while (token=_ID) and
  1192. not((vd_object in options) and
  1193. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1194. begin
  1195. semicoloneaten:=false;
  1196. sc.clear;
  1197. repeat
  1198. sorg:=orgpattern;
  1199. if token=_ID then
  1200. begin
  1201. vs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1202. sc.add(vs);
  1203. recst.insert(vs);
  1204. end;
  1205. consume(_ID);
  1206. until not try_to_consume(_COMMA);
  1207. consume(_COLON);
  1208. { Don't search in the recordsymtable for types }
  1209. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1210. symtablestack.pop(recst);
  1211. read_anon_type(hdef,false);
  1212. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1213. symtablestack.push(recst);
  1214. { Process procvar directives }
  1215. if maybe_parse_proc_directives(hdef) then
  1216. semicoloneaten:=true;
  1217. {$if defined(powerpc) or defined(powerpc64)}
  1218. { from gcc/gcc/config/rs6000/rs6000.h:
  1219. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1220. /* Return the alignment of a struct based on the Macintosh PowerPC
  1221. alignment rules. In general the alignment of a struct is
  1222. determined by the greatest alignment of its elements. However, the
  1223. PowerPC rules cause the alignment of a struct to peg at word
  1224. alignment except when the first field has greater than word
  1225. (32-bit) alignment, in which case the alignment is determined by
  1226. the alignment of the first field. */
  1227. }
  1228. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1229. is_first_field and
  1230. (symtablestack.top.symtabletype=recordsymtable) and
  1231. (trecordsymtable(symtablestack.top).usefieldalignment=C_alignment) then
  1232. begin
  1233. tempdef:=hdef;
  1234. while tempdef.typ=arraydef do
  1235. tempdef:=tarraydef(tempdef).elementdef;
  1236. if tempdef.typ<>recorddef then
  1237. maxpadalign:=tempdef.alignment
  1238. else
  1239. maxpadalign:=trecorddef(tempdef).padalignment;
  1240. if (maxpadalign>4) and
  1241. (maxpadalign>trecordsymtable(symtablestack.top).padalignment) then
  1242. trecordsymtable(symtablestack.top).padalignment:=maxpadalign;
  1243. is_first_field:=false;
  1244. end;
  1245. {$endif powerpc or powerpc64}
  1246. { types that use init/final are not allowed in variant parts, but
  1247. classes are allowed }
  1248. if (variantrecordlevel>0) and
  1249. (hdef.needs_inittable and not is_class(hdef)) then
  1250. Message(parser_e_cant_use_inittable_here);
  1251. { try to parse the hint directives }
  1252. hintsymoptions:=[];
  1253. try_consume_hintdirective(hintsymoptions);
  1254. { update variable type and hints }
  1255. for i:=0 to sc.count-1 do
  1256. begin
  1257. fieldvs:=tfieldvarsym(sc[i]);
  1258. fieldvs.vardef:=hdef;
  1259. { insert any additional hint directives }
  1260. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1261. end;
  1262. { Records and objects can't have default values }
  1263. { for a record there doesn't need to be a ; before the END or ) }
  1264. if not(token in [_END,_RKLAMMER]) and
  1265. not(semicoloneaten) then
  1266. consume(_SEMICOLON);
  1267. { Parse procvar directives after ; }
  1268. maybe_parse_proc_directives(hdef);
  1269. { Add calling convention for procvar }
  1270. if (hdef.typ=procvardef) and
  1271. (hdef.typesym=nil) then
  1272. handle_calling_convention(tprocvardef(hdef));
  1273. { Check for STATIC directive }
  1274. if (vd_object in options) and
  1275. (cs_static_keyword in current_settings.moduleswitches) and
  1276. (try_to_consume(_STATIC)) then
  1277. begin
  1278. { add static flag and staticvarsyms }
  1279. for i:=0 to sc.count-1 do
  1280. begin
  1281. fieldvs:=tfieldvarsym(sc[i]);
  1282. include(fieldvs.symoptions,sp_static);
  1283. hstaticvs:=tstaticvarsym.create('$'+lower(symtablestack.top.name^)+'_'+fieldvs.name,vs_value,hdef,[]);
  1284. recst.defowner.owner.insert(hstaticvs);
  1285. insertbssdata(hstaticvs);
  1286. end;
  1287. consume(_SEMICOLON);
  1288. end;
  1289. if (sp_published in current_object_option) and
  1290. not(is_class(hdef)) then
  1291. begin
  1292. Message(parser_e_cant_publish_that);
  1293. exclude(current_object_option,sp_published);
  1294. { recover by changing access type to public }
  1295. for i:=0 to sc.count-1 do
  1296. begin
  1297. fieldvs:=tfieldvarsym(sc[i]);
  1298. exclude(fieldvs.symoptions,sp_published);
  1299. include(fieldvs.symoptions,sp_public);
  1300. end;
  1301. end
  1302. else
  1303. if (sp_published in current_object_option) and
  1304. not(oo_can_have_published in tobjectdef(hdef).objectoptions) and
  1305. not(m_delphi in current_settings.modeswitches) then
  1306. begin
  1307. Message(parser_e_only_publishable_classes_can_be_published);
  1308. exclude(current_object_option,sp_published);
  1309. end;
  1310. { Generate field in the recordsymtable }
  1311. for i:=0 to sc.count-1 do
  1312. begin
  1313. fieldvs:=tfieldvarsym(sc[i]);
  1314. { static data fields are already inserted in the globalsymtable }
  1315. if not(sp_static in current_object_option) then
  1316. recst.addfield(fieldvs);
  1317. end;
  1318. { restore current_object_option, it can be changed for
  1319. publishing or static }
  1320. current_object_option:=old_current_object_option;
  1321. end;
  1322. { Check for Case }
  1323. if (vd_record in options) and
  1324. try_to_consume(_CASE) then
  1325. begin
  1326. maxsize:=0;
  1327. maxalignment:=0;
  1328. maxpadalign:=0;
  1329. { including a field declaration? }
  1330. fieldvs:=nil;
  1331. sorg:=orgpattern;
  1332. hs:=pattern;
  1333. searchsym(hs,srsym,srsymtable);
  1334. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1335. begin
  1336. consume(_ID);
  1337. consume(_COLON);
  1338. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1339. symtablestack.top.insert(fieldvs);
  1340. end;
  1341. read_anon_type(casetype,true);
  1342. if assigned(fieldvs) then
  1343. begin
  1344. fieldvs.vardef:=casetype;
  1345. recst.addfield(fieldvs);
  1346. end;
  1347. if not(is_ordinal(casetype))
  1348. {$ifndef cpu64bitaddr}
  1349. or is_64bitint(casetype)
  1350. {$endif cpu64bitaddr}
  1351. then
  1352. Message(type_e_ordinal_expr_expected);
  1353. consume(_OF);
  1354. UnionSymtable:=trecordsymtable.create(current_settings.packrecords);
  1355. UnionDef:=trecorddef.create(unionsymtable);
  1356. uniondef.isunion:=true;
  1357. startvarrecsize:=UnionSymtable.datasize;
  1358. { align the bitpacking to the next byte }
  1359. UnionSymtable.datasize:=startvarrecsize;
  1360. startvarrecalign:=UnionSymtable.fieldalignment;
  1361. startpadalign:=Unionsymtable.padalignment;
  1362. symtablestack.push(UnionSymtable);
  1363. repeat
  1364. repeat
  1365. pt:=comp_expr(true);
  1366. if not(pt.nodetype=ordconstn) then
  1367. Message(parser_e_illegal_expression);
  1368. if try_to_consume(_POINTPOINT) then
  1369. pt:=crangenode.create(pt,comp_expr(true));
  1370. pt.free;
  1371. if token=_COMMA then
  1372. consume(_COMMA)
  1373. else
  1374. break;
  1375. until false;
  1376. consume(_COLON);
  1377. { read the vars }
  1378. consume(_LKLAMMER);
  1379. inc(variantrecordlevel);
  1380. if token<>_RKLAMMER then
  1381. read_record_fields([vd_record]);
  1382. dec(variantrecordlevel);
  1383. consume(_RKLAMMER);
  1384. { calculates maximal variant size }
  1385. maxsize:=max(maxsize,unionsymtable.datasize);
  1386. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1387. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1388. { the items of the next variant are overlayed }
  1389. unionsymtable.datasize:=startvarrecsize;
  1390. unionsymtable.fieldalignment:=startvarrecalign;
  1391. unionsymtable.padalignment:=startpadalign;
  1392. if (token<>_END) and (token<>_RKLAMMER) then
  1393. consume(_SEMICOLON)
  1394. else
  1395. break;
  1396. until (token=_END) or (token=_RKLAMMER);
  1397. symtablestack.pop(UnionSymtable);
  1398. { at last set the record size to that of the biggest variant }
  1399. unionsymtable.datasize:=maxsize;
  1400. unionsymtable.fieldalignment:=maxalignment;
  1401. unionsymtable.addalignmentpadding;
  1402. {$if defined(powerpc) or defined(powerpc64)}
  1403. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1404. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1405. is_first_field and
  1406. (recst.usefieldalignment=C_alignment) and
  1407. (maxpadalign>recst.padalignment) then
  1408. recst.padalignment:=maxpadalign;
  1409. {$endif powerpc or powerpc64}
  1410. { Align the offset where the union symtable is added }
  1411. case recst.usefieldalignment of
  1412. { allow the unionsymtable to be aligned however it wants }
  1413. { (within the global min/max limits) }
  1414. 0, { default }
  1415. C_alignment:
  1416. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  1417. { 1 byte alignment if we are bitpacked }
  1418. bit_alignment:
  1419. usedalign:=1;
  1420. { otherwise alignment at the packrecords alignment of the }
  1421. { current record }
  1422. else
  1423. usedalign:=used_align(recst.fieldalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1424. end;
  1425. offset:=align(recst.datasize,usedalign);
  1426. recst.datasize:=offset+unionsymtable.datasize;
  1427. if unionsymtable.recordalignment>recst.fieldalignment then
  1428. recst.fieldalignment:=unionsymtable.recordalignment;
  1429. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1430. uniondef.owner.deletedef(uniondef);
  1431. end;
  1432. block_type:=old_block_type;
  1433. current_object_option:=old_current_object_option;
  1434. { free the list }
  1435. sc.free;
  1436. {$ifdef powerpc}
  1437. is_first_field := false;
  1438. {$endif powerpc}
  1439. end;
  1440. end.