pdecvar.pas 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  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,
  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.insert(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.insert(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.insert(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.insert(hreadparavs);
  380. sc.add(hreadparavs);
  381. consume(_ID);
  382. until not try_to_consume(_COMMA);
  383. if try_to_consume(_COLON) then
  384. begin
  385. if try_to_consume(_ARRAY) then
  386. begin
  387. consume(_OF);
  388. { define range and type of range }
  389. hdef:=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.insert(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);
  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.insert(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. if writeprocdef.proctypeoption=potype_propsetter then
  816. writeprocdef.register_def;
  817. end;
  818. result:=p;
  819. end;
  820. function maybe_parse_proc_directives(def:tdef):boolean;
  821. var
  822. newtype : ttypesym;
  823. begin
  824. result:=false;
  825. { Process procvar directives before = and ; }
  826. if (def.typ=procvardef) and
  827. (def.typesym=nil) and
  828. check_proc_directive(true) then
  829. begin
  830. newtype:=ctypesym.create('unnamed',def);
  831. parse_var_proc_directives(tsym(newtype));
  832. newtype.typedef:=nil;
  833. def.typesym:=nil;
  834. newtype.free;
  835. result:=true;
  836. end;
  837. end;
  838. const
  839. variantrecordlevel : longint = 0;
  840. procedure read_public_and_external_sc(sc:TFPObjectList);
  841. var
  842. vs: tabstractvarsym;
  843. begin
  844. { only allowed for one var }
  845. vs:=tabstractvarsym(sc[0]);
  846. if sc.count>1 then
  847. Message1(parser_e_directive_only_one_var,arraytokeninfo[idtoken].str);
  848. read_public_and_external(vs);
  849. end;
  850. procedure read_public_and_external(vs: tabstractvarsym);
  851. var
  852. is_dll,
  853. is_far,
  854. is_cdecl,
  855. is_external_var,
  856. is_weak_external,
  857. is_public_var : boolean;
  858. dll_name,section_name,
  859. C_name,mangledname : string;
  860. begin
  861. { only allowed for one var }
  862. { only allow external and public on global symbols }
  863. if vs.typ<>staticvarsym then
  864. begin
  865. Message(parser_e_no_local_var_external);
  866. exit;
  867. end;
  868. { defaults }
  869. is_dll:=false;
  870. is_far:=false;
  871. is_cdecl:=false;
  872. is_external_var:=false;
  873. is_public_var:=false;
  874. section_name := '';
  875. dll_name := '';
  876. C_name:=vs.realname;
  877. { macpas specific handling due to some switches}
  878. if (m_mac in current_settings.modeswitches) then
  879. begin
  880. if (cs_external_var in current_settings.localswitches) then
  881. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  882. is_cdecl:=true;
  883. is_external_var:=true;
  884. end
  885. else if (cs_externally_visible in current_settings.localswitches) then
  886. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  887. is_cdecl:=true;
  888. is_public_var:=true;
  889. end;
  890. end;
  891. { cdecl }
  892. if try_to_consume(_CVAR) then
  893. begin
  894. consume(_SEMICOLON);
  895. is_cdecl:=true;
  896. end;
  897. { external }
  898. is_weak_external:=try_to_consume(_WEAKEXTERNAL);
  899. if is_weak_external or
  900. try_to_consume(_EXTERNAL) then
  901. begin
  902. is_external_var:=true;
  903. { near/far? }
  904. if target_info.system in systems_allow_external_far_var then
  905. begin
  906. if try_to_consume(_FAR) then
  907. is_far:=true
  908. else if try_to_consume(_NEAR) then
  909. is_far:=false;
  910. end;
  911. if (idtoken<>_NAME) and (token<>_SEMICOLON) then
  912. begin
  913. is_dll:=true;
  914. dll_name:=get_stringconst;
  915. if ExtractFileExt(dll_name)='' then
  916. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  917. end;
  918. if not(is_cdecl) and try_to_consume(_NAME) then
  919. C_name:=get_stringconst;
  920. consume(_SEMICOLON);
  921. end;
  922. { export or public }
  923. if idtoken in [_EXPORT,_PUBLIC] then
  924. begin
  925. consume(_ID);
  926. if is_external_var then
  927. Message(parser_e_not_external_and_export)
  928. else
  929. is_public_var:=true;
  930. if try_to_consume(_NAME) then
  931. C_name:=get_stringconst;
  932. if (target_info.system in systems_allow_section_no_semicolon) and
  933. (vs.typ=staticvarsym) and
  934. try_to_consume (_SECTION) then
  935. section_name:=get_stringconst;
  936. consume(_SEMICOLON);
  937. end;
  938. { Windows uses an indirect reference using import tables }
  939. if is_dll and
  940. (target_info.system in systems_all_windows) then
  941. include(vs.varoptions,vo_is_dll_var);
  942. { This can only happen if vs.typ=staticvarsym }
  943. if section_name<>'' then
  944. begin
  945. tstaticvarsym(vs).section:=section_name;
  946. include(vs.varoptions,vo_has_section);
  947. end;
  948. { Add C _ prefix }
  949. if is_cdecl or
  950. (
  951. is_dll and
  952. (target_info.system in systems_darwin)
  953. ) then
  954. C_Name := target_info.Cprefix+C_Name;
  955. if is_public_var then
  956. begin
  957. include(vs.varoptions,vo_is_public);
  958. vs.varregable := vr_none;
  959. { mark as referenced }
  960. inc(vs.refs);
  961. end;
  962. mangledname:=C_name;
  963. { now we can insert it in the import lib if its a dll, or
  964. add it to the externals }
  965. if is_external_var then
  966. begin
  967. if vo_is_typed_const in vs.varoptions then
  968. Message(parser_e_initialized_not_for_external);
  969. include(vs.varoptions,vo_is_external);
  970. if is_far then
  971. include(vs.varoptions,vo_is_far);
  972. if (is_weak_external) then
  973. begin
  974. if not(target_info.system in systems_weak_linking) then
  975. message(parser_e_weak_external_not_supported);
  976. include(vs.varoptions,vo_is_weak_external);
  977. end;
  978. vs.varregable := vr_none;
  979. if is_dll then
  980. begin
  981. if target_info.system in (systems_all_windows + systems_nativent +
  982. [system_i386_emx, system_i386_os2]) then
  983. mangledname:=make_dllmangledname(dll_name,C_name,0,pocall_none);
  984. current_module.AddExternalImport(dll_name,C_Name,mangledname,0,true,false);
  985. end
  986. else
  987. if tf_has_dllscanner in target_info.flags then
  988. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  989. end;
  990. { Set the assembler name }
  991. tstaticvarsym(vs).set_mangledbasename(mangledname);
  992. tstaticvarsym(vs).set_mangledname(mangledname);
  993. end;
  994. procedure try_consume_sectiondirective(var asection: ansistring);
  995. begin
  996. if idtoken=_SECTION then
  997. begin
  998. consume(_ID);
  999. asection:=get_stringconst;
  1000. consume(_SEMICOLON);
  1001. end;
  1002. end;
  1003. procedure try_read_field_external(vs: tabstractvarsym);
  1004. var
  1005. extname: string;
  1006. begin
  1007. if try_to_consume(_EXTERNAL) then
  1008. begin
  1009. consume(_NAME);
  1010. extname:=get_stringconst;
  1011. tfieldvarsym(vs).set_externalname(extname);
  1012. consume(_SEMICOLON);
  1013. end;
  1014. end;
  1015. procedure try_read_field_external_sc(sc:TFPObjectList);
  1016. var
  1017. vs: tabstractvarsym;
  1018. begin
  1019. { only allowed for one var }
  1020. vs:=tabstractvarsym(sc[0]);
  1021. if sc.count>1 then
  1022. Message1(parser_e_directive_only_one_var,arraytokeninfo[idtoken].str);
  1023. try_read_field_external(vs);
  1024. end;
  1025. procedure read_var_decls(options:Tvar_dec_options;out had_generic:boolean);
  1026. procedure read_default_value(sc : TFPObjectList);
  1027. var
  1028. vs : tabstractnormalvarsym;
  1029. tcsym : tstaticvarsym;
  1030. templist : tasmlist;
  1031. begin
  1032. vs:=tabstractnormalvarsym(sc[0]);
  1033. if sc.count>1 then
  1034. Message(parser_e_initialized_only_one_var);
  1035. if vo_is_thread_var in vs.varoptions then
  1036. Message(parser_e_initialized_not_for_threadvar);
  1037. consume(_EQ);
  1038. case vs.typ of
  1039. localvarsym :
  1040. begin
  1041. tcsym:=cstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  1042. include(tcsym.symoptions,sp_internal);
  1043. symtablestack.top.insert(tcsym);
  1044. templist:=tasmlist.create;
  1045. read_typed_const(templist,tcsym,false);
  1046. { in case of a generic routine, this initialisation value is not
  1047. used, and will be re-parsed during specialisations (and the
  1048. current version is not type-correct and hence breaks code
  1049. generation for LLVM) }
  1050. if not parse_generic then
  1051. begin
  1052. vs.defaultconstsym:=tcsym;
  1053. current_asmdata.asmlists[al_typedconsts].concatlist(templist);
  1054. end;
  1055. templist.free;
  1056. end;
  1057. staticvarsym :
  1058. begin
  1059. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs),false);
  1060. end;
  1061. else
  1062. internalerror(200611051);
  1063. end;
  1064. vs.varstate:=vs_initialised;
  1065. end;
  1066. {$ifdef gpc_mode}
  1067. procedure read_gpc_name(sc : TFPObjectList);
  1068. var
  1069. vs : tabstractnormalvarsym;
  1070. C_Name : string;
  1071. begin
  1072. consume(_ID);
  1073. C_Name:=get_stringconst;
  1074. vs:=tabstractnormalvarsym(sc[0]);
  1075. if sc.count>1 then
  1076. Message(parser_e_directive_only_one_var,'ABSOLUTE');
  1077. if vs.typ=staticvarsym then
  1078. begin
  1079. tstaticvarsym(vs).set_mangledname(C_Name);
  1080. include(vs.varoptions,vo_is_external);
  1081. end
  1082. else
  1083. Message(parser_e_no_local_var_external);
  1084. end;
  1085. {$endif}
  1086. procedure read_absolute(sc : TFPObjectList);
  1087. var
  1088. vs : tabstractvarsym;
  1089. abssym : tabsolutevarsym;
  1090. pt,hp : tnode;
  1091. st : tsymtable;
  1092. {$if defined(i386) or defined(i8086)}
  1093. tmpaddr : int64;
  1094. {$endif defined(i386) or defined(i8086)}
  1095. begin
  1096. abssym:=nil;
  1097. { only allowed for one var }
  1098. vs:=tabstractvarsym(sc[0]);
  1099. if sc.count>1 then
  1100. Message1(parser_e_directive_only_one_var,'ABSOLUTE');
  1101. if vo_is_typed_const in vs.varoptions then
  1102. Message(parser_e_initialized_not_for_external);
  1103. { parse the rest }
  1104. pt:=expr(true);
  1105. { check allowed absolute types }
  1106. if (pt.nodetype=stringconstn) or
  1107. (is_constcharnode(pt)) then
  1108. begin
  1109. abssym:=cabsolutevarsym.create(vs.realname,vs.vardef);
  1110. abssym.fileinfo:=vs.fileinfo;
  1111. if pt.nodetype=stringconstn then
  1112. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  1113. else
  1114. abssym.asmname:=stringdup(chr(tordconstnode(pt).value.svalue));
  1115. consume(token);
  1116. abssym.abstyp:=toasm;
  1117. end
  1118. { address }
  1119. else if is_constintnode(pt) then
  1120. begin
  1121. abssym:=cabsolutevarsym.create(vs.realname,vs.vardef);
  1122. abssym.fileinfo:=vs.fileinfo;
  1123. abssym.abstyp:=toaddr;
  1124. {$ifndef cpu64bitaddr}
  1125. { on 64 bit systems, abssym.addroffset is a qword and hence this
  1126. test is useless (value is a 64 bit entity) and will always fail
  1127. for positive values (since int64(high(abssym.addroffset))=-1
  1128. }
  1129. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  1130. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  1131. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(abssym.addroffset)),tostr(high(abssym.addroffset)))
  1132. else
  1133. {$endif}
  1134. abssym.addroffset:=Tordconstnode(pt).value.svalue;
  1135. {$if defined(i386) or defined(i8086)}
  1136. tcpuabsolutevarsym(abssym).absseg:=false;
  1137. if (target_info.system in [system_i386_go32v2,system_i386_watcom,system_i8086_msdos,system_i8086_win16,system_i8086_embedded]) and
  1138. try_to_consume(_COLON) then
  1139. begin
  1140. pt.free;
  1141. pt:=expr(true);
  1142. if is_constintnode(pt) then
  1143. begin
  1144. {$if defined(i8086)}
  1145. tcpuabsolutevarsym(abssym).addrsegment:=abssym.addroffset;
  1146. tmpaddr:=tordconstnode(pt).value.svalue;
  1147. if (tmpaddr<int64(low(abssym.addroffset))) or
  1148. (tmpaddr>int64(high(abssym.addroffset))) then
  1149. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(abssym.addroffset)),tostr(high(abssym.addroffset)))
  1150. else
  1151. abssym.addroffset:=tmpaddr;
  1152. {$elseif defined(i386)}
  1153. tmpaddr:=abssym.addroffset shl 4+tordconstnode(pt).value.svalue;
  1154. if (tmpaddr<int64(low(abssym.addroffset))) or
  1155. (tmpaddr>int64(high(abssym.addroffset))) then
  1156. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(abssym.addroffset)),tostr(high(abssym.addroffset)))
  1157. else
  1158. abssym.addroffset:=tmpaddr;
  1159. {$endif}
  1160. tcpuabsolutevarsym(abssym).absseg:=true;
  1161. end
  1162. else
  1163. Message(type_e_ordinal_expr_expected);
  1164. end;
  1165. {$endif i386 or i8086}
  1166. end
  1167. { variable }
  1168. else
  1169. begin
  1170. { we have to be able to take the address of the absolute
  1171. expression
  1172. }
  1173. valid_for_addr(pt,true);
  1174. { remove subscriptn before checking for loadn }
  1175. hp:=pt;
  1176. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  1177. begin
  1178. { check for implicit dereferencing and reject it }
  1179. if (hp.nodetype in [subscriptn,vecn]) then
  1180. begin
  1181. if (tunarynode(hp).left.resultdef.typ in [pointerdef,classrefdef]) then
  1182. break;
  1183. { catch, e.g., 'var b: char absolute pchar_var[5];"
  1184. (pchar_var[5] is a pchar_2_string typeconv ->
  1185. the vecn only sees an array of char)
  1186. I don't know if all of these type conversions are
  1187. possible, but they're definitely all bad.
  1188. }
  1189. if (tunarynode(hp).left.nodetype=typeconvn) and
  1190. (ttypeconvnode(tunarynode(hp).left).convtype in
  1191. [tc_pchar_2_string,tc_pointer_2_array,
  1192. tc_intf_2_string,tc_intf_2_guid,
  1193. tc_dynarray_2_variant,tc_interface_2_variant,
  1194. tc_array_2_dynarray]) then
  1195. break;
  1196. if (tunarynode(hp).left.resultdef.typ=stringdef) and
  1197. not(tstringdef(tunarynode(hp).left.resultdef).stringtype in [st_shortstring,st_longstring]) then
  1198. break;
  1199. if (tunarynode(hp).left.resultdef.typ=objectdef) and
  1200. (tobjectdef(tunarynode(hp).left.resultdef).objecttype<>odt_object) then
  1201. break;
  1202. if is_dynamic_array(tunarynode(hp).left.resultdef) then
  1203. break;
  1204. end;
  1205. hp:=tunarynode(hp).left;
  1206. end;
  1207. if (hp.nodetype=loadn) then
  1208. begin
  1209. { we should check the result type of loadn }
  1210. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym,absolutevarsym]) then
  1211. Message(parser_e_absolute_only_to_var_or_const);
  1212. abssym:=cabsolutevarsym.create(vs.realname,vs.vardef);
  1213. abssym.fileinfo:=vs.fileinfo;
  1214. abssym.abstyp:=tovar;
  1215. abssym.ref:=node_to_propaccesslist(pt);
  1216. { if the sizes are different, can't be a regvar since you }
  1217. { can't be "absolute upper 8 bits of a register" (except }
  1218. { if its a record field of the same size of a record }
  1219. { regvar, but in that case pt.resultdef.size will have }
  1220. { the same size since it refers to the field and not to }
  1221. { the whole record -- which is why we use pt and not hp) }
  1222. { we can't take the size of an open array }
  1223. if is_open_array(pt.resultdef) or
  1224. (vs.vardef.size <> pt.resultdef.size) then
  1225. make_not_regable(pt,[ra_addr_regable]);
  1226. end
  1227. else
  1228. Message(parser_e_absolute_only_to_var_or_const);
  1229. end;
  1230. pt.free;
  1231. { replace old varsym with the new absolutevarsym }
  1232. if assigned(abssym) then
  1233. begin
  1234. st:=vs.owner;
  1235. vs.owner.Delete(vs);
  1236. st.insert(abssym);
  1237. sc[0]:=abssym;
  1238. end;
  1239. end;
  1240. var
  1241. sc : TFPObjectList;
  1242. vs : tabstractvarsym;
  1243. hdef : tdef;
  1244. i : longint;
  1245. first,
  1246. isgeneric,
  1247. semicoloneaten,
  1248. allowdefaultvalue,
  1249. hasdefaultvalue : boolean;
  1250. hintsymoptions : tsymoptions;
  1251. deprecatedmsg : pshortstring;
  1252. old_block_type : tblock_type;
  1253. sectionname : ansistring;
  1254. tmp_filepos,
  1255. old_current_filepos : tfileposinfo;
  1256. begin
  1257. old_block_type:=block_type;
  1258. block_type:=bt_var;
  1259. { Force an expected ID error message }
  1260. if not (token in [_ID,_CASE,_END]) then
  1261. consume(_ID);
  1262. { read vars }
  1263. sc:=TFPObjectList.create(false);
  1264. first:=true;
  1265. had_generic:=false;
  1266. vs:=nil;
  1267. fillchar(tmp_filepos,sizeof(tmp_filepos),0);
  1268. while (token=_ID) do
  1269. begin
  1270. semicoloneaten:=false;
  1271. hasdefaultvalue:=false;
  1272. allowdefaultvalue:=true;
  1273. sc.clear;
  1274. repeat
  1275. if (token = _ID) then
  1276. begin
  1277. isgeneric:=(vd_check_generic in options) and
  1278. not (m_delphi in current_settings.modeswitches) and
  1279. (idtoken=_GENERIC);
  1280. case symtablestack.top.symtabletype of
  1281. localsymtable :
  1282. vs:=clocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  1283. staticsymtable,
  1284. globalsymtable :
  1285. begin
  1286. vs:=cstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  1287. if vd_threadvar in options then
  1288. include(vs.varoptions,vo_is_thread_var);
  1289. end;
  1290. else
  1291. internalerror(200411064);
  1292. end;
  1293. sc.add(vs);
  1294. if isgeneric then
  1295. tmp_filepos:=current_filepos;
  1296. end
  1297. else
  1298. isgeneric:=false;
  1299. consume(_ID);
  1300. { when the first variable had been read the next declaration could be
  1301. a "generic procedure", "generic function" or
  1302. "generic class (function/procedure)" }
  1303. if not first
  1304. and isgeneric
  1305. and (sc.count=1)
  1306. and (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  1307. begin
  1308. vs.free;
  1309. sc.clear;
  1310. had_generic:=true;
  1311. break;
  1312. end
  1313. else
  1314. begin
  1315. vs.register_sym;
  1316. if isgeneric then
  1317. begin
  1318. { ensure correct error position }
  1319. old_current_filepos:=current_filepos;
  1320. current_filepos:=tmp_filepos;
  1321. symtablestack.top.insert(vs);
  1322. current_filepos:=old_current_filepos;
  1323. end
  1324. else
  1325. symtablestack.top.insert(vs);
  1326. end;
  1327. until not try_to_consume(_COMMA);
  1328. if had_generic then
  1329. break;
  1330. { read variable type def }
  1331. block_type:=bt_var_type;
  1332. consume(_COLON);
  1333. {$ifdef gpc_mode}
  1334. if (m_gpc in current_settings.modeswitches) and
  1335. (token=_ID) and
  1336. (orgpattern='__asmname__') then
  1337. read_gpc_name(sc);
  1338. {$endif}
  1339. read_anon_type(hdef,false);
  1340. maybe_guarantee_record_typesym(hdef,symtablestack.top);
  1341. for i:=0 to sc.count-1 do
  1342. begin
  1343. vs:=tabstractvarsym(sc[i]);
  1344. vs.vardef:=hdef;
  1345. end;
  1346. block_type:=bt_var;
  1347. { Process procvar directives }
  1348. if maybe_parse_proc_directives(hdef) then
  1349. semicoloneaten:=true;
  1350. { check for absolute }
  1351. if try_to_consume(_ABSOLUTE) then
  1352. begin
  1353. read_absolute(sc);
  1354. allowdefaultvalue:=false;
  1355. end;
  1356. { Check for EXTERNAL etc directives before a semicolon }
  1357. if (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) or (idtoken = _WEAKEXTERNAL) then
  1358. begin
  1359. read_public_and_external_sc(sc);
  1360. allowdefaultvalue:=false;
  1361. semicoloneaten:=true;
  1362. end;
  1363. { try to parse the hint directives }
  1364. hintsymoptions:=[];
  1365. deprecatedmsg:=nil;
  1366. try_consume_hintdirective(hintsymoptions,deprecatedmsg);
  1367. for i:=0 to sc.count-1 do
  1368. begin
  1369. vs:=tabstractvarsym(sc[i]);
  1370. vs.symoptions := vs.symoptions + hintsymoptions;
  1371. if deprecatedmsg<>nil then
  1372. vs.deprecatedmsg:=stringdup(deprecatedmsg^);
  1373. end;
  1374. stringdispose(deprecatedmsg);
  1375. { Handling of Delphi typed const = initialized vars }
  1376. if allowdefaultvalue and
  1377. (token=_EQ) and
  1378. not(m_tp7 in current_settings.modeswitches) and
  1379. (symtablestack.top.symtabletype<>parasymtable) then
  1380. begin
  1381. { Add calling convention for procvar }
  1382. if (hdef.typ=procvardef) and
  1383. (hdef.typesym=nil) then
  1384. handle_calling_convention(tprocvardef(hdef),hcc_default_actions_intf);
  1385. read_default_value(sc);
  1386. hasdefaultvalue:=true;
  1387. end
  1388. else
  1389. begin
  1390. if not(semicoloneaten) then
  1391. consume(_SEMICOLON);
  1392. end;
  1393. { Support calling convention for procvars after semicolon }
  1394. if not(hasdefaultvalue) and
  1395. (hdef.typ=procvardef) and
  1396. (hdef.typesym=nil) then
  1397. begin
  1398. { Parse procvar directives after ; }
  1399. maybe_parse_proc_directives(hdef);
  1400. { Add calling convention for procvar }
  1401. handle_calling_convention(tprocvardef(hdef),hcc_default_actions_intf);
  1402. { Handling of Delphi typed const = initialized vars }
  1403. if (token=_EQ) and
  1404. not(m_tp7 in current_settings.modeswitches) and
  1405. (symtablestack.top.symtabletype<>parasymtable) then
  1406. begin
  1407. read_default_value(sc);
  1408. hasdefaultvalue:=true;
  1409. end;
  1410. end;
  1411. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1412. if (
  1413. (
  1414. ((idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) or (idtoken = _WEAKEXTERNAL)) and
  1415. (m_cvar_support in current_settings.modeswitches)
  1416. ) or
  1417. (
  1418. (m_mac in current_settings.modeswitches) and
  1419. (
  1420. (cs_external_var in current_settings.localswitches) or
  1421. (cs_externally_visible in current_settings.localswitches)
  1422. )
  1423. )
  1424. ) then
  1425. read_public_and_external_sc(sc);
  1426. { try to parse a section directive }
  1427. if (target_info.system in systems_allow_section) and
  1428. (symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) and
  1429. (idtoken=_SECTION) then
  1430. begin
  1431. try_consume_sectiondirective(sectionname);
  1432. if sectionname<>'' then
  1433. begin
  1434. for i:=0 to sc.count-1 do
  1435. begin
  1436. vs:=tabstractvarsym(sc[i]);
  1437. if (vs.varoptions *[vo_is_external,vo_is_weak_external])<>[] then
  1438. Message(parser_e_externals_no_section);
  1439. if vs.typ<>staticvarsym then
  1440. Message(parser_e_section_no_locals);
  1441. tstaticvarsym(vs).section:=sectionname;
  1442. include(vs.varoptions, vo_has_section);
  1443. end;
  1444. end;
  1445. end;
  1446. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1447. for i:=0 to sc.count-1 do
  1448. begin
  1449. vs:=tabstractvarsym(sc[i]);
  1450. if (vs.typ=staticvarsym) and
  1451. not(vo_is_typed_const in vs.varoptions) and
  1452. not(vo_is_external in vs.varoptions) then
  1453. cnodeutils.insertbssdata(tstaticvarsym(vs));
  1454. if vo_is_public in vs.varoptions then
  1455. current_module.add_public_asmsym(vs.mangledname,AB_GLOBAL,AT_DATA);
  1456. end;
  1457. first:=false;
  1458. end;
  1459. block_type:=old_block_type;
  1460. { free the list }
  1461. sc.free;
  1462. end;
  1463. function check_allowed_for_var_or_const(def:tdef;allowdynarray:boolean):boolean;
  1464. var
  1465. stowner,tmpdef : tdef;
  1466. st : tsymtable;
  1467. begin
  1468. result:=true;
  1469. st:=symtablestack.top;
  1470. if not (st.symtabletype in [recordsymtable,objectsymtable]) then
  1471. exit;
  1472. stowner:=tdef(st.defowner);
  1473. while assigned(stowner) and (stowner.typ in [objectdef,recorddef]) do
  1474. begin
  1475. if def.typ=arraydef then
  1476. begin
  1477. tmpdef:=def;
  1478. while (tmpdef.typ=arraydef) do
  1479. begin
  1480. { dynamic arrays are allowed in certain cases }
  1481. if allowdynarray and (ado_IsDynamicArray in tarraydef(tmpdef).arrayoptions) then
  1482. begin
  1483. tmpdef:=nil;
  1484. break;
  1485. end;
  1486. tmpdef:=tarraydef(tmpdef).elementdef;
  1487. end;
  1488. end
  1489. else
  1490. tmpdef:=def;
  1491. if assigned(tmpdef) and
  1492. (is_object(tmpdef) or is_record(tmpdef)) and
  1493. is_owned_by(tabstractrecorddef(stowner),tabstractrecorddef(tmpdef)) then
  1494. begin
  1495. Message1(type_e_type_is_not_completly_defined,tabstractrecorddef(tmpdef).RttiName);
  1496. result:=false;
  1497. break;
  1498. end;
  1499. stowner:=tdef(stowner.owner.defowner);
  1500. end;
  1501. end;
  1502. procedure read_record_fields(options:Tvar_dec_options; reorderlist: TFPObjectList; variantdesc : ppvariantrecdesc;out had_generic:boolean);
  1503. var
  1504. sc : TFPObjectList;
  1505. i : longint;
  1506. hs,sorg : string;
  1507. hdef,casetype : tdef;
  1508. { maxsize contains the max. size of a variant }
  1509. { startvarrec contains the start of the variant part of a record }
  1510. maxsize, startvarrecsize : longint;
  1511. usedalign,
  1512. maxalignment,startvarrecalign,
  1513. maxpadalign, startpadalign: shortint;
  1514. pt : tnode;
  1515. fieldvs : tfieldvarsym;
  1516. hstaticvs : tstaticvarsym;
  1517. vs : tabstractvarsym;
  1518. srsym : tsym;
  1519. srsymtable : TSymtable;
  1520. visibility : tvisibility;
  1521. recst : tabstractrecordsymtable;
  1522. unionsymtable : trecordsymtable;
  1523. offset : longint;
  1524. uniondef : trecorddef;
  1525. hintsymoptions : tsymoptions;
  1526. deprecatedmsg : pshortstring;
  1527. hadgendummy,
  1528. semicoloneaten,
  1529. removeclassoption: boolean;
  1530. {$if defined(powerpc) or defined(powerpc64)}
  1531. tempdef: tdef;
  1532. is_first_type: boolean;
  1533. {$endif powerpc or powerpc64}
  1534. old_block_type: tblock_type;
  1535. tmpidx: Integer;
  1536. begin
  1537. old_block_type:=block_type;
  1538. block_type:=bt_var;
  1539. recst:=tabstractrecordsymtable(symtablestack.top);
  1540. {$if defined(powerpc) or defined(powerpc64)}
  1541. is_first_type:=true;
  1542. {$endif powerpc or powerpc64}
  1543. { Force an expected ID error message }
  1544. if not (token in [_ID,_CASE,_END]) then
  1545. consume(_ID);
  1546. { read vars }
  1547. sc:=TFPObjectList.create(false);
  1548. removeclassoption:=false;
  1549. had_generic:=false;
  1550. while (token=_ID) and
  1551. not(((vd_object in options) or
  1552. ((vd_record in options) and (m_advanced_records in current_settings.modeswitches))) and
  1553. ((idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT]) or
  1554. ((m_final_fields in current_settings.modeswitches) and
  1555. (idtoken=_FINAL)))) do
  1556. begin
  1557. visibility:=symtablestack.top.currentvisibility;
  1558. semicoloneaten:=false;
  1559. sc.clear;
  1560. repeat
  1561. sorg:=orgpattern;
  1562. if token=_ID then
  1563. begin
  1564. vs:=cfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1565. { normally the visibility is set via addfield, but sometimes
  1566. we collect symbols so we can add them in a batch of
  1567. potentially mixed visibility, and then the individual
  1568. symbols need to have their visibility already set }
  1569. vs.visibility:=visibility;
  1570. if (vd_check_generic in options) and (idtoken=_GENERIC) then
  1571. had_generic:=true;
  1572. end
  1573. else
  1574. vs:=nil;
  1575. consume(_ID);
  1576. if assigned(vs) and
  1577. (
  1578. not had_generic or
  1579. not (token in [_PROCEDURE,_FUNCTION,_CLASS])
  1580. ) then
  1581. begin
  1582. vs.register_sym;
  1583. sc.add(vs);
  1584. recst.insert(vs);
  1585. had_generic:=false;
  1586. end
  1587. else
  1588. vs.free;
  1589. until not try_to_consume(_COMMA);
  1590. if m_delphi in current_settings.modeswitches then
  1591. block_type:=bt_var_type
  1592. else
  1593. block_type:=old_block_type;
  1594. if had_generic and (sc.count=0) then
  1595. break;
  1596. consume(_COLON);
  1597. read_anon_type(hdef,false);
  1598. maybe_guarantee_record_typesym(hdef,symtablestack.top);
  1599. block_type:=bt_var;
  1600. { allow only static fields reference to struct where they are declared }
  1601. if not (vd_class in options) then
  1602. begin
  1603. if not check_allowed_for_var_or_const(hdef,true) then
  1604. { for error recovery or compiler will crash later }
  1605. hdef:=generrordef;
  1606. end;
  1607. { field type is a generic param so set a flag in the struct }
  1608. if assigned(hdef.typesym) and (sp_generic_para in hdef.typesym.symoptions) then
  1609. include(current_structdef.defoptions,df_has_generic_fields);
  1610. { Process procvar directives }
  1611. if maybe_parse_proc_directives(hdef) then
  1612. semicoloneaten:=true;
  1613. {$if defined(powerpc) or defined(powerpc64)}
  1614. { from gcc/gcc/config/rs6000/rs6000.h:
  1615. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1616. /* Return the alignment of a struct based on the Macintosh PowerPC
  1617. alignment rules. In general the alignment of a struct is
  1618. determined by the greatest alignment of its elements. However, the
  1619. PowerPC rules cause the alignment of a struct to peg at word
  1620. alignment except when the first field has greater than word
  1621. (32-bit) alignment, in which case the alignment is determined by
  1622. the alignment of the first field. */
  1623. }
  1624. { TODO: check whether this is also for AIX }
  1625. if (target_info.abi in [abi_powerpc_aix,abi_powerpc_darwin]) and
  1626. is_first_type and
  1627. (symtablestack.top.symtabletype=recordsymtable) and
  1628. (trecordsymtable(symtablestack.top).usefieldalignment=C_alignment) then
  1629. begin
  1630. tempdef:=hdef;
  1631. while tempdef.typ=arraydef do
  1632. tempdef:=tarraydef(tempdef).elementdef;
  1633. if tempdef.typ<>recorddef then
  1634. maxpadalign:=tempdef.alignment
  1635. else
  1636. maxpadalign:=trecorddef(tempdef).padalignment;
  1637. if (maxpadalign>4) and
  1638. (maxpadalign>trecordsymtable(symtablestack.top).padalignment) then
  1639. trecordsymtable(symtablestack.top).padalignment:=maxpadalign;
  1640. is_first_type:=false;
  1641. end;
  1642. {$endif powerpc or powerpc64}
  1643. { types that use init/final are not allowed in variant parts, but
  1644. classes are allowed }
  1645. if (variantrecordlevel>0) then
  1646. if is_managed_type(hdef) then
  1647. Message(parser_e_cant_use_inittable_here)
  1648. else
  1649. if hdef.typ=undefineddef then
  1650. Message(parser_e_cant_use_type_parameters_here);
  1651. { try to parse the hint directives }
  1652. hintsymoptions:=[];
  1653. deprecatedmsg:=nil;
  1654. try_consume_hintdirective(hintsymoptions,deprecatedmsg);
  1655. { update variable type and hints }
  1656. for i:=0 to sc.count-1 do
  1657. begin
  1658. fieldvs:=tfieldvarsym(sc[i]);
  1659. fieldvs.vardef:=hdef;
  1660. { insert any additional hint directives }
  1661. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1662. if deprecatedmsg<>nil then
  1663. fieldvs.deprecatedmsg:=stringdup(deprecatedmsg^);
  1664. end;
  1665. stringdispose(deprecatedmsg);
  1666. { Records and objects can't have default values }
  1667. { for a record there doesn't need to be a ; before the END or ) }
  1668. if not(token in [_END,_RKLAMMER]) and
  1669. not(semicoloneaten) then
  1670. consume(_SEMICOLON);
  1671. { Parse procvar directives after ; }
  1672. maybe_parse_proc_directives(hdef);
  1673. { Add calling convention for procvar }
  1674. if (hdef.typ=procvardef) and
  1675. (hdef.typesym=nil) then
  1676. handle_calling_convention(tprocvardef(hdef),hcc_default_actions_intf);
  1677. if (vd_object in options) then
  1678. begin
  1679. { if it is not a class var section and token=STATIC then it is a class field too }
  1680. if not (vd_class in options) and try_to_consume(_STATIC) then
  1681. begin
  1682. consume(_SEMICOLON);
  1683. include(options,vd_class);
  1684. removeclassoption:=true;
  1685. end;
  1686. { Fields in Java classes/interfaces can have a separately
  1687. specified external name }
  1688. if is_java_class_or_interface(tdef(recst.defowner)) and
  1689. (oo_is_external in tobjectdef(recst.defowner).objectoptions) then
  1690. try_read_field_external_sc(sc);
  1691. end;
  1692. if (visibility=vis_published) and
  1693. not(is_class(hdef)) then
  1694. begin
  1695. MessagePos(tfieldvarsym(sc[0]).fileinfo,parser_e_cant_publish_that);
  1696. visibility:=vis_public;
  1697. end;
  1698. if (visibility=vis_published) and
  1699. not(oo_can_have_published in tobjectdef(hdef).objectoptions) and
  1700. not(m_delphi in current_settings.modeswitches) then
  1701. begin
  1702. MessagePos(tfieldvarsym(sc[0]).fileinfo,parser_e_only_publishable_classes_can_be_published);
  1703. visibility:=vis_public;
  1704. end;
  1705. if vd_class in options then
  1706. begin
  1707. { add static flag and staticvarsyms }
  1708. for i:=0 to sc.count-1 do
  1709. begin
  1710. fieldvs:=tfieldvarsym(sc[i]);
  1711. fieldvs.visibility:=visibility;
  1712. hstaticvs:=make_field_static(recst,fieldvs);
  1713. if vd_threadvar in options then
  1714. include(hstaticvs.varoptions,vo_is_thread_var);
  1715. if not parse_generic then
  1716. cnodeutils.insertbssdata(hstaticvs);
  1717. if vd_final in options then
  1718. hstaticvs.varspez:=vs_final;
  1719. end;
  1720. if removeclassoption then
  1721. begin
  1722. exclude(options,vd_class);
  1723. removeclassoption:=false;
  1724. end;
  1725. end;
  1726. if vd_final in options then
  1727. begin
  1728. { add final flag }
  1729. for i:=0 to sc.count-1 do
  1730. begin
  1731. fieldvs:=tfieldvarsym(sc[i]);
  1732. fieldvs.varspez:=vs_final;
  1733. end;
  1734. end;
  1735. if not(vd_canreorder in options) then
  1736. { add field(s) to the recordsymtable }
  1737. recst.addfieldlist(sc,false)
  1738. else
  1739. { we may reorder the fields before adding them to the symbol
  1740. table }
  1741. reorderlist.concatlistcopy(sc)
  1742. end;
  1743. if m_delphi in current_settings.modeswitches then
  1744. block_type:=bt_var_type
  1745. else
  1746. block_type:=old_block_type;
  1747. { Check for Case }
  1748. if (vd_record in options) and
  1749. try_to_consume(_CASE) then
  1750. begin
  1751. maxsize:=0;
  1752. maxalignment:=0;
  1753. maxpadalign:=0;
  1754. { already inside a variant record? if not, setup a new variantdesc chain }
  1755. if not(assigned(variantdesc)) then
  1756. variantdesc:=@trecorddef(trecordsymtable(recst).defowner).variantrecdesc;
  1757. { else just concat the info to the given one }
  1758. new(variantdesc^);
  1759. fillchar(variantdesc^^,sizeof(tvariantrecdesc),0);
  1760. { including a field declaration? }
  1761. fieldvs:=nil;
  1762. sorg:=orgpattern;
  1763. hs:=pattern;
  1764. searchsym(hs,srsym,srsymtable);
  1765. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1766. begin
  1767. consume(_ID);
  1768. consume(_COLON);
  1769. fieldvs:=cfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1770. variantdesc^^.variantselector:=fieldvs;
  1771. symtablestack.top.insert(fieldvs);
  1772. end;
  1773. read_anon_type(casetype,true);
  1774. block_type:=bt_var;
  1775. if assigned(fieldvs) then
  1776. begin
  1777. fieldvs.vardef:=casetype;
  1778. recst.addfield(fieldvs,recst.currentvisibility);
  1779. end;
  1780. if not(is_ordinal(casetype))
  1781. {$ifndef cpu64bitaddr}
  1782. or is_64bitint(casetype)
  1783. {$endif cpu64bitaddr}
  1784. then
  1785. Message(type_e_ordinal_expr_expected);
  1786. consume(_OF);
  1787. UnionSymtable:=trecordsymtable.create('',current_settings.packrecords,current_settings.alignment.recordalignmin);
  1788. UnionDef:=crecorddef.create('',unionsymtable);
  1789. uniondef.isunion:=true;
  1790. startvarrecsize:=UnionSymtable.datasize;
  1791. { align the bitpacking to the next byte }
  1792. UnionSymtable.datasize:=startvarrecsize;
  1793. startvarrecalign:=UnionSymtable.fieldalignment;
  1794. startpadalign:=Unionsymtable.padalignment;
  1795. symtablestack.push(UnionSymtable);
  1796. repeat
  1797. SetLength(variantdesc^^.branches,length(variantdesc^^.branches)+1);
  1798. fillchar(variantdesc^^.branches[high(variantdesc^^.branches)],
  1799. sizeof(variantdesc^^.branches[high(variantdesc^^.branches)]),0);
  1800. repeat
  1801. pt:=comp_expr([ef_accept_equal]);
  1802. if not(pt.nodetype=ordconstn) then
  1803. Message(parser_e_illegal_expression);
  1804. { iso pascal does not support ranges in variant record definitions }
  1805. if (([m_iso,m_extpas]*current_settings.modeswitches)=[]) and try_to_consume(_POINTPOINT) then
  1806. pt:=crangenode.create(pt,comp_expr([ef_accept_equal]))
  1807. else
  1808. begin
  1809. with variantdesc^^.branches[high(variantdesc^^.branches)] do
  1810. begin
  1811. SetLength(values,length(values)+1);
  1812. values[high(values)]:=tordconstnode(pt).value;
  1813. end;
  1814. end;
  1815. pt.free;
  1816. if token=_COMMA then
  1817. consume(_COMMA)
  1818. else
  1819. break;
  1820. until false;
  1821. if m_delphi in current_settings.modeswitches then
  1822. block_type:=bt_var_type
  1823. else
  1824. block_type:=old_block_type;
  1825. consume(_COLON);
  1826. { read the vars }
  1827. consume(_LKLAMMER);
  1828. inc(variantrecordlevel);
  1829. if token<>_RKLAMMER then
  1830. read_record_fields([vd_record],nil,@variantdesc^^.branches[high(variantdesc^^.branches)].nestedvariant,hadgendummy);
  1831. dec(variantrecordlevel);
  1832. consume(_RKLAMMER);
  1833. { calculates maximal variant size }
  1834. maxsize:=max(maxsize,unionsymtable.datasize);
  1835. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1836. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1837. { the items of the next variant are overlayed }
  1838. unionsymtable.datasize:=startvarrecsize;
  1839. unionsymtable.fieldalignment:=startvarrecalign;
  1840. unionsymtable.padalignment:=startpadalign;
  1841. if (token<>_END) and (token<>_RKLAMMER) then
  1842. consume(_SEMICOLON)
  1843. else
  1844. break;
  1845. until (token=_END) or (token=_RKLAMMER);
  1846. symtablestack.pop(UnionSymtable);
  1847. { at last set the record size to that of the biggest variant }
  1848. unionsymtable.datasize:=maxsize;
  1849. unionsymtable.fieldalignment:=maxalignment;
  1850. unionsymtable.addalignmentpadding;
  1851. {$if defined(powerpc) or defined(powerpc64)}
  1852. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1853. if (target_info.system in [system_powerpc_darwin, system_powerpc_macosclassic, system_powerpc64_darwin]) and
  1854. is_first_type and
  1855. (recst.usefieldalignment=C_alignment) and
  1856. (maxpadalign>recst.padalignment) then
  1857. recst.padalignment:=maxpadalign;
  1858. {$endif powerpc or powerpc64}
  1859. { Align the offset where the union symtable is added }
  1860. case recst.usefieldalignment of
  1861. { allow the unionsymtable to be aligned however it wants }
  1862. { (within the global min/max limits) }
  1863. 0, { default }
  1864. C_alignment:
  1865. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  1866. { 1 byte alignment if we are bitpacked }
  1867. bit_alignment:
  1868. usedalign:=1;
  1869. mac68k_alignment:
  1870. usedalign:=2;
  1871. { otherwise alignment at the packrecords alignment of the }
  1872. { current record }
  1873. else
  1874. usedalign:=used_align(recst.fieldalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1875. end;
  1876. offset:=align(recst.datasize,usedalign);
  1877. recst.datasize:=offset+unionsymtable.datasize;
  1878. if unionsymtable.recordalignment>recst.fieldalignment then
  1879. recst.fieldalignment:=unionsymtable.recordalignment;
  1880. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1881. uniondef.owner.deletedef(uniondef);
  1882. { this prevents a dangling pointer and use after free }
  1883. tmpidx:=current_module.deflist.IndexOfItem(uniondef,FromEnd);
  1884. if tmpidx<>-1 then
  1885. current_module.deflist[tmpidx]:=nil;
  1886. end;
  1887. { free the list }
  1888. sc.free;
  1889. {$ifdef powerpc}
  1890. is_first_type := false;
  1891. {$endif powerpc}
  1892. block_type:=old_block_type;
  1893. end;
  1894. end.