pdecvar.pas 66 KB

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