pdecvar.pas 82 KB

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