pdecvar.pas 84 KB

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