pdecvar.pas 67 KB

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