pdecvar.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Parses variable declarations. Used for var statement and record
  4. definitions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecvar;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symsym,symdef;
  23. type
  24. tvar_dec_option=(vd_record,vd_object,vd_threadvar);
  25. tvar_dec_options=set of tvar_dec_option;
  26. function read_property_dec(aclass:tobjectdef):tpropertysym;
  27. procedure read_var_decls(options:Tvar_dec_options);
  28. procedure read_record_fields(options:Tvar_dec_options);
  29. procedure read_public_and_external(vs: tabstractvarsym);
  30. implementation
  31. uses
  32. SysUtils,
  33. { common }
  34. cutils,cclasses,
  35. { global }
  36. globtype,globals,tokens,verbose,constexp,
  37. systems,
  38. { symtable }
  39. symconst,symbase,symtype,symtable,defutil,defcmp,
  40. fmodule,htypechk,
  41. { pass 1 }
  42. node,pass_1,aasmdata,
  43. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,nutils,
  44. { codegen }
  45. ncgutil,
  46. { parser }
  47. scanner,
  48. pbase,pexpr,ptype,ptconst,pdecsub,
  49. { link }
  50. import
  51. ;
  52. function read_property_dec(aclass:tobjectdef):tpropertysym;
  53. { convert a node tree to symlist and return the last
  54. symbol }
  55. function parse_symlist(pl:tpropaccesslist;var def:tdef):boolean;
  56. var
  57. idx : longint;
  58. sym : tsym;
  59. srsymtable : TSymtable;
  60. st : TSymtable;
  61. p : tnode;
  62. begin
  63. result:=true;
  64. def:=nil;
  65. if token=_ID then
  66. begin
  67. if assigned(aclass) then
  68. sym:=search_class_member(aclass,pattern)
  69. else
  70. searchsym(pattern,sym,srsymtable);
  71. if assigned(sym) then
  72. begin
  73. case sym.typ of
  74. fieldvarsym :
  75. begin
  76. if not(sp_private in current_object_option) then
  77. addsymref(sym);
  78. pl.addsym(sl_load,sym);
  79. def:=tfieldvarsym(sym).vardef;
  80. end;
  81. procsym :
  82. begin
  83. if not(sp_private in current_object_option) then
  84. addsymref(sym);
  85. pl.addsym(sl_call,sym);
  86. end;
  87. else
  88. begin
  89. Message1(parser_e_illegal_field_or_method,orgpattern);
  90. def:=generrordef;
  91. result:=false;
  92. end;
  93. end;
  94. end
  95. else
  96. begin
  97. Message1(parser_e_illegal_field_or_method,orgpattern);
  98. def:=generrordef;
  99. result:=false;
  100. end;
  101. consume(_ID);
  102. repeat
  103. case token of
  104. _ID,
  105. _SEMICOLON :
  106. begin
  107. break;
  108. end;
  109. _POINT :
  110. begin
  111. consume(_POINT);
  112. if assigned(def) then
  113. begin
  114. st:=def.GetSymtable(gs_record);
  115. if assigned(st) then
  116. begin
  117. sym:=tsym(st.Find(pattern));
  118. if not(assigned(sym)) and is_object(def) then
  119. sym:=search_class_member(tobjectdef(def),pattern);
  120. if assigned(sym) then
  121. begin
  122. pl.addsym(sl_subscript,sym);
  123. case sym.typ of
  124. fieldvarsym :
  125. def:=tfieldvarsym(sym).vardef;
  126. else
  127. begin
  128. Message1(sym_e_illegal_field,orgpattern);
  129. result:=false;
  130. end;
  131. end;
  132. end
  133. else
  134. begin
  135. Message1(sym_e_illegal_field,orgpattern);
  136. result:=false;
  137. end;
  138. end
  139. else
  140. begin
  141. Message(parser_e_invalid_qualifier);
  142. result:=false;
  143. end;
  144. end
  145. else
  146. begin
  147. Message(parser_e_invalid_qualifier);
  148. result:=false;
  149. end;
  150. consume(_ID);
  151. end;
  152. _LECKKLAMMER :
  153. begin
  154. consume(_LECKKLAMMER);
  155. repeat
  156. if def.typ=arraydef then
  157. begin
  158. idx:=0;
  159. p:=comp_expr(true);
  160. if (not codegenerror) then
  161. begin
  162. if (p.nodetype=ordconstn) then
  163. begin
  164. { type/range checking }
  165. inserttypeconv(p,tarraydef(def).rangedef);
  166. if (Tordconstnode(p).value<int64(low(longint))) or
  167. (Tordconstnode(p).value>int64(high(longint))) then
  168. message(parser_e_array_range_out_of_bounds)
  169. else
  170. idx:=Tordconstnode(p).value.svalue
  171. end
  172. else
  173. Message(type_e_ordinal_expr_expected)
  174. end;
  175. p.free;
  176. pl.addconst(sl_vec,idx,p.resultdef);
  177. def:=tarraydef(def).elementdef;
  178. end
  179. else
  180. begin
  181. Message(parser_e_invalid_qualifier);
  182. result:=false;
  183. end;
  184. until not try_to_consume(_COMMA);
  185. consume(_RECKKLAMMER);
  186. end;
  187. else
  188. begin
  189. Message(parser_e_ill_property_access_sym);
  190. result:=false;
  191. break;
  192. end;
  193. end;
  194. until false;
  195. end
  196. else
  197. begin
  198. Message(parser_e_ill_property_access_sym);
  199. result:=false;
  200. end;
  201. end;
  202. var
  203. sym : tsym;
  204. p : tpropertysym;
  205. overriden : tsym;
  206. varspez : tvarspez;
  207. hdef : tdef;
  208. arraytype : tdef;
  209. def : tdef;
  210. pt : tnode;
  211. sc : TFPObjectList;
  212. paranr : word;
  213. i : longint;
  214. ImplIntf : TImplementedInterface;
  215. found : boolean;
  216. hreadparavs,
  217. hparavs : tparavarsym;
  218. storedprocdef,
  219. readprocdef,
  220. writeprocdef : tprocvardef;
  221. begin
  222. { Generate temp procvardefs to search for matching read/write
  223. procedures. the readprocdef will store all definitions }
  224. paranr:=0;
  225. readprocdef:=tprocvardef.create(normal_function_level);
  226. writeprocdef:=tprocvardef.create(normal_function_level);
  227. storedprocdef:=tprocvardef.create(normal_function_level);
  228. { make it method pointers }
  229. if assigned(aclass) then
  230. begin
  231. include(readprocdef.procoptions,po_methodpointer);
  232. include(writeprocdef.procoptions,po_methodpointer);
  233. include(storedprocdef.procoptions,po_methodpointer);
  234. end;
  235. { method for stored must return boolean }
  236. storedprocdef.returndef:=booltype;
  237. if token<>_ID then
  238. begin
  239. consume(_ID);
  240. consume(_SEMICOLON);
  241. exit;
  242. end;
  243. { Generate propertysym and insert in symtablestack }
  244. p:=tpropertysym.create(orgpattern);
  245. symtablestack.top.insert(p);
  246. consume(_ID);
  247. { property parameters ? }
  248. if try_to_consume(_LECKKLAMMER) then
  249. begin
  250. if (sp_published in current_object_option) and
  251. not (m_delphi in current_settings.modeswitches) then
  252. Message(parser_e_cant_publish_that_property);
  253. { create a list of the parameters }
  254. symtablestack.push(readprocdef.parast);
  255. sc:=TFPObjectList.create(false);
  256. inc(testcurobject);
  257. repeat
  258. if try_to_consume(_VAR) then
  259. varspez:=vs_var
  260. else if try_to_consume(_CONST) then
  261. varspez:=vs_const
  262. else if (m_out in current_settings.modeswitches) and try_to_consume(_OUT) then
  263. varspez:=vs_out
  264. else
  265. varspez:=vs_value;
  266. sc.clear;
  267. repeat
  268. inc(paranr);
  269. hreadparavs:=tparavarsym.create(orgpattern,10*paranr,varspez,generrordef,[]);
  270. readprocdef.parast.insert(hreadparavs);
  271. sc.add(hreadparavs);
  272. consume(_ID);
  273. until not try_to_consume(_COMMA);
  274. if try_to_consume(_COLON) then
  275. begin
  276. if try_to_consume(_ARRAY) then
  277. begin
  278. consume(_OF);
  279. { define range and type of range }
  280. hdef:=tarraydef.create(0,-1,s32inttype);
  281. { define field type }
  282. single_type(arraytype,false);
  283. tarraydef(hdef).elementdef:=arraytype;
  284. end
  285. else
  286. single_type(hdef,false);
  287. end
  288. else
  289. hdef:=cformaltype;
  290. for i:=0 to sc.count-1 do
  291. begin
  292. hreadparavs:=tparavarsym(sc[i]);
  293. hreadparavs.vardef:=hdef;
  294. { also update the writeprocdef }
  295. hparavs:=tparavarsym.create(hreadparavs.realname,hreadparavs.paranr,vs_value,hdef,[]);
  296. writeprocdef.parast.insert(hparavs);
  297. end;
  298. until not try_to_consume(_SEMICOLON);
  299. sc.free;
  300. dec(testcurobject);
  301. symtablestack.pop(readprocdef.parast);
  302. consume(_RECKKLAMMER);
  303. { the parser need to know if a property has parameters, the
  304. index parameter doesn't count (PFV) }
  305. if paranr>0 then
  306. include(p.propoptions,ppo_hasparameters);
  307. end;
  308. { overriden property ? }
  309. { force property interface
  310. there is a property parameter
  311. a global property }
  312. if (token=_COLON) or (paranr>0) or (aclass=nil) then
  313. begin
  314. consume(_COLON);
  315. single_type(p.propdef,false);
  316. if (idtoken=_INDEX) then
  317. begin
  318. consume(_INDEX);
  319. pt:=comp_expr(true);
  320. { Only allow enum and integer indexes. Convert all integer
  321. values to s32int to be compatible with delphi, because the
  322. procedure matching requires equal parameters }
  323. if is_constnode(pt) and
  324. is_ordinal(pt.resultdef)
  325. {$ifndef cpu64bitaddr}
  326. and (not is_64bitint(pt.resultdef))
  327. {$endif cpu64bitaddr}
  328. then
  329. begin
  330. if is_integer(pt.resultdef) then
  331. inserttypeconv_internal(pt,s32inttype);
  332. p.index:=tordconstnode(pt).value.svalue;
  333. end
  334. else
  335. begin
  336. Message(parser_e_invalid_property_index_value);
  337. p.index:=0;
  338. end;
  339. p.indexdef:=pt.resultdef;
  340. include(p.propoptions,ppo_indexed);
  341. { concat a longint to the para templates }
  342. inc(paranr);
  343. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  344. readprocdef.parast.insert(hparavs);
  345. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  346. writeprocdef.parast.insert(hparavs);
  347. hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indexdef,[]);
  348. storedprocdef.parast.insert(hparavs);
  349. pt.free;
  350. end;
  351. end
  352. else
  353. begin
  354. { do an property override }
  355. overriden:=search_class_member(aclass.childof,p.name);
  356. if assigned(overriden) and
  357. (overriden.typ=propertysym) and
  358. not(is_dispinterface(aclass)) then
  359. begin
  360. p.overridenpropsym:=tpropertysym(overriden);
  361. { inherit all type related entries }
  362. p.indexdef:=tpropertysym(overriden).indexdef;
  363. p.propdef:=tpropertysym(overriden).propdef;
  364. p.index:=tpropertysym(overriden).index;
  365. p.default:=tpropertysym(overriden).default;
  366. p.propoptions:=tpropertysym(overriden).propoptions;
  367. end
  368. else
  369. begin
  370. p.propdef:=generrordef;
  371. message(parser_e_no_property_found_to_override);
  372. end;
  373. end;
  374. if ((sp_published in current_object_option) or is_dispinterface(aclass)) and
  375. not(p.propdef.is_publishable) then
  376. Message(parser_e_cant_publish_that_property);
  377. if not(is_dispinterface(aclass)) then
  378. begin
  379. if try_to_consume(_READ) then
  380. begin
  381. p.propaccesslist[palt_read].clear;
  382. if parse_symlist(p.propaccesslist[palt_read],def) then
  383. begin
  384. sym:=p.propaccesslist[palt_read].firstsym^.sym;
  385. case sym.typ of
  386. procsym :
  387. begin
  388. { read is function returning the type of the property }
  389. readprocdef.returndef:=p.propdef;
  390. { Insert hidden parameters }
  391. handle_calling_convention(readprocdef);
  392. { search procdefs matching readprocdef }
  393. { we ignore hidden stuff here because the property access symbol might have
  394. non default calling conventions which might change the hidden stuff;
  395. see tw3216.pp (FK) }
  396. p.propaccesslist[palt_read].procdef:=Tprocsym(sym).Find_procdef_bypara(readprocdef.paras,p.propdef,[cpo_allowdefaults,cpo_ignorehidden]);
  397. if not assigned(p.propaccesslist[palt_read].procdef) then
  398. Message(parser_e_ill_property_access_sym);
  399. end;
  400. fieldvarsym :
  401. begin
  402. if not assigned(def) then
  403. internalerror(200310071);
  404. if compare_defs(def,p.propdef,nothingn)>=te_equal then
  405. begin
  406. { property parameters are allowed if this is
  407. an indexed property, because the index is then
  408. the parameter.
  409. Note: In the help of Kylix it is written
  410. that it isn't allowed, but the compiler accepts it (PFV) }
  411. if (ppo_hasparameters in p.propoptions) then
  412. Message(parser_e_ill_property_access_sym);
  413. end
  414. else
  415. IncompatibleTypes(def,p.propdef);
  416. end;
  417. else
  418. Message(parser_e_ill_property_access_sym);
  419. end;
  420. end;
  421. end;
  422. if try_to_consume(_WRITE) then
  423. begin
  424. p.propaccesslist[palt_write].clear;
  425. if parse_symlist(p.propaccesslist[palt_write],def) then
  426. begin
  427. sym:=p.propaccesslist[palt_write].firstsym^.sym;
  428. case sym.typ of
  429. procsym :
  430. begin
  431. { write is a procedure with an extra value parameter
  432. of the of the property }
  433. writeprocdef.returndef:=voidtype;
  434. inc(paranr);
  435. hparavs:=tparavarsym.create('$value',10*paranr,vs_value,p.propdef,[]);
  436. writeprocdef.parast.insert(hparavs);
  437. { Insert hidden parameters }
  438. handle_calling_convention(writeprocdef);
  439. { search procdefs matching writeprocdef }
  440. p.propaccesslist[palt_write].procdef:=Tprocsym(sym).Find_procdef_bypara(writeprocdef.paras,writeprocdef.returndef,[cpo_allowdefaults]);
  441. if not assigned(p.propaccesslist[palt_write].procdef) then
  442. Message(parser_e_ill_property_access_sym);
  443. end;
  444. fieldvarsym :
  445. begin
  446. if not assigned(def) then
  447. internalerror(200310072);
  448. if compare_defs(def,p.propdef,nothingn)>=te_equal then
  449. begin
  450. { property parameters are allowed if this is
  451. an indexed property, because the index is then
  452. the parameter.
  453. Note: In the help of Kylix it is written
  454. that it isn't allowed, but the compiler accepts it (PFV) }
  455. if (ppo_hasparameters in p.propoptions) then
  456. Message(parser_e_ill_property_access_sym);
  457. end
  458. else
  459. IncompatibleTypes(def,p.propdef);
  460. end;
  461. else
  462. Message(parser_e_ill_property_access_sym);
  463. end;
  464. end;
  465. end;
  466. end
  467. else
  468. begin
  469. if try_to_consume(_READONLY) then
  470. begin
  471. end
  472. else if try_to_consume(_WRITEONLY) then
  473. begin
  474. end;
  475. if try_to_consume(_DISPID) then
  476. begin
  477. pt:=comp_expr(true);
  478. if is_constintnode(pt) then
  479. // tprocdef(pd).extnumber:=tordconstnode(pt).value
  480. else
  481. Message(parser_e_dispid_must_be_ord_const);
  482. pt.free;
  483. end;
  484. end;
  485. if assigned(aclass) and not(is_dispinterface(aclass)) then
  486. begin
  487. { ppo_stored is default on for not overriden properties }
  488. if not assigned(p.overridenpropsym) then
  489. include(p.propoptions,ppo_stored);
  490. if try_to_consume(_STORED) then
  491. begin
  492. include(p.propoptions,ppo_stored);
  493. p.propaccesslist[palt_stored].clear;
  494. case token of
  495. _ID:
  496. begin
  497. { in the case that idtoken=_DEFAULT }
  498. { we have to do nothing except }
  499. { setting ppo_stored, it's the same }
  500. { as stored true }
  501. if idtoken<>_DEFAULT then
  502. begin
  503. if parse_symlist(p.propaccesslist[palt_stored],def) then
  504. begin
  505. sym:=p.propaccesslist[palt_stored].firstsym^.sym;
  506. case sym.typ of
  507. procsym :
  508. begin
  509. { Insert hidden parameters }
  510. handle_calling_convention(storedprocdef);
  511. p.propaccesslist[palt_stored].procdef:=Tprocsym(sym).Find_procdef_bypara(storedprocdef.paras,storedprocdef.returndef,[cpo_allowdefaults,cpo_ignorehidden]);
  512. if not assigned(p.propaccesslist[palt_stored].procdef) then
  513. message(parser_e_ill_property_storage_sym);
  514. end;
  515. fieldvarsym :
  516. begin
  517. if not assigned(def) then
  518. internalerror(200310073);
  519. if (ppo_hasparameters in p.propoptions) or
  520. not(is_boolean(def)) then
  521. Message(parser_e_stored_property_must_be_boolean);
  522. end;
  523. else
  524. Message(parser_e_ill_property_access_sym);
  525. end;
  526. end;
  527. end;
  528. end;
  529. _FALSE:
  530. begin
  531. consume(_FALSE);
  532. exclude(p.propoptions,ppo_stored);
  533. end;
  534. _TRUE:
  535. begin
  536. p.default:=longint($80000000);
  537. consume(_TRUE);
  538. end;
  539. end;
  540. end;
  541. end;
  542. if try_to_consume(_DEFAULT) then
  543. begin
  544. if not(is_ordinal(p.propdef) or
  545. {$ifndef cpu64bitaddr}
  546. is_64bitint(p.propdef) or
  547. {$endif cpu64bitaddr}
  548. is_class(p.propdef) or
  549. is_single(p.propdef) or
  550. (p.propdef.typ in [classrefdef,pointerdef]) or
  551. is_smallset(p.propdef)
  552. ) or
  553. (
  554. (p.propdef.typ=arraydef) and
  555. (ppo_indexed in p.propoptions)
  556. ) or
  557. (ppo_hasparameters in p.propoptions) then
  558. begin
  559. Message(parser_e_property_cant_have_a_default_value);
  560. { Error recovery }
  561. pt:=comp_expr(true);
  562. pt.free;
  563. end
  564. else
  565. begin
  566. { Get the result of the default, the firstpass is
  567. needed to support values like -1 }
  568. pt:=comp_expr(true);
  569. if (p.propdef.typ=setdef) and
  570. (pt.nodetype=arrayconstructorn) then
  571. begin
  572. arrayconstructor_to_set(pt);
  573. do_typecheckpass(pt);
  574. end;
  575. inserttypeconv(pt,p.propdef);
  576. if not(is_constnode(pt)) then
  577. Message(parser_e_property_default_value_must_const);
  578. { Set default value }
  579. case pt.nodetype of
  580. setconstn :
  581. p.default:=plongint(tsetconstnode(pt).value_set)^;
  582. ordconstn :
  583. if (Tordconstnode(pt).value<int64(low(longint))) or
  584. (Tordconstnode(pt).value>int64(high(cardinal))) then
  585. message(parser_e_range_check_error)
  586. else
  587. p.default:=longint(tordconstnode(pt).value.svalue);
  588. niln :
  589. p.default:=0;
  590. realconstn:
  591. p.default:=longint(single(trealconstnode(pt).value_real));
  592. end;
  593. pt.free;
  594. end;
  595. end
  596. else if try_to_consume(_NODEFAULT) then
  597. begin
  598. p.default:=longint($80000000);
  599. end;
  600. { Parse possible "implements" keyword }
  601. if try_to_consume(_IMPLEMENTS) then
  602. begin
  603. single_type(def,false);
  604. if not(is_interface(def)) then
  605. message(parser_e_class_implements_must_be_interface);
  606. if is_interface(p.propdef) then
  607. begin
  608. if compare_defs(def,p.propdef,nothingn)<te_equal then
  609. begin
  610. message2(parser_e_implements_must_have_correct_type,def.GetTypeName,p.propdef.GetTypeName);
  611. exit;
  612. end;
  613. end
  614. else if is_class(p.propdef) then
  615. begin
  616. ImplIntf:=tobjectdef(p.propdef).find_implemented_interface(tobjectdef(def));
  617. if assigned(ImplIntf) then
  618. begin
  619. if compare_defs(ImplIntf.IntfDef,def,nothingn)<te_equal then
  620. begin
  621. message2(parser_e_implements_must_have_correct_type,ImplIntf.IntfDef.GetTypeName,def.GetTypeName);
  622. exit;
  623. end;
  624. end
  625. else
  626. begin
  627. message2(parser_e_class_doesnt_implement_interface,p.propdef.GetTypeName,def.GetTypeName);
  628. exit;
  629. end;
  630. end
  631. else
  632. begin
  633. message(parser_e_implements_must_be_class_or_interface);
  634. exit;
  635. end;
  636. if not assigned(p.propaccesslist[palt_read].firstsym) then
  637. begin
  638. message(parser_e_implements_must_read_specifier);
  639. exit;
  640. end;
  641. if assigned(p.propaccesslist[palt_write].firstsym) then
  642. begin
  643. message(parser_e_implements_must_not_have_write_specifier);
  644. exit;
  645. end;
  646. if assigned(p.propaccesslist[palt_stored].firstsym) then
  647. begin
  648. message(parser_e_implements_must_not_have_stored_specifier);
  649. exit;
  650. end;
  651. found:=false;
  652. for i:=0 to aclass.ImplementedInterfaces.Count-1 do
  653. begin
  654. ImplIntf:=TImplementedInterface(aclass.ImplementedInterfaces[i]);
  655. if compare_defs(def,ImplIntf.IntfDef,nothingn)>=te_equal then
  656. begin
  657. found:=true;
  658. break;
  659. end;
  660. end;
  661. if found then
  662. begin
  663. ImplIntf.ImplementsGetter:=p;
  664. case p.propaccesslist[palt_read].firstsym^.sym.typ of
  665. procsym :
  666. begin
  667. if (po_virtualmethod in tprocdef(p.propaccesslist[palt_read].procdef).procoptions) then
  668. ImplIntf.IType:=etVirtualMethodResult
  669. else
  670. ImplIntf.IType:=etStaticMethodResult;
  671. end;
  672. fieldvarsym :
  673. ImplIntf.IType:=etFieldValue;
  674. else
  675. internalerror(200802161);
  676. end;
  677. end
  678. else
  679. message1(parser_e_implements_uses_non_implemented_interface,def.GetTypeName);
  680. end;
  681. { remove temporary procvardefs }
  682. readprocdef.owner.deletedef(readprocdef);
  683. writeprocdef.owner.deletedef(writeprocdef);
  684. result:=p;
  685. end;
  686. function maybe_parse_proc_directives(def:tdef):boolean;
  687. var
  688. newtype : ttypesym;
  689. begin
  690. result:=false;
  691. { Process procvar directives before = and ; }
  692. if (def.typ=procvardef) and
  693. (def.typesym=nil) and
  694. check_proc_directive(true) then
  695. begin
  696. newtype:=ttypesym.create('unnamed',def);
  697. parse_var_proc_directives(tsym(newtype));
  698. newtype.typedef:=nil;
  699. def.typesym:=nil;
  700. newtype.free;
  701. result:=true;
  702. end;
  703. end;
  704. const
  705. variantrecordlevel : longint = 0;
  706. procedure read_public_and_external_sc(sc:TFPObjectList);
  707. var
  708. vs: tabstractvarsym;
  709. begin
  710. { only allowed for one var }
  711. vs:=tabstractvarsym(sc[0]);
  712. if sc.count>1 then
  713. Message(parser_e_absolute_only_one_var);
  714. read_public_and_external(vs);
  715. end;
  716. procedure read_public_and_external(vs: tabstractvarsym);
  717. var
  718. is_dll,
  719. is_cdecl,
  720. is_external_var,
  721. is_public_var : boolean;
  722. dll_name,
  723. C_name : string;
  724. begin
  725. { only allowed for one var }
  726. { only allow external and public on global symbols }
  727. if vs.typ<>staticvarsym then
  728. begin
  729. Message(parser_e_no_local_var_external);
  730. exit;
  731. end;
  732. { defaults }
  733. is_dll:=false;
  734. is_cdecl:=false;
  735. is_external_var:=false;
  736. is_public_var:=false;
  737. C_name:=vs.realname;
  738. { macpas specific handling due to some switches}
  739. if (m_mac in current_settings.modeswitches) then
  740. begin
  741. if (cs_external_var in current_settings.localswitches) then
  742. begin {The effect of this is the same as if cvar; external; has been given as directives.}
  743. is_cdecl:=true;
  744. is_external_var:=true;
  745. end
  746. else if (cs_externally_visible in current_settings.localswitches) then
  747. begin {The effect of this is the same as if cvar has been given as directives and it's made public.}
  748. is_cdecl:=true;
  749. is_public_var:=true;
  750. end;
  751. end;
  752. { cdecl }
  753. if try_to_consume(_CVAR) then
  754. begin
  755. consume(_SEMICOLON);
  756. is_cdecl:=true;
  757. end;
  758. { external }
  759. if try_to_consume(_EXTERNAL) then
  760. begin
  761. is_external_var:=true;
  762. if not is_cdecl then
  763. begin
  764. if idtoken<>_NAME then
  765. begin
  766. is_dll:=true;
  767. dll_name:=get_stringconst;
  768. if ExtractFileExt(dll_name)='' then
  769. dll_name:=ChangeFileExt(dll_name,target_info.sharedlibext);
  770. end;
  771. if try_to_consume(_NAME) then
  772. C_name:=get_stringconst;
  773. end;
  774. consume(_SEMICOLON);
  775. end;
  776. { export or public }
  777. if idtoken in [_EXPORT,_PUBLIC] then
  778. begin
  779. consume(_ID);
  780. if is_external_var then
  781. Message(parser_e_not_external_and_export)
  782. else
  783. is_public_var:=true;
  784. if try_to_consume(_NAME) then
  785. C_name:=get_stringconst;
  786. consume(_SEMICOLON);
  787. end;
  788. { Windows uses an indirect reference using import tables }
  789. if is_dll and
  790. (target_info.system in system_all_windows) then
  791. include(vs.varoptions,vo_is_dll_var);
  792. { Add C _ prefix }
  793. if is_cdecl or
  794. (
  795. is_dll and
  796. (target_info.system in systems_darwin)
  797. ) then
  798. C_Name := target_info.Cprefix+C_Name;
  799. if is_public_var then
  800. begin
  801. include(vs.varoptions,vo_is_public);
  802. vs.varregable := vr_none;
  803. { mark as referenced }
  804. inc(vs.refs);
  805. end;
  806. { now we can insert it in the import lib if its a dll, or
  807. add it to the externals }
  808. if is_external_var then
  809. begin
  810. if vo_is_typed_const in vs.varoptions then
  811. Message(parser_e_initialized_not_for_external);
  812. include(vs.varoptions,vo_is_external);
  813. vs.varregable := vr_none;
  814. if is_dll then
  815. current_module.AddExternalImport(dll_name,C_Name,0,true,false)
  816. else
  817. if tf_has_dllscanner in target_info.flags then
  818. current_module.dllscannerinputlist.Add(vs.mangledname,vs);
  819. end;
  820. { Set the assembler name }
  821. tstaticvarsym(vs).set_mangledname(C_Name);
  822. end;
  823. procedure read_var_decls(options:Tvar_dec_options);
  824. procedure read_default_value(sc : TFPObjectList);
  825. var
  826. vs : tabstractnormalvarsym;
  827. tcsym : tstaticvarsym;
  828. begin
  829. vs:=tabstractnormalvarsym(sc[0]);
  830. if sc.count>1 then
  831. Message(parser_e_initialized_only_one_var);
  832. if vo_is_thread_var in vs.varoptions then
  833. Message(parser_e_initialized_not_for_threadvar);
  834. consume(_EQUAL);
  835. case vs.typ of
  836. localvarsym :
  837. begin
  838. tcsym:=tstaticvarsym.create('$default'+vs.realname,vs_const,vs.vardef,[]);
  839. include(tcsym.symoptions,sp_internal);
  840. vs.defaultconstsym:=tcsym;
  841. symtablestack.top.insert(tcsym);
  842. read_typed_const(current_asmdata.asmlists[al_typedconsts],tcsym);
  843. end;
  844. staticvarsym :
  845. begin
  846. read_typed_const(current_asmdata.asmlists[al_typedconsts],tstaticvarsym(vs));
  847. end;
  848. else
  849. internalerror(200611051);
  850. end;
  851. vs.varstate:=vs_initialised;
  852. end;
  853. {$ifdef gpc_mode}
  854. procedure read_gpc_name(sc : TFPObjectList);
  855. var
  856. vs : tabstractnormalvarsym;
  857. C_Name : string;
  858. begin
  859. consume(_ID);
  860. C_Name:=get_stringconst;
  861. vs:=tabstractnormalvarsym(sc[0]);
  862. if sc.count>1 then
  863. Message(parser_e_absolute_only_one_var);
  864. if vs.typ=staticvarsym then
  865. begin
  866. tstaticvarsym(vs).set_mangledname(C_Name);
  867. include(vs.varoptions,vo_is_external);
  868. end
  869. else
  870. Message(parser_e_no_local_var_external);
  871. end;
  872. {$endif}
  873. procedure read_absolute(sc : TFPObjectList);
  874. var
  875. vs : tabstractvarsym;
  876. abssym : tabsolutevarsym;
  877. pt,hp : tnode;
  878. st : tsymtable;
  879. begin
  880. abssym:=nil;
  881. { only allowed for one var }
  882. vs:=tabstractvarsym(sc[0]);
  883. if sc.count>1 then
  884. Message(parser_e_absolute_only_one_var);
  885. if vo_is_typed_const in vs.varoptions then
  886. Message(parser_e_initialized_not_for_external);
  887. { parse the rest }
  888. pt:=expr;
  889. { check allowed absolute types }
  890. if (pt.nodetype=stringconstn) or
  891. (is_constcharnode(pt)) then
  892. begin
  893. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  894. abssym.fileinfo:=vs.fileinfo;
  895. if pt.nodetype=stringconstn then
  896. abssym.asmname:=stringdup(strpas(tstringconstnode(pt).value_str))
  897. else
  898. abssym.asmname:=stringdup(chr(tordconstnode(pt).value.svalue));
  899. consume(token);
  900. abssym.abstyp:=toasm;
  901. end
  902. { address }
  903. else if is_constintnode(pt) then
  904. begin
  905. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  906. abssym.fileinfo:=vs.fileinfo;
  907. abssym.abstyp:=toaddr;
  908. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  909. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  910. message(parser_e_range_check_error)
  911. else
  912. abssym.addroffset:=Tordconstnode(pt).value.svalue;
  913. {$ifdef i386}
  914. abssym.absseg:=false;
  915. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  916. try_to_consume(_COLON) then
  917. begin
  918. pt.free;
  919. pt:=expr;
  920. if is_constintnode(pt) then
  921. begin
  922. if (Tordconstnode(pt).value<int64(low(abssym.addroffset))) or
  923. (Tordconstnode(pt).value>int64(high(abssym.addroffset))) then
  924. message(parser_e_range_check_error)
  925. else
  926. abssym.addroffset:=abssym.addroffset shl 4+tordconstnode(pt).value.svalue;
  927. abssym.absseg:=true;
  928. end
  929. else
  930. Message(type_e_ordinal_expr_expected);
  931. end;
  932. {$endif i386}
  933. end
  934. { variable }
  935. else
  936. begin
  937. { remove subscriptn before checking for loadn }
  938. hp:=pt;
  939. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  940. hp:=tunarynode(hp).left;
  941. if (hp.nodetype=loadn) then
  942. begin
  943. { we should check the result type of loadn }
  944. if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,staticvarsym,localvarsym,paravarsym]) then
  945. Message(parser_e_absolute_only_to_var_or_const);
  946. abssym:=tabsolutevarsym.create(vs.realname,vs.vardef);
  947. abssym.fileinfo:=vs.fileinfo;
  948. abssym.abstyp:=tovar;
  949. abssym.ref:=node_to_propaccesslist(pt);
  950. { if the sizes are different, can't be a regvar since you }
  951. { can't be "absolute upper 8 bits of a register" (except }
  952. { if its a record field of the same size of a record }
  953. { regvar, but in that case pt.resultdef.size will have }
  954. { the same size since it refers to the field and not to }
  955. { the whole record -- which is why we use pt and not hp) }
  956. { we can't take the size of an open array }
  957. if is_open_array(pt.resultdef) or
  958. (vs.vardef.size <> pt.resultdef.size) then
  959. make_not_regable(pt,[ra_addr_regable]);
  960. end
  961. else
  962. Message(parser_e_absolute_only_to_var_or_const);
  963. end;
  964. pt.free;
  965. { replace old varsym with the new absolutevarsym }
  966. if assigned(abssym) then
  967. begin
  968. st:=vs.owner;
  969. vs.owner.Delete(vs);
  970. st.insert(abssym);
  971. sc[0]:=abssym;
  972. end;
  973. end;
  974. var
  975. sc : TFPObjectList;
  976. vs : tabstractvarsym;
  977. hdef : tdef;
  978. i : longint;
  979. semicoloneaten,
  980. allowdefaultvalue,
  981. hasdefaultvalue : boolean;
  982. old_current_object_option : tsymoptions;
  983. hintsymoptions : tsymoptions;
  984. old_block_type : tblock_type;
  985. begin
  986. old_current_object_option:=current_object_option;
  987. { all variables are public if not in a object declaration }
  988. current_object_option:=[sp_public];
  989. old_block_type:=block_type;
  990. block_type:=bt_type;
  991. { Force an expected ID error message }
  992. if not (token in [_ID,_CASE,_END]) then
  993. consume(_ID);
  994. { read vars }
  995. sc:=TFPObjectList.create(false);
  996. while (token=_ID) do
  997. begin
  998. semicoloneaten:=false;
  999. hasdefaultvalue:=false;
  1000. allowdefaultvalue:=true;
  1001. sc.clear;
  1002. repeat
  1003. if (token = _ID) then
  1004. begin
  1005. case symtablestack.top.symtabletype of
  1006. localsymtable :
  1007. vs:=tlocalvarsym.create(orgpattern,vs_value,generrordef,[]);
  1008. staticsymtable,
  1009. globalsymtable :
  1010. begin
  1011. vs:=tstaticvarsym.create(orgpattern,vs_value,generrordef,[]);
  1012. if vd_threadvar in options then
  1013. include(vs.varoptions,vo_is_thread_var);
  1014. end;
  1015. else
  1016. internalerror(200411064);
  1017. end;
  1018. sc.add(vs);
  1019. symtablestack.top.insert(vs);
  1020. end;
  1021. consume(_ID);
  1022. until not try_to_consume(_COMMA);
  1023. consume(_COLON);
  1024. {$ifdef gpc_mode}
  1025. if (m_gpc in current_settings.modeswitches) and
  1026. (token=_ID) and
  1027. (orgpattern='__asmname__') then
  1028. read_gpc_name(sc);
  1029. {$endif}
  1030. { read variable type def }
  1031. read_anon_type(hdef,false);
  1032. for i:=0 to sc.count-1 do
  1033. begin
  1034. vs:=tabstractvarsym(sc[i]);
  1035. vs.vardef:=hdef;
  1036. end;
  1037. { Process procvar directives }
  1038. if maybe_parse_proc_directives(hdef) then
  1039. semicoloneaten:=true;
  1040. { check for absolute }
  1041. if try_to_consume(_ABSOLUTE) then
  1042. begin
  1043. read_absolute(sc);
  1044. allowdefaultvalue:=false;
  1045. end;
  1046. { Check for EXTERNAL etc directives before a semicolon }
  1047. if (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  1048. begin
  1049. read_public_and_external_sc(sc);
  1050. allowdefaultvalue:=false;
  1051. semicoloneaten:=true;
  1052. end;
  1053. { try to parse the hint directives }
  1054. hintsymoptions:=[];
  1055. try_consume_hintdirective(hintsymoptions);
  1056. for i:=0 to sc.count-1 do
  1057. begin
  1058. vs:=tabstractvarsym(sc[i]);
  1059. vs.symoptions := vs.symoptions + hintsymoptions;
  1060. end;
  1061. { Handling of Delphi typed const = initialized vars }
  1062. if allowdefaultvalue and
  1063. (token=_EQUAL) and
  1064. not(m_tp7 in current_settings.modeswitches) and
  1065. (symtablestack.top.symtabletype<>parasymtable) then
  1066. begin
  1067. { Add calling convention for procvar }
  1068. if (hdef.typ=procvardef) and
  1069. (hdef.typesym=nil) then
  1070. handle_calling_convention(tprocvardef(hdef));
  1071. read_default_value(sc);
  1072. hasdefaultvalue:=true;
  1073. end
  1074. else
  1075. begin
  1076. if not(semicoloneaten) then
  1077. consume(_SEMICOLON);
  1078. end;
  1079. { Support calling convention for procvars after semicolon }
  1080. if not(hasdefaultvalue) and
  1081. (hdef.typ=procvardef) and
  1082. (hdef.typesym=nil) then
  1083. begin
  1084. { Parse procvar directives after ; }
  1085. maybe_parse_proc_directives(hdef);
  1086. { Add calling convention for procvar }
  1087. handle_calling_convention(tprocvardef(hdef));
  1088. { Handling of Delphi typed const = initialized vars }
  1089. if (token=_EQUAL) and
  1090. not(m_tp7 in current_settings.modeswitches) and
  1091. (symtablestack.top.symtabletype<>parasymtable) then
  1092. begin
  1093. read_default_value(sc);
  1094. hasdefaultvalue:=true;
  1095. end;
  1096. end;
  1097. { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
  1098. if (
  1099. (
  1100. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) and
  1101. (m_cvar_support in current_settings.modeswitches)
  1102. ) or
  1103. (
  1104. (m_mac in current_settings.modeswitches) and
  1105. (
  1106. (cs_external_var in current_settings.localswitches) or
  1107. (cs_externally_visible in current_settings.localswitches)
  1108. )
  1109. )
  1110. ) then
  1111. read_public_and_external_sc(sc);
  1112. { allocate normal variable (non-external and non-typed-const) staticvarsyms }
  1113. for i:=0 to sc.count-1 do
  1114. begin
  1115. vs:=tabstractvarsym(sc[i]);
  1116. if (vs.typ=staticvarsym) and
  1117. not(vo_is_typed_const in vs.varoptions) and
  1118. not(vo_is_external in vs.varoptions) then
  1119. insertbssdata(tstaticvarsym(vs));
  1120. end;
  1121. end;
  1122. block_type:=old_block_type;
  1123. current_object_option:=old_current_object_option;
  1124. { free the list }
  1125. sc.free;
  1126. end;
  1127. procedure read_record_fields(options:Tvar_dec_options);
  1128. var
  1129. sc : TFPObjectList;
  1130. i : longint;
  1131. old_block_type : tblock_type;
  1132. old_current_object_option : tsymoptions;
  1133. hs,sorg : string;
  1134. hdef,casetype : tdef;
  1135. { maxsize contains the max. size of a variant }
  1136. { startvarrec contains the start of the variant part of a record }
  1137. maxsize, startvarrecsize : longint;
  1138. usedalign,
  1139. maxalignment,startvarrecalign,
  1140. maxpadalign, startpadalign: shortint;
  1141. pt : tnode;
  1142. fieldvs : tfieldvarsym;
  1143. hstaticvs : tstaticvarsym;
  1144. vs : tabstractvarsym;
  1145. srsym : tsym;
  1146. srsymtable : TSymtable;
  1147. recst : tabstractrecordsymtable;
  1148. unionsymtable : trecordsymtable;
  1149. offset : longint;
  1150. uniondef : trecorddef;
  1151. hintsymoptions : tsymoptions;
  1152. semicoloneaten: boolean;
  1153. {$ifdef support_llvm}
  1154. is_first_field: boolean;
  1155. {$endif support_llvm}
  1156. {$if defined(powerpc) or defined(powerpc64)}
  1157. tempdef: tdef;
  1158. is_first_type: boolean;
  1159. {$endif powerpc or powerpc64}
  1160. begin
  1161. recst:=tabstractrecordsymtable(symtablestack.top);
  1162. {$ifdef support_llvm}
  1163. is_first_field:=true;
  1164. {$endif support_llvm}
  1165. {$if defined(powerpc) or defined(powerpc64)}
  1166. is_first_type:=true;
  1167. {$endif powerpc or powerpc64}
  1168. old_current_object_option:=current_object_option;
  1169. { all variables are public if not in a object declaration }
  1170. if not(vd_object in options) then
  1171. current_object_option:=[sp_public];
  1172. old_block_type:=block_type;
  1173. block_type:=bt_type;
  1174. { Force an expected ID error message }
  1175. if not (token in [_ID,_CASE,_END]) then
  1176. consume(_ID);
  1177. { read vars }
  1178. sc:=TFPObjectList.create(false);
  1179. while (token=_ID) and
  1180. not((vd_object in options) and
  1181. (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED,_STRICT])) do
  1182. begin
  1183. semicoloneaten:=false;
  1184. sc.clear;
  1185. repeat
  1186. sorg:=orgpattern;
  1187. if token=_ID then
  1188. begin
  1189. vs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1190. sc.add(vs);
  1191. recst.insert(vs);
  1192. end;
  1193. consume(_ID);
  1194. until not try_to_consume(_COMMA);
  1195. consume(_COLON);
  1196. { Don't search in the recordsymtable for types }
  1197. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1198. symtablestack.pop(recst);
  1199. read_anon_type(hdef,false);
  1200. if ([df_generic,df_specialization]*tdef(recst.defowner).defoptions=[]) then
  1201. symtablestack.push(recst);
  1202. { Process procvar directives }
  1203. if maybe_parse_proc_directives(hdef) then
  1204. semicoloneaten:=true;
  1205. {$if defined(powerpc) or defined(powerpc64)}
  1206. { from gcc/gcc/config/rs6000/rs6000.h:
  1207. /* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
  1208. /* Return the alignment of a struct based on the Macintosh PowerPC
  1209. alignment rules. In general the alignment of a struct is
  1210. determined by the greatest alignment of its elements. However, the
  1211. PowerPC rules cause the alignment of a struct to peg at word
  1212. alignment except when the first field has greater than word
  1213. (32-bit) alignment, in which case the alignment is determined by
  1214. the alignment of the first field. */
  1215. }
  1216. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1217. is_first_type and
  1218. (symtablestack.top.symtabletype=recordsymtable) and
  1219. (trecordsymtable(symtablestack.top).usefieldalignment=C_alignment) then
  1220. begin
  1221. tempdef:=hdef;
  1222. while tempdef.typ=arraydef do
  1223. tempdef:=tarraydef(tempdef).elementdef;
  1224. if tempdef.typ<>recorddef then
  1225. maxpadalign:=tempdef.alignment
  1226. else
  1227. maxpadalign:=trecorddef(tempdef).padalignment;
  1228. if (maxpadalign>4) and
  1229. (maxpadalign>trecordsymtable(symtablestack.top).padalignment) then
  1230. trecordsymtable(symtablestack.top).padalignment:=maxpadalign;
  1231. is_first_type:=false;
  1232. end;
  1233. {$endif powerpc or powerpc64}
  1234. { types that use init/final are not allowed in variant parts, but
  1235. classes are allowed }
  1236. if (variantrecordlevel>0) and
  1237. (hdef.needs_inittable and not is_class(hdef)) then
  1238. Message(parser_e_cant_use_inittable_here);
  1239. { try to parse the hint directives }
  1240. hintsymoptions:=[];
  1241. try_consume_hintdirective(hintsymoptions);
  1242. {$ifdef support_llvm}
  1243. { mark first field }
  1244. if (is_first_field) then
  1245. begin
  1246. include(tfieldvarsym(sc[0]).varoptions,vo_is_first_field);
  1247. is_first_field:=false;
  1248. end;
  1249. {$endif support_llvm}
  1250. { update variable type and hints }
  1251. for i:=0 to sc.count-1 do
  1252. begin
  1253. fieldvs:=tfieldvarsym(sc[i]);
  1254. fieldvs.vardef:=hdef;
  1255. { insert any additional hint directives }
  1256. fieldvs.symoptions := fieldvs.symoptions + hintsymoptions;
  1257. end;
  1258. { Records and objects can't have default values }
  1259. { for a record there doesn't need to be a ; before the END or ) }
  1260. if not(token in [_END,_RKLAMMER]) and
  1261. not(semicoloneaten) then
  1262. consume(_SEMICOLON);
  1263. { Parse procvar directives after ; }
  1264. maybe_parse_proc_directives(hdef);
  1265. { Add calling convention for procvar }
  1266. if (hdef.typ=procvardef) and
  1267. (hdef.typesym=nil) then
  1268. handle_calling_convention(tprocvardef(hdef));
  1269. { Check for STATIC directive }
  1270. if (vd_object in options) and
  1271. (cs_static_keyword in current_settings.moduleswitches) and
  1272. (try_to_consume(_STATIC)) then
  1273. begin
  1274. { add static flag and staticvarsyms }
  1275. for i:=0 to sc.count-1 do
  1276. begin
  1277. fieldvs:=tfieldvarsym(sc[i]);
  1278. include(fieldvs.symoptions,sp_static);
  1279. hstaticvs:=tstaticvarsym.create('$'+lower(symtablestack.top.name^)+'_'+fieldvs.name,vs_value,hdef,[]);
  1280. recst.defowner.owner.insert(hstaticvs);
  1281. insertbssdata(hstaticvs);
  1282. end;
  1283. consume(_SEMICOLON);
  1284. end;
  1285. if (sp_published in current_object_option) and
  1286. not(is_class(hdef)) then
  1287. begin
  1288. Message(parser_e_cant_publish_that);
  1289. exclude(current_object_option,sp_published);
  1290. { recover by changing access type to public }
  1291. for i:=0 to sc.count-1 do
  1292. begin
  1293. fieldvs:=tfieldvarsym(sc[i]);
  1294. exclude(fieldvs.symoptions,sp_published);
  1295. include(fieldvs.symoptions,sp_public);
  1296. end;
  1297. end
  1298. else
  1299. if (sp_published in current_object_option) and
  1300. not(oo_can_have_published in tobjectdef(hdef).objectoptions) and
  1301. not(m_delphi in current_settings.modeswitches) then
  1302. begin
  1303. Message(parser_e_only_publishable_classes_can_be_published);
  1304. exclude(current_object_option,sp_published);
  1305. end;
  1306. { Generate field in the recordsymtable }
  1307. for i:=0 to sc.count-1 do
  1308. begin
  1309. fieldvs:=tfieldvarsym(sc[i]);
  1310. { static data fields are already inserted in the globalsymtable }
  1311. if not(sp_static in current_object_option) then
  1312. recst.addfield(fieldvs);
  1313. end;
  1314. { restore current_object_option, it can be changed for
  1315. publishing or static }
  1316. current_object_option:=old_current_object_option;
  1317. end;
  1318. { Check for Case }
  1319. if (vd_record in options) and
  1320. try_to_consume(_CASE) then
  1321. begin
  1322. maxsize:=0;
  1323. maxalignment:=0;
  1324. maxpadalign:=0;
  1325. { including a field declaration? }
  1326. fieldvs:=nil;
  1327. sorg:=orgpattern;
  1328. hs:=pattern;
  1329. searchsym(hs,srsym,srsymtable);
  1330. if not(assigned(srsym) and (srsym.typ in [typesym,unitsym])) then
  1331. begin
  1332. consume(_ID);
  1333. consume(_COLON);
  1334. fieldvs:=tfieldvarsym.create(sorg,vs_value,generrordef,[]);
  1335. symtablestack.top.insert(fieldvs);
  1336. end;
  1337. read_anon_type(casetype,true);
  1338. if assigned(fieldvs) then
  1339. begin
  1340. {$ifdef support_llvm}
  1341. { mark first field if not yet marked }
  1342. if (is_first_field) then
  1343. begin
  1344. include(fieldvs.varoptions,vo_is_first_field);
  1345. is_first_field:=false;
  1346. end;
  1347. {$endif support_llvm}
  1348. fieldvs.vardef:=casetype;
  1349. recst.addfield(fieldvs);
  1350. end;
  1351. if not(is_ordinal(casetype))
  1352. {$ifndef cpu64bitaddr}
  1353. or is_64bitint(casetype)
  1354. {$endif cpu64bitaddr}
  1355. then
  1356. Message(type_e_ordinal_expr_expected);
  1357. consume(_OF);
  1358. UnionSymtable:=trecordsymtable.create(current_settings.packrecords);
  1359. UnionDef:=trecorddef.create(unionsymtable);
  1360. uniondef.isunion:=true;
  1361. startvarrecsize:=UnionSymtable.datasize;
  1362. { align the bitpacking to the next byte }
  1363. UnionSymtable.datasize:=startvarrecsize;
  1364. startvarrecalign:=UnionSymtable.fieldalignment;
  1365. startpadalign:=Unionsymtable.padalignment;
  1366. symtablestack.push(UnionSymtable);
  1367. repeat
  1368. repeat
  1369. pt:=comp_expr(true);
  1370. if not(pt.nodetype=ordconstn) then
  1371. Message(parser_e_illegal_expression);
  1372. if try_to_consume(_POINTPOINT) then
  1373. pt:=crangenode.create(pt,comp_expr(true));
  1374. pt.free;
  1375. if token=_COMMA then
  1376. consume(_COMMA)
  1377. else
  1378. break;
  1379. until false;
  1380. consume(_COLON);
  1381. { read the vars }
  1382. consume(_LKLAMMER);
  1383. inc(variantrecordlevel);
  1384. if token<>_RKLAMMER then
  1385. read_record_fields([vd_record]);
  1386. dec(variantrecordlevel);
  1387. consume(_RKLAMMER);
  1388. { calculates maximal variant size }
  1389. maxsize:=max(maxsize,unionsymtable.datasize);
  1390. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1391. maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
  1392. { the items of the next variant are overlayed }
  1393. unionsymtable.datasize:=startvarrecsize;
  1394. unionsymtable.fieldalignment:=startvarrecalign;
  1395. unionsymtable.padalignment:=startpadalign;
  1396. if (token<>_END) and (token<>_RKLAMMER) then
  1397. consume(_SEMICOLON)
  1398. else
  1399. break;
  1400. until (token=_END) or (token=_RKLAMMER);
  1401. symtablestack.pop(UnionSymtable);
  1402. { at last set the record size to that of the biggest variant }
  1403. unionsymtable.datasize:=maxsize;
  1404. unionsymtable.fieldalignment:=maxalignment;
  1405. unionsymtable.addalignmentpadding;
  1406. {$if defined(powerpc) or defined(powerpc64)}
  1407. { parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
  1408. if (target_info.system in [system_powerpc_darwin, system_powerpc_macos, system_powerpc64_darwin]) and
  1409. is_first_type and
  1410. (recst.usefieldalignment=C_alignment) and
  1411. (maxpadalign>recst.padalignment) then
  1412. recst.padalignment:=maxpadalign;
  1413. {$endif powerpc or powerpc64}
  1414. { Align the offset where the union symtable is added }
  1415. case recst.usefieldalignment of
  1416. { allow the unionsymtable to be aligned however it wants }
  1417. { (within the global min/max limits) }
  1418. 0, { default }
  1419. C_alignment:
  1420. usedalign:=used_align(unionsymtable.recordalignment,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  1421. { 1 byte alignment if we are bitpacked }
  1422. bit_alignment:
  1423. usedalign:=1;
  1424. { otherwise alignment at the packrecords alignment of the }
  1425. { current record }
  1426. else
  1427. usedalign:=used_align(recst.fieldalignment,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax);
  1428. end;
  1429. offset:=align(recst.datasize,usedalign);
  1430. recst.datasize:=offset+unionsymtable.datasize;
  1431. if unionsymtable.recordalignment>recst.fieldalignment then
  1432. recst.fieldalignment:=unionsymtable.recordalignment;
  1433. trecordsymtable(recst).insertunionst(Unionsymtable,offset);
  1434. uniondef.owner.deletedef(uniondef);
  1435. end;
  1436. block_type:=old_block_type;
  1437. current_object_option:=old_current_object_option;
  1438. { free the list }
  1439. sc.free;
  1440. {$ifdef powerpc}
  1441. is_first_type := false;
  1442. {$endif powerpc}
  1443. end;
  1444. end.