pdecvar.pas 79 KB

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