pdecvar.pas 57 KB

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