pdecvar.pas 59 KB

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