pdecvar.pas 59 KB

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