pdecvar.pas 76 KB

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