pdecvar.pas 60 KB

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