pdecvar.pas 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  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. symtable,symsym,symdef;
  23. type
  24. tvar_dec_option=(vd_record,vd_object,vd_threadvar,vd_class,vd_final);
  25. tvar_dec_options=set of tvar_dec_option;
  26. function read_property_dec(is_classproperty:boolean;astruct:tabstractrecorddef):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. procedure try_consume_sectiondirective(var asection: ansistring);
  31. implementation
  32. uses
  33. SysUtils,
  34. { common }
  35. cutils,cclasses,
  36. { global }
  37. globtype,globals,tokens,verbose,constexp,
  38. systems,
  39. { symtable }
  40. symconst,symbase,symtype,defutil,defcmp,symcreat,
  41. {$ifdef jvm}
  42. jvmdef,
  43. {$endif}
  44. fmodule,htypechk,
  45. { pass 1 }
  46. node,pass_1,aasmdata,
  47. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,nutils,
  48. { codegen }
  49. ncgutil,ngenutil,
  50. { parser }
  51. scanner,
  52. pbase,pexpr,ptype,ptconst,pdecsub,
  53. {$ifdef jvm}
  54. pjvm,
  55. {$endif}
  56. { link }
  57. import
  58. ;
  59. function read_property_dec(is_classproperty:boolean;astruct:tabstractrecorddef):tpropertysym;
  60. { convert a node tree to symlist and return the last
  61. symbol }
  62. function parse_symlist(pl:tpropaccesslist;var def:tdef):boolean;
  63. var
  64. idx : longint;
  65. sym : tsym;
  66. srsymtable : TSymtable;
  67. st : TSymtable;
  68. p : tnode;
  69. begin
  70. result:=true;
  71. def:=nil;
  72. if token=_ID then
  73. begin
  74. if assigned(astruct) then
  75. sym:=search_struct_member(astruct,pattern)
  76. else
  77. searchsym(pattern,sym,srsymtable);
  78. if assigned(sym) then
  79. begin
  80. if assigned(astruct) and
  81. not is_visible_for_object(sym,astruct) then
  82. Message(parser_e_cant_access_private_member);
  83. case sym.typ of
  84. fieldvarsym :
  85. begin
  86. if (symtablestack.top.currentvisibility<>vis_private) then
  87. addsymref(sym);
  88. pl.addsym(sl_load,sym);
  89. def:=tfieldvarsym(sym).vardef;
  90. end;
  91. procsym :
  92. begin
  93. if (symtablestack.top.currentvisibility<>vis_private) then
  94. addsymref(sym);
  95. pl.addsym(sl_call,sym);
  96. end;
  97. else
  98. begin
  99. Message1(parser_e_illegal_field_or_method,orgpattern);
  100. def:=generrordef;
  101. result:=false;
  102. end;
  103. end;
  104. end
  105. else
  106. begin
  107. Message1(parser_e_illegal_field_or_method,orgpattern);
  108. def:=generrordef;
  109. result:=false;
  110. end;
  111. consume(_ID);
  112. repeat
  113. case token of
  114. _ID,
  115. _SEMICOLON :
  116. begin
  117. break;
  118. end;
  119. _POINT :
  120. begin
  121. consume(_POINT);
  122. if assigned(def) then
  123. begin
  124. st:=def.GetSymtable(gs_record);
  125. if assigned(st) then
  126. begin
  127. sym:=tsym(st.Find(pattern));
  128. if not(assigned(sym)) and is_object(def) then
  129. sym:=search_struct_member(tobjectdef(def),pattern);
  130. if assigned(sym) then
  131. begin
  132. pl.addsym(sl_subscript,sym);
  133. case sym.typ of
  134. fieldvarsym :
  135. def:=tfieldvarsym(sym).vardef;
  136. else
  137. begin
  138. Message1(sym_e_illegal_field,orgpattern);
  139. result:=false;
  140. end;
  141. end;
  142. end
  143. else
  144. begin
  145. Message1(sym_e_illegal_field,orgpattern);
  146. result:=false;
  147. end;
  148. end
  149. else
  150. begin
  151. Message(parser_e_invalid_qualifier);
  152. result:=false;
  153. end;
  154. end
  155. else
  156. begin
  157. Message(parser_e_invalid_qualifier);
  158. result:=false;
  159. end;
  160. consume(_ID);
  161. end;
  162. _LECKKLAMMER :
  163. begin
  164. consume(_LECKKLAMMER);
  165. repeat
  166. if def.typ=arraydef then
  167. begin
  168. idx:=0;
  169. p:=comp_expr(true,false);
  170. if (not codegenerror) then
  171. begin
  172. if (p.nodetype=ordconstn) then
  173. begin
  174. { type/range checking }
  175. inserttypeconv(p,tarraydef(def).rangedef);
  176. if (Tordconstnode(p).value<int64(low(longint))) or
  177. (Tordconstnode(p).value>int64(high(longint))) then
  178. message(parser_e_array_range_out_of_bounds)
  179. else
  180. idx:=Tordconstnode(p).value.svalue
  181. end
  182. else
  183. Message(type_e_ordinal_expr_expected)
  184. end;
  185. pl.addconst(sl_vec,idx,p.resultdef);
  186. p.free;
  187. def:=tarraydef(def).elementdef;
  188. end
  189. else
  190. begin
  191. Message(parser_e_invalid_qualifier);
  192. result:=false;
  193. end;
  194. until not try_to_consume(_COMMA);
  195. consume(_RECKKLAMMER);
  196. end;
  197. else
  198. begin
  199. Message(parser_e_ill_property_access_sym);
  200. result:=false;
  201. break;
  202. end;
  203. end;
  204. until false;
  205. end
  206. else
  207. begin
  208. Message(parser_e_ill_property_access_sym);
  209. result:=false;
  210. end;
  211. end;
  212. function allow_default_property(p : tpropertysym) : boolean;
  213. begin
  214. allow_default_property:=
  215. (is_ordinal(p.propdef) or
  216. {$ifndef cpu64bitaddr}
  217. is_64bitint(p.propdef) or
  218. {$endif cpu64bitaddr}
  219. is_class(p.propdef) or
  220. is_single(p.propdef) or
  221. (p.propdef.typ in [classrefdef,pointerdef]) or
  222. is_smallset(p.propdef)
  223. ) and not
  224. (
  225. (p.propdef.typ=arraydef) and
  226. (ppo_indexed in p.propoptions)
  227. ) and not
  228. (ppo_hasparameters in p.propoptions);
  229. end;
  230. procedure create_accessor_procsym(p: tpropertysym; pd: tprocdef; const prefix: string;
  231. accesstype: tpropaccesslisttypes);
  232. var
  233. sym: tprocsym;
  234. begin
  235. handle_calling_convention(pd);
  236. sym:=tprocsym.create(prefix+lower(p.realname));
  237. symtablestack.top.insert(sym);
  238. pd.procsym:=sym;
  239. include(pd.procoptions,po_dispid);
  240. include(pd.procoptions,po_global);
  241. pd.visibility:=vis_private;
  242. proc_add_definition(pd);
  243. p.propaccesslist[accesstype].addsym(sl_call,sym);
  244. p.propaccesslist[accesstype].procdef:=pd;
  245. end;
  246. procedure parse_dispinterface(p : tpropertysym; readpd,writepd: tprocdef;
  247. var paranr: word);
  248. var
  249. hasread, haswrite: boolean;
  250. pt: tnode;
  251. hdispid: longint;
  252. hparavs: tparavarsym;
  253. begin
  254. p.propaccesslist[palt_read].clear;
  255. p.propaccesslist[palt_write].clear;
  256. hasread:=true;
  257. haswrite:=true;
  258. if try_to_consume(_READONLY) then
  259. haswrite:=false
  260. else if try_to_consume(_WRITEONLY) then
  261. hasread:=false;
  262. if try_to_consume(_DISPID) then
  263. begin
  264. pt:=comp_expr(true,false);
  265. if is_constintnode(pt) then
  266. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  267. message(parser_e_range_check_error)
  268. else
  269. hdispid:=Tordconstnode(pt).value.svalue
  270. else
  271. Message(parser_e_dispid_must_be_ord_const);
  272. pt.free;
  273. end
  274. else
  275. hdispid:=tobjectdef(astruct).get_next_dispid;
  276. { COM property is simply a pair of methods, tagged with 'propertyget'
  277. and 'propertyset' flags (or a single method if access is restricted).
  278. Creating these implicit accessor methods also allows the rest of compiler
  279. to handle dispinterface properties the same way as regular ones. }
  280. if hasread then
  281. begin
  282. readpd.returndef:=p.propdef;
  283. readpd.dispid:=hdispid;
  284. readpd.proctypeoption:=potype_propgetter;
  285. create_accessor_procsym(p,readpd,'get$',palt_read);
  286. end;
  287. if haswrite then
  288. begin
  289. { add an extra parameter, a placeholder of the value to set }
  290. inc(paranr);
  291. hparavs:=tparavarsym.create('$value',10*paranr,vs_value,p.propdef,[]);
  292. writepd.parast.insert(hparavs);
  293. writepd.proctypeoption:=potype_propsetter;
  294. writepd.dispid:=hdispid;
  295. create_accessor_procsym(p,writepd,'put$',palt_write);
  296. end;
  297. end;
  298. procedure add_index_parameter(var paranr: word; p: tpropertysym; readprocdef, writeprocdef: tprocdef);
  299. var
  300. hparavs: tparavarsym;
  301. begin
  302. inc(paranr);
  303. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  304. readprocdef.parast.insert(hparavs);
  305. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  306. writeprocdef.parast.insert(hparavs);
  307. end;
  308. var
  309. sym : tsym;
  310. srsymtable: tsymtable;
  311. p : tpropertysym;
  312. overridden : tsym;
  313. varspez : tvarspez;
  314. hdef : tdef;
  315. arraytype : tdef;
  316. def : tdef;
  317. pt : tnode;
  318. sc : TFPObjectList;
  319. paranr : word;
  320. i : longint;
  321. ImplIntf : TImplementedInterface;
  322. found : boolean;
  323. hreadparavs,
  324. hparavs : tparavarsym;
  325. storedprocdef: tprocvardef;
  326. readprocdef,
  327. writeprocdef : tprocdef;
  328. begin
  329. { Generate temp procdefs to search for matching read/write
  330. procedures. the readprocdef will store all definitions }
  331. paranr:=0;
  332. readprocdef:=tprocdef.create(normal_function_level);
  333. writeprocdef:=tprocdef.create(normal_function_level);
  334. readprocdef.struct:=astruct;
  335. writeprocdef.struct:=astruct;
  336. if assigned(astruct) and is_classproperty then
  337. begin
  338. readprocdef.procoptions:=[po_staticmethod,po_classmethod];
  339. writeprocdef.procoptions:=[po_staticmethod,po_classmethod];
  340. end;
  341. if token<>_ID then
  342. begin
  343. consume(_ID);
  344. consume(_SEMICOLON);
  345. exit;
  346. end;
  347. { Generate propertysym and insert in symtablestack }
  348. p:=tpropertysym.create(orgpattern);
  349. p.visibility:=symtablestack.top.currentvisibility;
  350. p.default:=longint($80000000);
  351. if is_classproperty then
  352. include(p.symoptions, sp_static);
  353. symtablestack.top.insert(p);
  354. consume(_ID);
  355. { property parameters ? }
  356. if try_to_consume(_LECKKLAMMER) then
  357. begin
  358. if (p.visibility=vis_published) and
  359. not (m_delphi in current_settings.modeswitches) then
  360. Message(parser_e_cant_publish_that_property);
  361. { create a list of the parameters }
  362. symtablestack.push(readprocdef.parast);
  363. sc:=TFPObjectList.create(false);
  364. repeat
  365. if try_to_consume(_VAR) then
  366. varspez:=vs_var
  367. else if try_to_consume(_CONST) then
  368. varspez:=vs_const
  369. else if try_to_consume(_CONSTREF) then
  370. varspez:=vs_constref
  371. else if (m_out in current_settings.modeswitches) and try_to_consume(_OUT) then
  372. varspez:=vs_out
  373. else
  374. varspez:=vs_value;
  375. sc.clear;
  376. repeat
  377. inc(paranr);
  378. hreadparavs:=tparavarsym.create(orgpattern,10*paranr,varspez,generrordef,[]);
  379. readprocdef.parast.insert(hreadparavs);
  380. sc.add(hreadparavs);
  381. consume(_ID);
  382. until not try_to_consume(_COMMA);
  383. if try_to_consume(_COLON) then
  384. begin
  385. if try_to_consume(_ARRAY) then
  386. begin
  387. consume(_OF);
  388. { define range and type of range }
  389. hdef:=tarraydef.create(0,-1,s32inttype);
  390. { define field type }
  391. single_type(arraytype,[]);
  392. tarraydef(hdef).elementdef:=arraytype;
  393. end
  394. else
  395. single_type(hdef,[]);
  396. end
  397. else
  398. hdef:=cformaltype;
  399. for i:=0 to sc.count-1 do
  400. begin
  401. hreadparavs:=tparavarsym(sc[i]);
  402. hreadparavs.vardef:=hdef;
  403. { also update the writeprocdef }
  404. hparavs:=tparavarsym.create(hreadparavs.realname,hreadparavs.paranr,vs_value,hdef,[]);
  405. writeprocdef.parast.insert(hparavs);
  406. end;
  407. until not try_to_consume(_SEMICOLON);
  408. sc.free;
  409. symtablestack.pop(readprocdef.parast);
  410. consume(_RECKKLAMMER);
  411. { the parser need to know if a property has parameters, the
  412. index parameter doesn't count (PFV) }
  413. if paranr>0 then
  414. include(p.propoptions,ppo_hasparameters);
  415. end;
  416. { overridden property ? }
  417. { force property interface
  418. there is a property parameter
  419. a global property }
  420. if (token=_COLON) or (paranr>0) or (astruct=nil) then
  421. begin
  422. consume(_COLON);
  423. single_type(p.propdef,[]);
  424. if is_dispinterface(astruct) and not is_automatable(p.propdef) then
  425. Message1(type_e_not_automatable,p.propdef.typename);
  426. if (idtoken=_INDEX) then
  427. begin
  428. consume(_INDEX);
  429. pt:=comp_expr(true,false);
  430. { Only allow enum and integer indexes. Convert all integer
  431. values to s32int to be compatible with delphi, because the
  432. procedure matching requires equal parameters }
  433. if is_constnode(pt) and
  434. is_ordinal(pt.resultdef)
  435. {$ifndef cpu64bitaddr}
  436. and (not is_64bitint(pt.resultdef))
  437. {$endif cpu64bitaddr}
  438. then
  439. begin
  440. if is_integer(pt.resultdef) then
  441. inserttypeconv_internal(pt,s32inttype);
  442. p.index:=tordconstnode(pt).value.svalue;
  443. end
  444. else
  445. begin
  446. Message(parser_e_invalid_property_index_value);
  447. p.index:=0;
  448. end;
  449. p.indexdef:=pt.resultdef;
  450. include(p.propoptions,ppo_indexed);
  451. { concat a longint to the para templates }
  452. add_index_parameter(paranr,p,readprocdef,writeprocdef);
  453. pt.free;
  454. end;
  455. end
  456. else
  457. begin
  458. { do an property override }
  459. if (astruct.typ=objectdef) then
  460. overridden:=search_struct_member(tobjectdef(astruct).childof,p.name)
  461. else
  462. overridden:=nil;
  463. if assigned(overridden) and
  464. (overridden.typ=propertysym) and
  465. not(is_dispinterface(astruct)) then
  466. begin
  467. p.overriddenpropsym:=tpropertysym(overridden);
  468. { inherit all type related entries }
  469. p.indexdef:=tpropertysym(overridden).indexdef;
  470. p.propdef:=tpropertysym(overridden).propdef;
  471. p.index:=tpropertysym(overridden).index;
  472. p.default:=tpropertysym(overridden).default;
  473. p.propoptions:=tpropertysym(overridden).propoptions;
  474. if ppo_indexed in p.propoptions then
  475. add_index_parameter(paranr,p,readprocdef,writeprocdef);
  476. end
  477. else
  478. begin
  479. p.propdef:=generrordef;
  480. message(parser_e_no_property_found_to_override);
  481. end;
  482. end;
  483. if ((p.visibility=vis_published) or is_dispinterface(astruct)) and
  484. (not(p.propdef.is_publishable) or (sp_static in p.symoptions)) then
  485. begin
  486. Message(parser_e_cant_publish_that_property);
  487. p.visibility:=vis_public;
  488. end;
  489. if not(is_dispinterface(astruct)) then
  490. begin
  491. if try_to_consume(_READ) then
  492. begin
  493. p.propaccesslist[palt_read].clear;
  494. if parse_symlist(p.propaccesslist[palt_read],def) then
  495. begin
  496. sym:=p.propaccesslist[palt_read].firstsym^.sym;
  497. case sym.typ of
  498. procsym :
  499. begin
  500. { read is function returning the type of the property }
  501. readprocdef.returndef:=p.propdef;
  502. { Insert hidden parameters }
  503. handle_calling_convention(readprocdef);
  504. { search procdefs matching readprocdef }
  505. { we ignore hidden stuff here because the property access symbol might have
  506. non default calling conventions which might change the hidden stuff;
  507. see tw3216.pp (FK) }
  508. p.propaccesslist[palt_read].procdef:=Tprocsym(sym).Find_procdef_bypara(readprocdef.paras,p.propdef,[cpo_allowdefaults,cpo_ignorehidden]);
  509. if not assigned(p.propaccesslist[palt_read].procdef) or
  510. { because of cpo_ignorehidden we need to compare if it is a static class method and we have a class property }
  511. ((sp_static in p.symoptions) <> tprocdef(p.propaccesslist[palt_read].procdef).no_self_node) then
  512. Message(parser_e_ill_property_access_sym)
  513. else
  514. begin
  515. {$ifdef jvm}
  516. { if the visibility of the getter is lower than
  517. the visibility of the property, wrap it so that
  518. we can call it from all contexts in which the
  519. property is visible }
  520. if (tprocdef(p.propaccesslist[palt_read].procdef).visibility<p.visibility) then
  521. begin
  522. p.propaccesslist[palt_read].procdef:=jvm_wrap_method_with_vis(tprocdef(p.propaccesslist[palt_read].procdef),p.visibility);
  523. p.propaccesslist[palt_read].firstsym^.sym:=tprocdef(p.propaccesslist[palt_read].procdef).procsym;
  524. end;
  525. {$endif jvm}
  526. end;
  527. end;
  528. fieldvarsym :
  529. begin
  530. if not assigned(def) then
  531. internalerror(200310071);
  532. if compare_defs(def,p.propdef,nothingn)>=te_equal then
  533. begin
  534. { property parameters are allowed if this is
  535. an indexed property, because the index is then
  536. the parameter.
  537. Note: In the help of Kylix it is written
  538. that it isn't allowed, but the compiler accepts it (PFV) }
  539. if (ppo_hasparameters in p.propoptions) or
  540. ((sp_static in p.symoptions) <> (sp_static in sym.symoptions)) then
  541. Message(parser_e_ill_property_access_sym);
  542. {$ifdef jvm}
  543. { if the visibility of the field is lower than the
  544. visibility of the property, wrap it in a getter
  545. so that we can access it from all contexts in
  546. which the property is visibile }
  547. if (tfieldvarsym(sym).visibility<p.visibility) then
  548. jvm_create_getter_for_property(p);
  549. {$endif}
  550. end
  551. else
  552. IncompatibleTypes(def,p.propdef);
  553. end;
  554. else
  555. Message(parser_e_ill_property_access_sym);
  556. end;
  557. end;
  558. end;
  559. if try_to_consume(_WRITE) then
  560. begin
  561. p.propaccesslist[palt_write].clear;
  562. if parse_symlist(p.propaccesslist[palt_write],def) then
  563. begin
  564. sym:=p.propaccesslist[palt_write].firstsym^.sym;
  565. case sym.typ of
  566. procsym :
  567. begin
  568. { write is a procedure with an extra value parameter
  569. of the of the property }
  570. writeprocdef.returndef:=voidtype;
  571. inc(paranr);
  572. hparavs:=tparavarsym.create('$value',10*paranr,vs_value,p.propdef,[]);
  573. writeprocdef.parast.insert(hparavs);
  574. { Insert hidden parameters }
  575. handle_calling_convention(writeprocdef);
  576. { search procdefs matching writeprocdef }
  577. if cs_varpropsetter in current_settings.localswitches then
  578. p.propaccesslist[palt_write].procdef:=Tprocsym(sym).Find_procdef_bypara(writeprocdef.paras,writeprocdef.returndef,[cpo_allowdefaults,cpo_ignorevarspez])
  579. else
  580. p.propaccesslist[palt_write].procdef:=Tprocsym(sym).Find_procdef_bypara(writeprocdef.paras,writeprocdef.returndef,[cpo_allowdefaults]);
  581. if not assigned(p.propaccesslist[palt_write].procdef) then
  582. Message(parser_e_ill_property_access_sym)
  583. else
  584. begin
  585. {$ifdef jvm}
  586. { if the visibility of the getter is lower than
  587. the visibility of the property, wrap it so that
  588. we can call it from all contexts in which the
  589. property is visible }
  590. if (tprocdef(p.propaccesslist[palt_write].procdef).visibility<p.visibility) then
  591. begin
  592. p.propaccesslist[palt_write].procdef:=jvm_wrap_method_with_vis(tprocdef(p.propaccesslist[palt_write].procdef),p.visibility);
  593. p.propaccesslist[palt_write].firstsym^.sym:=tprocdef(p.propaccesslist[palt_write].procdef).procsym;
  594. end;
  595. {$endif jvm}
  596. end;
  597. end;
  598. fieldvarsym :
  599. begin
  600. if not assigned(def) then
  601. internalerror(200310072);
  602. if compare_defs(def,p.propdef,nothingn)>=te_equal then
  603. begin
  604. { property parameters are allowed if this is
  605. an indexed property, because the index is then
  606. the parameter.
  607. Note: In the help of Kylix it is written
  608. that it isn't allowed, but the compiler accepts it (PFV) }
  609. if (ppo_hasparameters in p.propoptions) or
  610. ((sp_static in p.symoptions) <> (sp_static in sym.symoptions)) then
  611. Message(parser_e_ill_property_access_sym);
  612. {$ifdef jvm}
  613. { if the visibility of the field is lower than the
  614. visibility of the property, wrap it in a getter
  615. so that we can access it from all contexts in
  616. which the property is visibile }
  617. if (tfieldvarsym(sym).visibility<p.visibility) then
  618. jvm_create_setter_for_property(p);
  619. {$endif}
  620. end
  621. else
  622. IncompatibleTypes(def,p.propdef);
  623. end;
  624. else
  625. Message(parser_e_ill_property_access_sym);
  626. end;
  627. end;
  628. end;
  629. end
  630. else
  631. parse_dispinterface(p,readprocdef,writeprocdef,paranr);
  632. { stored is not allowed for dispinterfaces, records or class properties }
  633. if assigned(astruct) and not(is_dispinterface(astruct) or is_record(astruct)) and not is_classproperty then
  634. begin
  635. { ppo_stored is default on for not overridden properties }
  636. if not assigned(p.overriddenpropsym) then
  637. include(p.propoptions,ppo_stored);
  638. if try_to_consume(_STORED) then
  639. begin
  640. include(p.propoptions,ppo_stored);
  641. p.propaccesslist[palt_stored].clear;
  642. case token of
  643. _ID:
  644. begin
  645. { in the case that idtoken=_DEFAULT }
  646. { we have to do nothing except }
  647. { setting ppo_stored, it's the same }
  648. { as stored true }
  649. if idtoken<>_DEFAULT then
  650. begin
  651. { parse_symlist cannot deal with constsyms, and
  652. we also don't want to put constsyms in symlists
  653. since they have to be evaluated immediately rather
  654. than each time the property is accessed
  655. The proper fix would be to always create a parse tree
  656. and then convert that one, if appropriate, to a symlist.
  657. Currently, we e.g. don't support any constant expressions
  658. yet either here, while Delphi does.
  659. }
  660. { make sure we don't let constants mask class fields/
  661. methods
  662. }
  663. if (not assigned(astruct) or
  664. (search_struct_member(astruct,pattern)=nil)) and
  665. searchsym(pattern,sym,srsymtable) and
  666. (sym.typ = constsym) then
  667. begin
  668. addsymref(sym);
  669. if not is_boolean(tconstsym(sym).constdef) then
  670. Message(parser_e_stored_property_must_be_boolean)
  671. else if (tconstsym(sym).value.valueord=0) then
  672. { same as for _FALSE }
  673. exclude(p.propoptions,ppo_stored)
  674. else
  675. { same as for _TRUE }
  676. p.default:=longint($80000000);
  677. consume(_ID);
  678. end
  679. else if parse_symlist(p.propaccesslist[palt_stored],def) then
  680. begin
  681. sym:=p.propaccesslist[palt_stored].firstsym^.sym;
  682. case sym.typ of
  683. procsym :
  684. begin
  685. { Create a temporary procvardef to handle parameters }
  686. storedprocdef:=tprocvardef.create(normal_function_level);
  687. include(storedprocdef.procoptions,po_methodpointer);
  688. { Return type must be boolean }
  689. storedprocdef.returndef:=pasbool8type;
  690. { Add index parameter if needed }
  691. if ppo_indexed in p.propoptions then
  692. begin
  693. hparavs:=tparavarsym.create('$index',10,vs_value,p.indexdef,[]);
  694. storedprocdef.parast.insert(hparavs);
  695. end;
  696. { Insert hidden parameters }
  697. handle_calling_convention(storedprocdef);
  698. p.propaccesslist[palt_stored].procdef:=Tprocsym(sym).Find_procdef_bypara(storedprocdef.paras,storedprocdef.returndef,[cpo_allowdefaults,cpo_ignorehidden]);
  699. if not assigned(p.propaccesslist[palt_stored].procdef) then
  700. message(parser_e_ill_property_storage_sym);
  701. { Not needed anymore }
  702. storedprocdef.owner.deletedef(storedprocdef);
  703. end;
  704. fieldvarsym :
  705. begin
  706. if not assigned(def) then
  707. internalerror(200310073);
  708. if (ppo_hasparameters in p.propoptions) or
  709. not(is_boolean(def)) then
  710. Message(parser_e_stored_property_must_be_boolean);
  711. end;
  712. else
  713. Message(parser_e_ill_property_access_sym);
  714. end;
  715. end;
  716. end;
  717. end;
  718. _FALSE:
  719. begin
  720. consume(_FALSE);
  721. exclude(p.propoptions,ppo_stored);
  722. end;
  723. _TRUE:
  724. begin
  725. p.default:=longint($80000000);
  726. consume(_TRUE);
  727. end;
  728. end;
  729. end;
  730. end;
  731. if not is_record(astruct) and try_to_consume(_DEFAULT) then
  732. begin
  733. if not allow_default_property(p) then
  734. begin
  735. Message(parser_e_property_cant_have_a_default_value);
  736. { Error recovery }
  737. pt:=comp_expr(true,false);
  738. pt.free;
  739. end
  740. else
  741. begin
  742. { Get the result of the default, the firstpass is
  743. needed to support values like -1 }
  744. pt:=comp_expr(true,false);
  745. if (p.propdef.typ=setdef) and
  746. (pt.nodetype=arrayconstructorn) then
  747. begin
  748. arrayconstructor_to_set(pt);
  749. do_typecheckpass(pt);
  750. end;
  751. inserttypeconv(pt,p.propdef);
  752. if not(is_constnode(pt)) then
  753. Message(parser_e_property_default_value_must_const);
  754. { Set default value }
  755. case pt.nodetype of
  756. setconstn :
  757. p.default:=plongint(tsetconstnode(pt).value_set)^;
  758. ordconstn :
  759. if (Tordconstnode(pt).value<int64(low(longint))) or
  760. (Tordconstnode(pt).value>int64(high(cardinal))) then
  761. message(parser_e_range_check_error)
  762. else
  763. p.default:=longint(tordconstnode(pt).value.svalue);
  764. niln :
  765. p.default:=0;
  766. realconstn:
  767. p.default:=longint(single(trealconstnode(pt).value_real));
  768. end;
  769. pt.free;
  770. end;
  771. end
  772. else if not is_record(astruct) and try_to_consume(_NODEFAULT) then
  773. begin
  774. p.default:=longint($80000000);
  775. end;
  776. (*
  777. else {if allow_default_property(p) then
  778. begin
  779. p.default:=longint($80000000);
  780. end;
  781. *)
  782. { Parse possible "implements" keyword }
  783. if not is_record(astruct) and try_to_consume(_IMPLEMENTS) then
  784. begin
  785. single_type(def,[]);
  786. if not(is_interface(def)) then
  787. message(parser_e_class_implements_must_be_interface);
  788. if is_interface(p.propdef) then
  789. begin
  790. if compare_defs(def,p.propdef,nothingn)<te_equal then
  791. begin
  792. message2(parser_e_implements_must_have_correct_type,def.typename,p.propdef.typename);
  793. exit;
  794. end;
  795. end
  796. else if is_class(p.propdef) then
  797. begin
  798. ImplIntf:=tobjectdef(p.propdef).find_implemented_interface(tobjectdef(def));
  799. if assigned(ImplIntf) then
  800. begin
  801. if compare_defs(ImplIntf.IntfDef,def,nothingn)<te_equal then
  802. begin
  803. message2(parser_e_implements_must_have_correct_type,ImplIntf.IntfDef.typename,def.typename);
  804. exit;
  805. end;
  806. end
  807. else
  808. begin
  809. message2(parser_e_class_doesnt_implement_interface,p.propdef.typename,def.typename);
  810. exit;
  811. end;
  812. end
  813. else
  814. begin
  815. message(parser_e_implements_must_be_class_or_interface);
  816. exit;
  817. end;
  818. if not assigned(p.propaccesslist[palt_read].firstsym) then
  819. begin
  820. message(parser_e_implements_must_read_specifier);
  821. exit;
  822. end;
  823. if assigned(p.propaccesslist[palt_read].procdef) and
  824. (tprocdef(p.propaccesslist[palt_read].procdef).proccalloption<>pocall_default) then
  825. message(parser_e_implements_getter_not_default_cc);
  826. if assigned(p.propaccesslist[palt_write].firstsym) then
  827. begin
  828. message(parser_e_implements_must_not_have_write_specifier);
  829. exit;
  830. end;
  831. if assigned(p.propaccesslist[palt_stored].firstsym) then
  832. begin
  833. message(parser_e_implements_must_not_have_stored_specifier);
  834. exit;
  835. end;
  836. found:=false;
  837. for i:=0 to tobjectdef(astruct).ImplementedInterfaces.Count-1 do
  838. begin
  839. ImplIntf:=TImplementedInterface(tobjectdef(astruct).ImplementedInterfaces[i]);
  840. if compare_defs(def,ImplIntf.IntfDef,nothingn)>=te_equal then
  841. begin
  842. found:=true;
  843. break;
  844. end;
  845. end;
  846. if found then
  847. begin
  848. { An interface may not be delegated by more than one property,
  849. it also may not have method mappings. }
  850. if Assigned(ImplIntf.ImplementsGetter) then
  851. message1(parser_e_duplicate_implements_clause,ImplIntf.IntfDef.typename);
  852. if Assigned(ImplIntf.NameMappings) then
  853. message2(parser_e_mapping_no_implements,ImplIntf.IntfDef.typename,astruct.objrealname^);
  854. ImplIntf.ImplementsGetter:=p;
  855. ImplIntf.VtblImplIntf:=ImplIntf;
  856. case p.propaccesslist[palt_read].firstsym^.sym.typ of
  857. procsym :
  858. begin
  859. if (po_virtualmethod in tprocdef(p.propaccesslist[palt_read].procdef).procoptions) and
  860. not is_objectpascal_helper(tprocdef(p.propaccesslist[palt_read].procdef).struct) then
  861. ImplIntf.IType:=etVirtualMethodResult
  862. else
  863. ImplIntf.IType:=etStaticMethodResult;
  864. end;
  865. fieldvarsym :
  866. begin
  867. ImplIntf.IType:=etFieldValue;
  868. { this must be done more sophisticated, here is also probably the wrong place }
  869. ImplIntf.IOffset:=tfieldvarsym(p.propaccesslist[palt_read].firstsym^.sym).fieldoffset;
  870. end
  871. else
  872. internalerror(200802161);
  873. end;
  874. if not is_interface(p.propdef) then
  875. case ImplIntf.IType of
  876. etVirtualMethodResult: ImplIntf.IType := etVirtualMethodClass;
  877. etStaticMethodResult: ImplIntf.IType := etStaticMethodClass;
  878. etFieldValue: ImplIntf.IType := etFieldValueClass;
  879. else
  880. internalerror(200912101);
  881. end;
  882. end
  883. else
  884. message1(parser_e_implements_uses_non_implemented_interface,def.typename);
  885. end;
  886. { remove unneeded procdefs }
  887. if readprocdef.proctypeoption<>potype_propgetter then
  888. readprocdef.owner.deletedef(readprocdef);
  889. if writeprocdef.proctypeoption<>potype_propsetter then
  890. writeprocdef.owner.deletedef(writeprocdef);
  891. result:=p;
  892. end;
  893. function maybe_parse_proc_directives(def:tdef):boolean;
  894. var
  895. newtype : ttypesym;
  896. begin
  897. result:=false;
  898. { Process procvar directives before = and ; }
  899. if (def.typ=procvardef) and
  900. (def.typesym=nil) and
  901. check_proc_directive(true) then
  902. begin
  903. newtype:=ttypesym.create('unnamed',def);
  904. parse_var_proc_directives(tsym(newtype));
  905. newtype.typedef:=nil;
  906. def.typesym:=nil;
  907. newtype.free;
  908. result:=true;
  909. end;
  910. end;
  911. const
  912. variantrecordlevel : longint = 0;
  913. procedure read_public_and_external_sc(sc:TFPObjectList);
  914. var
  915. vs: tabstractvarsym;
  916. begin
  917. { only allowed for one var }
  918. vs:=tabstractvarsym(sc[0]);
  919. if sc.count>1 then
  920. Message1(parser_e_directive_only_one_var,arraytokeninfo[idtoken].str);
  921. read_public_and_external(vs);
  922. end;
  923. procedure read_public_and_external(vs: tabstractvarsym);
  924. var
  925. is_dll,
  926. is_cdecl,
  927. is_external_var,
  928. is_weak_external,
  929. is_public_var : boolean;
  930. dll_name,section_name,
  931. C_name,mangledname : string;
  932. begin
  933. { only allowed for one var }
  934. { only allow external and public on global symbols }
  935. if vs.typ<>staticvarsym then
  936. begin
  937. Message(parser_e_no_local_var_external);
  938. exit;
  939. end;
  940. { defaults }
  941. is_dll:=false;
  942. is_cdecl:=false;
  943. is_external_var:=false;
  944. is_public_var:=false;
  945. section_name := '';
  946. C_name:=vs.realname;
  947. { macpas specific handling due to some switches}
  948. if (m_mac in current_settings.modeswitches) then
  949. begin
  950. if (cs_external_var in current_settings.localswitches) then
  951. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  952. is_cdecl:=true;
  953. is_external_var:=true;
  954. end
  955. else if (cs_externally_visible in current_settings.localswitches) then
  956. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  957. is_cdecl:=true;
  958. is_public_var:=true;
  959. end;
  960. end;
  961. { cdecl }
  962. if try_to_consume(_CVAR) then
  963. begin
  964. consume(_SEMICOLON);
  965. is_cdecl:=true;
  966. end;
  967. { external }
  968. is_weak_external:=try_to_consume(_WEAKEXTERNAL);
  969. if is_weak_external or
  970. try_to_consume(_EXTERNAL) then
  971. begin
  972. is_external_var:=true;
  973. if (idtoken<>_NAME) and (token<>_SEMICOLON) then
  974. begin
  975. is_dll:=true;
  976. dll_name:=get_stringconst;
  977. if ExtractFileExt(dll_name)='' then
  978. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  979. end;
  980. if not(is_cdecl) and try_to_consume(_NAME) then
  981. C_name:=get_stringconst;
  982. consume(_SEMICOLON);
  983. end;
  984. { export or public }
  985. if idtoken in [_EXPORT,_PUBLIC] then
  986. begin
  987. consume(_ID);
  988. if is_external_var then
  989. Message(parser_e_not_external_and_export)
  990. else
  991. is_public_var:=true;
  992. if try_to_consume(_NAME) then
  993. C_name:=get_stringconst;
  994. if (target_info.system in systems_allow_section_no_semicolon) and
  995. (vs.typ=staticvarsym) and
  996. try_to_consume (_SECTION) then
  997. section_name:=get_stringconst;
  998. consume(_SEMICOLON);
  999. end;
  1000. { Windows uses an indirect reference using import tables }
  1001. if is_dll and
  1002. (target_info.system in systems_all_windows) then
  1003. include(vs.varoptions,vo_is_dll_var);
  1004. { This can only happen if vs.typ=staticvarsym }
  1005. if section_name<>'' then
  1006. begin
  1007. tstaticvarsym(vs).section:=section_name;
  1008. include(vs.varoptions,vo_has_section);
  1009. end;
  1010. { Add C _ prefix }
  1011. if is_cdecl or
  1012. (
  1013. is_dll and
  1014. (target_info.system in systems_darwin)
  1015. ) then
  1016. C_Name := target_info.Cprefix+C_Name;
  1017. if is_public_var then
  1018. begin
  1019. include(vs.varoptions,vo_is_public);
  1020. vs.varregable := vr_none;
  1021. { mark as referenced }
  1022. inc(vs.refs);
  1023. end;
  1024. mangledname:=C_name;
  1025. { now we can insert it in the import lib if its a dll, or
  1026. add it to the externals }
  1027. if is_external_var then
  1028. begin
  1029. if vo_is_typed_const in vs.varoptions then
  1030. Message(parser_e_initialized_not_for_external);
  1031. include(vs.varoptions,vo_is_external);
  1032. if (is_weak_external) then
  1033. begin
  1034. if not(target_info.system in systems_weak_linking) then
  1035. message(parser_e_weak_external_not_supported);
  1036. include(vs.varoptions,vo_is_weak_external);
  1037. end;
  1038. vs.varregable := vr_none;
  1039. if is_dll then
  1040. begin
  1041. if target_info.system in (systems_all_windows + systems_nativent +
  1042. [system_i386_emx, system_i386_os2]) then
  1043. mangledname:=make_dllmangledname(dll_name,C_name,0,pocall_none);
  1044. current_module.AddExternalImport(dll_name,C_Name,mangledname,0,true,false);
  1045. end
  1046. else
  1047. if tf_has_dllscanner in target_info.flags then
  1048. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  1049. end;
  1050. { Set the assembler name }
  1051. tstaticvarsym(vs).set_mangledname(mangledname);
  1052. end;
  1053. procedure try_consume_sectiondirective(var asection: ansistring);
  1054. begin
  1055. if idtoken=_SECTION then
  1056. begin
  1057. consume(_ID);
  1058. asection:=get_stringconst;
  1059. consume(_SEMICOLON);
  1060. end;
  1061. end;
  1062. procedure try_read_field_external(vs: tabstractvarsym);
  1063. var
  1064. extname: string;
  1065. begin
  1066. if try_to_consume(_EXTERNAL) then
  1067. begin
  1068. consume(_NAME);
  1069. extname:=get_stringconst;
  1070. tfieldvarsym(vs).set_externalname(extname);
  1071. consume(_SEMICOLON);
  1072. end;
  1073. end;
  1074. procedure try_read_field_external_sc(sc:TFPObjectList);
  1075. var
  1076. vs: tabstractvarsym;
  1077. begin
  1078. { only allowed for one var }
  1079. vs:=tabstractvarsym(sc[0]);
  1080. if sc.count>1 then
  1081. Message1(parser_e_directive_only_one_var,arraytokeninfo[idtoken].str);
  1082. try_read_field_external(vs);
  1083. end;
  1084. procedure read_var_decls(options:Tvar_dec_options);
  1085. procedure read_default_value(sc : TFPObjectList);
  1086. var
  1087. vs : tabstractnormalvarsym;
  1088. tcsym : tstaticvarsym;
  1089. begin
  1090. vs:=tabstractnormalvarsym(sc[0]);
  1091. if sc.count>1 then
  1092. Message(parser_e_initialized_only_one_var);
  1093. if vo_is_thread_var in vs.varoptions then
  1094. Message(parser_e_initialized_not_for_threadvar);
  1095. consume(_EQ);
  1096. case vs.typ of
  1097. localvarsym :
  1098. begin
  1099. tcsym:=tstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  1100. include(tcsym.symoptions,sp_internal);
  1101. vs.defaultconstsym:=tcsym;
  1102. symtablestack.top.insert(tcsym);
  1103. read_typed_const(current_asmdata.asmlists[al_typedconsts],tcsym,false);
  1104. end;
  1105. staticvarsym :
  1106. begin
  1107. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs),false);
  1108. end;
  1109. else
  1110. internalerror(200611051);
  1111. end;
  1112. vs.varstate:=vs_initialised;
  1113. end;
  1114. {$ifdef gpc_mode}
  1115. procedure read_gpc_name(sc : TFPObjectList);
  1116. var
  1117. vs : tabstractnormalvarsym;
  1118. C_Name : string;
  1119. begin
  1120. consume(_ID);
  1121. C_Name:=get_stringconst;
  1122. vs:=tabstractnormalvarsym(sc[0]);
  1123. if sc.count>1 then
  1124. Message(parser_e_directive_only_one_var,'ABSOLUTE');
  1125. if vs.typ=staticvarsym then
  1126. begin
  1127. tstaticvarsym(vs).set_mangledname(C_Name);
  1128. include(vs.varoptions,vo_is_external);
  1129. end
  1130. else
  1131. Message(parser_e_no_local_var_external);
  1132. end;
  1133. {$endif}
  1134. procedure read_absolute(sc : TFPObjectList);
  1135. var
  1136. vs : tabstractvarsym;
  1137. abssym : tabsolutevarsym;
  1138. pt,hp : tnode;
  1139. st : tsymtable;
  1140. {$ifdef i386}
  1141. tmpaddr : int64;
  1142. {$endif}
  1143. begin
  1144. abssym:=nil;
  1145. { only allowed for one var }
  1146. vs:=tabstractvarsym(sc[0]);
  1147. if sc.count>1 then
  1148. Message1(parser_e_directive_only_one_var,'ABSOLUTE');
  1149. if vo_is_typed_const in vs.varoptions then
  1150. Message(parser_e_initialized_not_for_external);
  1151. { parse the rest }
  1152. pt:=expr(true);
  1153. { check allowed absolute types }
  1154. if (pt.nodetype=stringconstn) or
  1155. (is_constcharnode(pt)) then
  1156. begin
  1157. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1158. abssym.fileinfo:=vs.fileinfo;
  1159. if pt.nodetype=stringconstn then
  1160. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  1161. else
  1162. abssym.asmname:=stringdup(chr(tordconstnode(pt).value.svalue));
  1163. consume(token);
  1164. abssym.abstyp:=toasm;
  1165. end
  1166. { address }
  1167. else if is_constintnode(pt) then
  1168. begin
  1169. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1170. abssym.fileinfo:=vs.fileinfo;
  1171. abssym.abstyp:=toaddr;
  1172. {$ifndef cpu64bitaddr}
  1173. { on 64 bit systems, abssym.addroffset is a qword and hence this
  1174. test is useless (value is a 64 bit entity) and will always fail
  1175. for positive values (since int64(high(abssym.addroffset))=-1
  1176. }
  1177. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  1178. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  1179. message(parser_e_range_check_error)
  1180. else
  1181. {$endif}
  1182. abssym.addroffset:=Tordconstnode(pt).value.svalue;
  1183. {$ifdef i386}
  1184. abssym.absseg:=false;
  1185. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1186. try_to_consume(_COLON) then
  1187. begin
  1188. pt.free;
  1189. pt:=expr(true);
  1190. if is_constintnode(pt) then
  1191. begin
  1192. tmpaddr:=abssym.addroffset shl 4+tordconstnode(pt).value.svalue;
  1193. if (tmpaddr<int64(low(abssym.addroffset))) or
  1194. (tmpaddr>int64(high(abssym.addroffset))) then
  1195. message(parser_e_range_check_error)
  1196. else
  1197. abssym.addroffset:=tmpaddr;
  1198. abssym.absseg:=true;
  1199. end
  1200. else
  1201. Message(type_e_ordinal_expr_expected);
  1202. end;
  1203. {$endif i386}
  1204. end
  1205. { variable }
  1206. else
  1207. begin
  1208. { we have to be able to take the address of the absolute
  1209. expression
  1210. }
  1211. valid_for_addr(pt,true);
  1212. { remove subscriptn before checking for loadn }
  1213. hp:=pt;
  1214. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  1215. begin
  1216. { check for implicit dereferencing and reject it }
  1217. if (hp.nodetype in [subscriptn,vecn]) then
  1218. begin
  1219. if (tunarynode(hp).left.resultdef.typ in [pointerdef,classrefdef]) then
  1220. break;
  1221. { catch, e.g., 'var b: char absolute pchar_var[5];"
  1222. (pchar_var[5] is a pchar_2_string typeconv ->
  1223. the vecn only sees an array of char)
  1224. I don't know if all of these type conversions are
  1225. possible, but they're definitely all bad.
  1226. }
  1227. if (tunarynode(hp).left.nodetype=typeconvn) and
  1228. (ttypeconvnode(tunarynode(hp).left).convtype in
  1229. [tc_pchar_2_string,tc_pointer_2_array,
  1230. tc_intf_2_string,tc_intf_2_guid,
  1231. tc_dynarray_2_variant,tc_interface_2_variant,
  1232. tc_array_2_dynarray]) then
  1233. break;
  1234. if (tunarynode(hp).left.resultdef.typ=stringdef) and
  1235. not(tstringdef(tunarynode(hp).left.resultdef).stringtype in [st_shortstring,st_longstring]) then
  1236. break;
  1237. if (tunarynode(hp).left.resultdef.typ=objectdef) and
  1238. (tobjectdef(tunarynode(hp).left.resultdef).objecttype<>odt_object) then
  1239. break;
  1240. if is_dynamic_array(tunarynode(hp).left.resultdef) then
  1241. break;
  1242. end;
  1243. hp:=tunarynode(hp).left;
  1244. end;
  1245. if (hp.nodetype=loadn) then
  1246. begin
  1247. { we should check the result type of loadn }
  1248. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym]) then
  1249. Message(parser_e_absolute_only_to_var_or_const);
  1250. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  1251. abssym.fileinfo:=vs.fileinfo;
  1252. abssym.abstyp:=tovar;
  1253. abssym.ref:=node_to_propaccesslist(pt);
  1254. { if the sizes are different, can't be a regvar since you }
  1255. { can't be "absolute upper 8 bits of a register" (except }
  1256. { if its a record field of the same size of a record }
  1257. { regvar, but in that case pt.resultdef.size will have }
  1258. { the same size since it refers to the field and not to }
  1259. { the whole record -- which is why we use pt and not hp) }
  1260. { we can't take the size of an open array }
  1261. if is_open_array(pt.resultdef) or
  1262. (vs.vardef.size <> pt.resultdef.size) then
  1263. make_not_regable(pt,[ra_addr_regable]);
  1264. end
  1265. else
  1266. Message(parser_e_absolute_only_to_var_or_const);
  1267. end;
  1268. pt.free;
  1269. { replace old varsym with the new absolutevarsym }
  1270. if assigned(abssym) then
  1271. begin
  1272. st:=vs.owner;
  1273. vs.owner.Delete(vs);
  1274. st.insert(abssym);
  1275. sc[0]:=abssym;
  1276. end;
  1277. end;
  1278. var
  1279. sc : TFPObjectList;
  1280. vs : tabstractvarsym;
  1281. hdef : tdef;
  1282. i : longint;
  1283. semicoloneaten,
  1284. allowdefaultvalue,
  1285. hasdefaultvalue : boolean;
  1286. hintsymoptions : tsymoptions;
  1287. deprecatedmsg : pshortstring;
  1288. old_block_type : tblock_type;
  1289. sectionname : ansistring;
  1290. begin
  1291. old_block_type:=block_type;
  1292. block_type:=bt_var;
  1293. { Force an expected ID error message }
  1294. if not (token in [_ID,_CASE,_END]) then
  1295. consume(_ID);
  1296. { read vars }
  1297. sc:=TFPObjectList.create(false);
  1298. while (token=_ID) do
  1299. begin
  1300. semicoloneaten:=false;
  1301. hasdefaultvalue:=false;
  1302. allowdefaultvalue:=true;
  1303. sc.clear;
  1304. repeat
  1305. if (token = _ID) then
  1306. begin
  1307. case symtablestack.top.symtabletype of
  1308. localsymtable :
  1309. vs:=tlocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  1310. staticsymtable,
  1311. globalsymtable :
  1312. begin
  1313. vs:=tstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  1314. if vd_threadvar in options then
  1315. include(vs.varoptions,vo_is_thread_var);
  1316. end;
  1317. else
  1318. internalerror(200411064);
  1319. end;
  1320. sc.add(vs);
  1321. symtablestack.top.insert(vs);
  1322. end;
  1323. consume(_ID);
  1324. until not try_to_consume(_COMMA);
  1325. { read variable type def }
  1326. block_type:=bt_var_type;
  1327. consume(_COLON);
  1328. {$ifdef gpc_mode}
  1329. if (m_gpc in current_settings.modeswitches) and
  1330. (token=_ID) and
  1331. (orgpattern='__asmname__') then
  1332. read_gpc_name(sc);
  1333. {$endif}
  1334. read_anon_type(hdef,false);
  1335. maybe_guarantee_record_typesym(hdef,symtablestack.top);
  1336. for i:=0 to sc.count-1 do
  1337. begin
  1338. vs:=tabstractvarsym(sc[i]);
  1339. vs.vardef:=hdef;
  1340. end;
  1341. block_type:=bt_var;
  1342. { Process procvar directives }
  1343. if maybe_parse_proc_directives(hdef) then
  1344. semicoloneaten:=true;
  1345. { check for absolute }
  1346. if try_to_consume(_ABSOLUTE) then
  1347. begin
  1348. read_absolute(sc);
  1349. allowdefaultvalue:=false;
  1350. end;
  1351. { Check for EXTERNAL etc directives before a semicolon }
  1352. if (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) then
  1353. begin
  1354. read_public_and_external_sc(sc);
  1355. allowdefaultvalue:=false;
  1356. semicoloneaten:=true;
  1357. end;
  1358. { try to parse the hint directives }
  1359. hintsymoptions:=[];
  1360. deprecatedmsg:=nil;
  1361. try_consume_hintdirective(hintsymoptions,deprecatedmsg);
  1362. for i:=0 to sc.count-1 do
  1363. begin
  1364. vs:=tabstractvarsym(sc[i]);
  1365. vs.symoptions := vs.symoptions + hintsymoptions;
  1366. if deprecatedmsg<>nil then
  1367. vs.deprecatedmsg:=stringdup(deprecatedmsg^);
  1368. end;
  1369. stringdispose(deprecatedmsg);
  1370. { Handling of Delphi typed const = initialized vars }
  1371. if allowdefaultvalue and
  1372. (token=_EQ) and
  1373. not(m_tp7 in current_settings.modeswitches) and
  1374. (symtablestack.top.symtabletype<>parasymtable) then
  1375. begin
  1376. { Add calling convention for procvar }
  1377. if (hdef.typ=procvardef) and
  1378. (hdef.typesym=nil) then
  1379. handle_calling_convention(tprocvardef(hdef));
  1380. read_default_value(sc);
  1381. hasdefaultvalue:=true;
  1382. end
  1383. else
  1384. begin
  1385. if not(semicoloneaten) then
  1386. consume(_SEMICOLON);
  1387. end;
  1388. { Support calling convention for procvars after semicolon }
  1389. if not(hasdefaultvalue) and
  1390. (hdef.typ=procvardef) and
  1391. (hdef.typesym=nil) then
  1392. begin
  1393. { Parse procvar directives after ; }
  1394. maybe_parse_proc_directives(hdef);
  1395. { Add calling convention for procvar }
  1396. handle_calling_convention(tprocvardef(hdef));
  1397. { Handling of Delphi typed const = initialized vars }
  1398. if (token=_EQ) and
  1399. not(m_tp7 in current_settings.modeswitches) and
  1400. (symtablestack.top.symtabletype<>parasymtable) then
  1401. begin
  1402. read_default_value(sc);
  1403. hasdefaultvalue:=true;
  1404. end;
  1405. end;
  1406. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1407. if (
  1408. (
  1409. (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) and
  1410. (m_cvar_support in current_settings.modeswitches)
  1411. ) or
  1412. (
  1413. (m_mac in current_settings.modeswitches) and
  1414. (
  1415. (cs_external_var in current_settings.localswitches) or
  1416. (cs_externally_visible in current_settings.localswitches)
  1417. )
  1418. )
  1419. ) then
  1420. read_public_and_external_sc(sc);
  1421. { try to parse a section directive }
  1422. if (target_info.system in systems_allow_section) and
  1423. (symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) and
  1424. (idtoken=_SECTION) then
  1425. begin
  1426. try_consume_sectiondirective(sectionname);
  1427. if sectionname<>'' then
  1428. begin
  1429. for i:=0 to sc.count-1 do
  1430. begin
  1431. vs:=tabstractvarsym(sc[i]);
  1432. if (vs.varoptions *[vo_is_external,vo_is_weak_external])<>[] then
  1433. Message(parser_e_externals_no_section);
  1434. if vs.typ<>staticvarsym then
  1435. Message(parser_e_section_no_locals);
  1436. tstaticvarsym(vs).section:=sectionname;
  1437. include(vs.varoptions, vo_has_section);
  1438. end;
  1439. end;
  1440. end;
  1441. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1442. for i:=0 to sc.count-1 do
  1443. begin
  1444. vs:=tabstractvarsym(sc[i]);
  1445. if (vs.typ=staticvarsym) and
  1446. not(vo_is_typed_const in vs.varoptions) and
  1447. not(vo_is_external in vs.varoptions) then
  1448. cnodeutils.insertbssdata(tstaticvarsym(vs));
  1449. end;
  1450. end;
  1451. block_type:=old_block_type;
  1452. { free the list }
  1453. sc.free;
  1454. end;
  1455. procedure read_record_fields(options:Tvar_dec_options);
  1456. var
  1457. sc : TFPObjectList;
  1458. i : longint;
  1459. hs,sorg : string;
  1460. hdef,casetype : tdef;
  1461. { maxsize contains the max. size of a variant }
  1462. { startvarrec contains the start of the variant part of a record }
  1463. maxsize, startvarrecsize : longint;
  1464. usedalign,
  1465. maxalignment,startvarrecalign,
  1466. maxpadalign, startpadalign: shortint;
  1467. pt : tnode;
  1468. fieldvs : tfieldvarsym;
  1469. hstaticvs : tstaticvarsym;
  1470. vs : tabstractvarsym;
  1471. srsym : tsym;
  1472. srsymtable : TSymtable;
  1473. visibility : tvisibility;
  1474. recst : tabstractrecordsymtable;
  1475. recstlist : tfpobjectlist;
  1476. unionsymtable : trecordsymtable;
  1477. offset : longint;
  1478. uniondef : trecorddef;
  1479. hintsymoptions : tsymoptions;
  1480. deprecatedmsg : pshortstring;
  1481. semicoloneaten: boolean;
  1482. {$if defined(powerpc) or defined(powerpc64)}
  1483. tempdef: tdef;
  1484. is_first_type: boolean;
  1485. {$endif powerpc or powerpc64}
  1486. old_block_type: tblock_type;
  1487. begin
  1488. old_block_type:=block_type;
  1489. block_type:=bt_var;
  1490. recst:=tabstractrecordsymtable(symtablestack.top);
  1491. {$if defined(powerpc) or defined(powerpc64)}
  1492. is_first_type:=true;
  1493. {$endif powerpc or powerpc64}
  1494. { Force an expected ID error message }
  1495. if not (token in [_ID,_CASE,_END]) then
  1496. consume(_ID);
  1497. { read vars }
  1498. sc:=TFPObjectList.create(false);
  1499. recstlist:=TFPObjectList.create(false);;
  1500. while (token=_ID) and
  1501. not(((vd_object in options) or
  1502. ((vd_record in options) and (m_advanced_records in current_settings.modeswitches))) and
  1503. ((idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT]) or
  1504. ((m_final_fields in current_settings.modeswitches) and
  1505. (idtoken=_FINAL)))) do
  1506. begin
  1507. visibility:=symtablestack.top.currentvisibility;
  1508. semicoloneaten:=false;
  1509. sc.clear;
  1510. repeat
  1511. sorg:=orgpattern;
  1512. if token=_ID then
  1513. begin
  1514. vs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1515. sc.add(vs);
  1516. recst.insert(vs);
  1517. end;
  1518. consume(_ID);
  1519. until not try_to_consume(_COMMA);
  1520. if m_delphi in current_settings.modeswitches then
  1521. block_type:=bt_var_type
  1522. else
  1523. block_type:=old_block_type;
  1524. consume(_COLON);
  1525. { Don't search for types where they can't be:
  1526. types can be only in objects, classes and records.
  1527. This just speedup the search a bit. }
  1528. recstlist.count:=0;
  1529. if not is_class_or_object(tdef(recst.defowner)) and
  1530. not is_record(tdef(recst.defowner)) and
  1531. not is_java_class_or_interface(tdef(recst.defowner)) then
  1532. begin
  1533. recstlist.add(recst);
  1534. symtablestack.pop(recst);
  1535. end;
  1536. read_anon_type(hdef,false);
  1537. maybe_guarantee_record_typesym(hdef,symtablestack.top);
  1538. block_type:=bt_var;
  1539. { allow only static fields reference to struct where they are declared }
  1540. if not (vd_class in options) and
  1541. (is_object(hdef) or is_record(hdef)) and
  1542. is_owned_by(tabstractrecorddef(recst.defowner),tabstractrecorddef(hdef)) then
  1543. begin
  1544. Message1(type_e_type_is_not_completly_defined, tabstractrecorddef(hdef).RttiName);
  1545. { for error recovery or compiler will crash later }
  1546. hdef:=generrordef;
  1547. end;
  1548. { restore stack }
  1549. for i:=recstlist.count-1 downto 0 do
  1550. begin
  1551. recst:=tabstractrecordsymtable(recstlist[i]);
  1552. symtablestack.push(recst);
  1553. end;
  1554. { Process procvar directives }
  1555. if maybe_parse_proc_directives(hdef) then
  1556. semicoloneaten:=true;
  1557. {$if defined(powerpc) or defined(powerpc64)}
  1558. { from gcc/gcc/config/rs6000/rs6000.h:
  1559. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1560. /* Return the alignment of a struct based on the Macintosh PowerPC
  1561. alignment rules. In general the alignment of a struct is
  1562. determined by the greatest alignment of its elements. However, the
  1563. PowerPC rules cause the alignment of a struct to peg at word
  1564. alignment except when the first field has greater than word
  1565. (32-bit) alignment, in which case the alignment is determined by
  1566. the alignment of the first field. */
  1567. }
  1568. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1569. is_first_type and
  1570. (symtablestack.top.symtabletype=recordsymtable) and
  1571. (trecordsymtable(symtablestack.top).usefieldalignment=C_alignment) then
  1572. begin
  1573. tempdef:=hdef;
  1574. while tempdef.typ=arraydef do
  1575. tempdef:=tarraydef(tempdef).elementdef;
  1576. if tempdef.typ<>recorddef then
  1577. maxpadalign:=tempdef.alignment
  1578. else
  1579. maxpadalign:=trecorddef(tempdef).padalignment;
  1580. if (maxpadalign>4) and
  1581. (maxpadalign>trecordsymtable(symtablestack.top).padalignment) then
  1582. trecordsymtable(symtablestack.top).padalignment:=maxpadalign;
  1583. is_first_type:=false;
  1584. end;
  1585. {$endif powerpc or powerpc64}
  1586. { types that use init/final are not allowed in variant parts, but
  1587. classes are allowed }
  1588. if (variantrecordlevel>0) then
  1589. if is_managed_type(hdef) then
  1590. Message(parser_e_cant_use_inittable_here)
  1591. else
  1592. if hdef.typ=undefineddef then
  1593. Message(parser_e_cant_use_type_parameters_here);
  1594. { try to parse the hint directives }
  1595. hintsymoptions:=[];
  1596. deprecatedmsg:=nil;
  1597. try_consume_hintdirective(hintsymoptions,deprecatedmsg);
  1598. { update variable type and hints }
  1599. for i:=0 to sc.count-1 do
  1600. begin
  1601. fieldvs:=tfieldvarsym(sc[i]);
  1602. fieldvs.vardef:=hdef;
  1603. { insert any additional hint directives }
  1604. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1605. if deprecatedmsg<>nil then
  1606. fieldvs.deprecatedmsg:=stringdup(deprecatedmsg^);
  1607. end;
  1608. stringdispose(deprecatedmsg);
  1609. { Records and objects can't have default values }
  1610. { for a record there doesn't need to be a ; before the END or ) }
  1611. if not(token in [_END,_RKLAMMER]) and
  1612. not(semicoloneaten) then
  1613. consume(_SEMICOLON);
  1614. { Parse procvar directives after ; }
  1615. maybe_parse_proc_directives(hdef);
  1616. { Add calling convention for procvar }
  1617. if (hdef.typ=procvardef) and
  1618. (hdef.typesym=nil) then
  1619. handle_calling_convention(tprocvardef(hdef));
  1620. if (vd_object in options) then
  1621. begin
  1622. { if it is not a class var section and token=STATIC then it is a class field too }
  1623. if not (vd_class in options) and try_to_consume(_STATIC) then
  1624. begin
  1625. consume(_SEMICOLON);
  1626. include(options, vd_class);
  1627. end;
  1628. { Fields in Java classes/interfaces can have a separately
  1629. specified external name }
  1630. if is_java_class_or_interface(tdef(recst.defowner)) and
  1631. (oo_is_external in tobjectdef(recst.defowner).objectoptions) then
  1632. try_read_field_external_sc(sc);
  1633. end;
  1634. if (visibility=vis_published) and
  1635. not(is_class(hdef)) then
  1636. begin
  1637. Message(parser_e_cant_publish_that);
  1638. visibility:=vis_public;
  1639. end;
  1640. if (visibility=vis_published) and
  1641. not(oo_can_have_published in tobjectdef(hdef).objectoptions) and
  1642. not(m_delphi in current_settings.modeswitches) then
  1643. begin
  1644. Message(parser_e_only_publishable_classes_can_be_published);
  1645. visibility:=vis_public;
  1646. end;
  1647. if vd_class in options then
  1648. begin
  1649. { add static flag and staticvarsyms }
  1650. for i:=0 to sc.count-1 do
  1651. begin
  1652. fieldvs:=tfieldvarsym(sc[i]);
  1653. fieldvs.visibility:=visibility;
  1654. hstaticvs:=make_field_static(recst,fieldvs);
  1655. if vd_final in options then
  1656. hstaticvs.varspez:=vs_final;
  1657. end;
  1658. end;
  1659. if vd_final in options then
  1660. begin
  1661. { add final flag }
  1662. for i:=0 to sc.count-1 do
  1663. begin
  1664. fieldvs:=tfieldvarsym(sc[i]);
  1665. fieldvs.varspez:=vs_final;
  1666. end;
  1667. end;
  1668. { Generate field in the recordsymtable }
  1669. for i:=0 to sc.count-1 do
  1670. begin
  1671. fieldvs:=tfieldvarsym(sc[i]);
  1672. { static data fields are already inserted in the globalsymtable }
  1673. if not(sp_static in fieldvs.symoptions) then
  1674. recst.addfield(fieldvs,visibility);
  1675. end;
  1676. end;
  1677. recstlist.free;
  1678. if m_delphi in current_settings.modeswitches then
  1679. block_type:=bt_var_type
  1680. else
  1681. block_type:=old_block_type;
  1682. { Check for Case }
  1683. if (vd_record in options) and
  1684. try_to_consume(_CASE) then
  1685. begin
  1686. maxsize:=0;
  1687. maxalignment:=0;
  1688. maxpadalign:=0;
  1689. { including a field declaration? }
  1690. fieldvs:=nil;
  1691. sorg:=orgpattern;
  1692. hs:=pattern;
  1693. searchsym(hs,srsym,srsymtable);
  1694. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1695. begin
  1696. consume(_ID);
  1697. consume(_COLON);
  1698. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1699. symtablestack.top.insert(fieldvs);
  1700. end;
  1701. read_anon_type(casetype,true);
  1702. block_type:=bt_var;
  1703. if assigned(fieldvs) then
  1704. begin
  1705. fieldvs.vardef:=casetype;
  1706. recst.addfield(fieldvs,recst.currentvisibility);
  1707. end;
  1708. if not(is_ordinal(casetype))
  1709. {$ifndef cpu64bitaddr}
  1710. or is_64bitint(casetype)
  1711. {$endif cpu64bitaddr}
  1712. then
  1713. Message(type_e_ordinal_expr_expected);
  1714. consume(_OF);
  1715. UnionSymtable:=trecordsymtable.create('',current_settings.packrecords);
  1716. UnionDef:=trecorddef.create('',unionsymtable);
  1717. uniondef.isunion:=true;
  1718. startvarrecsize:=UnionSymtable.datasize;
  1719. { align the bitpacking to the next byte }
  1720. UnionSymtable.datasize:=startvarrecsize;
  1721. startvarrecalign:=UnionSymtable.fieldalignment;
  1722. startpadalign:=Unionsymtable.padalignment;
  1723. symtablestack.push(UnionSymtable);
  1724. repeat
  1725. repeat
  1726. pt:=comp_expr(true,false);
  1727. if not(pt.nodetype=ordconstn) then
  1728. Message(parser_e_illegal_expression);
  1729. if try_to_consume(_POINTPOINT) then
  1730. pt:=crangenode.create(pt,comp_expr(true,false));
  1731. pt.free;
  1732. if token=_COMMA then
  1733. consume(_COMMA)
  1734. else
  1735. break;
  1736. until false;
  1737. if m_delphi in current_settings.modeswitches then
  1738. block_type:=bt_var_type
  1739. else
  1740. block_type:=old_block_type;
  1741. consume(_COLON);
  1742. { read the vars }
  1743. consume(_LKLAMMER);
  1744. inc(variantrecordlevel);
  1745. if token<>_RKLAMMER then
  1746. read_record_fields([vd_record]);
  1747. dec(variantrecordlevel);
  1748. consume(_RKLAMMER);
  1749. { calculates maximal variant size }
  1750. maxsize:=max(maxsize,unionsymtable.datasize);
  1751. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1752. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1753. { the items of the next variant are overlayed }
  1754. unionsymtable.datasize:=startvarrecsize;
  1755. unionsymtable.fieldalignment:=startvarrecalign;
  1756. unionsymtable.padalignment:=startpadalign;
  1757. if (token<>_END) and (token<>_RKLAMMER) then
  1758. consume(_SEMICOLON)
  1759. else
  1760. break;
  1761. until (token=_END) or (token=_RKLAMMER);
  1762. symtablestack.pop(UnionSymtable);
  1763. { at last set the record size to that of the biggest variant }
  1764. unionsymtable.datasize:=maxsize;
  1765. unionsymtable.fieldalignment:=maxalignment;
  1766. unionsymtable.addalignmentpadding;
  1767. {$if defined(powerpc) or defined(powerpc64)}
  1768. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1769. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1770. is_first_type and
  1771. (recst.usefieldalignment=C_alignment) and
  1772. (maxpadalign>recst.padalignment) then
  1773. recst.padalignment:=maxpadalign;
  1774. {$endif powerpc or powerpc64}
  1775. { Align the offset where the union symtable is added }
  1776. case recst.usefieldalignment of
  1777. { allow the unionsymtable to be aligned however it wants }
  1778. { (within the global min/max limits) }
  1779. 0, { default }
  1780. C_alignment:
  1781. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  1782. { 1 byte alignment if we are bitpacked }
  1783. bit_alignment:
  1784. usedalign:=1;
  1785. mac68k_alignment:
  1786. usedalign:=2;
  1787. { otherwise alignment at the packrecords alignment of the }
  1788. { current record }
  1789. else
  1790. usedalign:=used_align(recst.fieldalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1791. end;
  1792. offset:=align(recst.datasize,usedalign);
  1793. recst.datasize:=offset+unionsymtable.datasize;
  1794. if unionsymtable.recordalignment>recst.fieldalignment then
  1795. recst.fieldalignment:=unionsymtable.recordalignment;
  1796. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1797. uniondef.owner.deletedef(uniondef);
  1798. end;
  1799. { free the list }
  1800. sc.free;
  1801. {$ifdef powerpc}
  1802. is_first_type := false;
  1803. {$endif powerpc}
  1804. block_type:=old_block_type;
  1805. end;
  1806. end.