pdecvar.pas 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Parses variable declarations. Used for var statement and record
  5. definitions
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit pdecvar;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. symsym,symdef;
  24. function read_property_dec(aclass:tobjectdef):tpropertysym;
  25. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  26. implementation
  27. uses
  28. { common }
  29. cutils,cclasses,
  30. { global }
  31. globtype,globals,tokens,verbose,
  32. systems,
  33. { symtable }
  34. symconst,symbase,symtype,symtable,defutil,defcmp,
  35. fmodule,
  36. { pass 1 }
  37. node,pass_1,
  38. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,
  39. { codegen }
  40. ncgutil,
  41. { parser }
  42. scanner,
  43. pbase,pexpr,ptype,ptconst,pdecsub,
  44. { link }
  45. import
  46. {$ifdef Delphi}
  47. ,dmisc
  48. ,sysutils
  49. {$endif}
  50. ;
  51. function read_property_dec(aclass:tobjectdef):tpropertysym;
  52. { convert a node tree to symlist and return the last
  53. symbol }
  54. function parse_symlist(pl:tsymlist;var def:tdef):boolean;
  55. var
  56. idx : longint;
  57. sym : tsym;
  58. srsymtable : tsymtable;
  59. st : tsymtable;
  60. p : tnode;
  61. begin
  62. result:=true;
  63. def:=nil;
  64. if token=_ID then
  65. begin
  66. if assigned(aclass) then
  67. sym:=search_class_member(aclass,pattern)
  68. else
  69. searchsym(pattern,sym,srsymtable);
  70. if assigned(sym) then
  71. begin
  72. case sym.typ of
  73. varsym :
  74. begin
  75. pl.addsym(sl_load,sym);
  76. def:=tvarsym(sym).vartype.def;
  77. end;
  78. procsym :
  79. begin
  80. pl.addsym(sl_call,sym);
  81. end;
  82. end;
  83. end
  84. else
  85. begin
  86. Message1(parser_e_illegal_field_or_method,pattern);
  87. result:=false;
  88. end;
  89. consume(_ID);
  90. repeat
  91. case token of
  92. _ID,
  93. _SEMICOLON :
  94. begin
  95. break;
  96. end;
  97. _POINT :
  98. begin
  99. consume(_POINT);
  100. if assigned(def) then
  101. begin
  102. st:=def.getsymtable(gs_record);
  103. if assigned(st) then
  104. begin
  105. sym:=searchsymonlyin(st,pattern);
  106. if assigned(sym) then
  107. begin
  108. pl.addsym(sl_subscript,sym);
  109. case sym.typ of
  110. varsym :
  111. def:=tvarsym(sym).vartype.def;
  112. else
  113. begin
  114. Message1(sym_e_illegal_field,pattern);
  115. result:=false;
  116. end;
  117. end;
  118. end
  119. else
  120. begin
  121. Message1(sym_e_illegal_field,pattern);
  122. result:=false;
  123. end;
  124. end
  125. else
  126. begin
  127. Message(parser_e_invalid_qualifier);
  128. result:=false;
  129. end;
  130. end
  131. else
  132. begin
  133. Message(parser_e_invalid_qualifier);
  134. result:=false;
  135. end;
  136. consume(_ID);
  137. end;
  138. _LECKKLAMMER :
  139. begin
  140. consume(_LECKKLAMMER);
  141. repeat
  142. if def.deftype=arraydef then
  143. begin
  144. idx:=0;
  145. p:=comp_expr(true);
  146. if (not codegenerror) then
  147. begin
  148. if (p.nodetype=ordconstn) then
  149. begin
  150. if compare_defs(p.resulttype.def,tarraydef(def).rangetype.def,nothingn)>=te_equal then
  151. idx:=tordconstnode(p).value
  152. else
  153. IncompatibleTypes(p.resulttype.def,tarraydef(def).rangetype.def);
  154. end
  155. else
  156. Message(parser_e_illegal_expression)
  157. end;
  158. p.free;
  159. pl.addconst(sl_vec,idx);
  160. def:=tarraydef(def).elementtype.def;
  161. end
  162. else
  163. begin
  164. Message(parser_e_invalid_qualifier);
  165. result:=false;
  166. end;
  167. until not try_to_consume(_COMMA);
  168. consume(_RECKKLAMMER);
  169. end;
  170. else
  171. begin
  172. Message(parser_e_ill_property_access_sym);
  173. result:=false;
  174. break;
  175. end;
  176. end;
  177. until false;
  178. end
  179. else
  180. begin
  181. Message(parser_e_ill_property_access_sym);
  182. result:=false;
  183. end;
  184. end;
  185. var
  186. sym : tsym;
  187. p : tpropertysym;
  188. overriden : tsym;
  189. hs : string;
  190. varspez : tvarspez;
  191. s : string;
  192. tt : ttype;
  193. arraytype : ttype;
  194. def : tdef;
  195. pt : tnode;
  196. propname : stringid;
  197. sc : tsinglelist;
  198. oldregisterdef : boolean;
  199. readvs,
  200. hvs : tvarsym;
  201. readprocdef,
  202. writeprocdef : tprocvardef;
  203. oldsymtablestack : tsymtable;
  204. begin
  205. { Generate temp procvardefs to search for matching read/write
  206. procedures. the readprocdef will store all definitions }
  207. oldregisterdef:=registerdef;
  208. registerdef:=false;
  209. readprocdef:=tprocvardef.create(normal_function_level);
  210. writeprocdef:=tprocvardef.create(normal_function_level);
  211. registerdef:=oldregisterdef;
  212. { make it method pointers }
  213. if assigned(aclass) then
  214. begin
  215. include(readprocdef.procoptions,po_methodpointer);
  216. include(writeprocdef.procoptions,po_methodpointer);
  217. end;
  218. if token<>_ID then
  219. begin
  220. consume(_ID);
  221. consume(_SEMICOLON);
  222. exit;
  223. end;
  224. { Generate propertysym and insert in symtablestack }
  225. p:=tpropertysym.create(orgpattern);
  226. symtablestack.insert(p);
  227. propname:=pattern;
  228. consume(_ID);
  229. { Set the symtablestack to the parast of readprop so
  230. temp defs will be destroyed after declaration }
  231. readprocdef.parast.next:=symtablestack;
  232. symtablestack:=readprocdef.parast;
  233. { property parameters ? }
  234. if token=_LECKKLAMMER then
  235. begin
  236. if (sp_published in current_object_option) then
  237. Message(parser_e_cant_publish_that_property);
  238. { create a list of the parameters }
  239. sc:=tsinglelist.create;
  240. consume(_LECKKLAMMER);
  241. inc(testcurobject);
  242. repeat
  243. if token=_VAR then
  244. begin
  245. consume(_VAR);
  246. varspez:=vs_var;
  247. end
  248. else if token=_CONST then
  249. begin
  250. consume(_CONST);
  251. varspez:=vs_const;
  252. end
  253. else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  254. begin
  255. consume(_OUT);
  256. varspez:=vs_out;
  257. end
  258. else
  259. varspez:=vs_value;
  260. sc.reset;
  261. repeat
  262. readvs:=tvarsym.create(orgpattern,varspez,generrortype);
  263. readprocdef.parast.insert(readvs);
  264. sc.insert(readvs);
  265. consume(_ID);
  266. until not try_to_consume(_COMMA);
  267. if try_to_consume(_COLON) then
  268. begin
  269. { for records, don't search the recordsymtable for
  270. the symbols of the types }
  271. oldsymtablestack:=symtablestack;
  272. symtablestack:=symtablestack.next;
  273. if try_to_consume(_ARRAY) then
  274. begin
  275. consume(_OF);
  276. { define range and type of range }
  277. tt.setdef(tarraydef.create(0,-1,s32inttype));
  278. { define field type }
  279. single_type(arraytype,s,false);
  280. tarraydef(tt.def).setelementtype(arraytype);
  281. end
  282. else
  283. single_type(tt,s,false);
  284. symtablestack:=oldsymtablestack;
  285. end
  286. else
  287. tt:=cformaltype;
  288. readvs:=tvarsym(sc.first);
  289. while assigned(readvs) do
  290. begin
  291. readprocdef.concatpara(nil,tt,readvs,nil,false);
  292. { also update the writeprocdef }
  293. hvs:=tvarsym.create(readvs.realname,vs_value,generrortype);
  294. writeprocdef.parast.insert(hvs);
  295. writeprocdef.concatpara(nil,tt,hvs,nil,false);
  296. readvs:=tvarsym(readvs.listnext);
  297. end;
  298. until not try_to_consume(_SEMICOLON);
  299. sc.free;
  300. dec(testcurobject);
  301. consume(_RECKKLAMMER);
  302. { the parser need to know if a property has parameters, the
  303. index parameter doesn't count (PFV) }
  304. if readprocdef.minparacount>0 then
  305. include(p.propoptions,ppo_hasparameters);
  306. end;
  307. { overriden property ? }
  308. { force property interface
  309. there is a property parameter
  310. a global property }
  311. if (token=_COLON) or (readprocdef.minparacount>0) or (aclass=nil) then
  312. begin
  313. consume(_COLON);
  314. { insert types in global symtable }
  315. oldsymtablestack:=symtablestack;
  316. while not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) do
  317. symtablestack:=symtablestack.next;
  318. single_type(p.proptype,hs,false);
  319. symtablestack:=oldsymtablestack;
  320. if (idtoken=_INDEX) then
  321. begin
  322. consume(_INDEX);
  323. pt:=comp_expr(true);
  324. if is_constnode(pt) and
  325. is_ordinal(pt.resulttype.def)
  326. {$ifndef cpu64bit}
  327. and (not is_64bitint(pt.resulttype.def))
  328. {$endif}
  329. then
  330. p.index:=tordconstnode(pt).value
  331. else
  332. begin
  333. Message(parser_e_invalid_property_index_value);
  334. p.index:=0;
  335. end;
  336. inserttypeconv(pt,sinttype);
  337. p.indextype.setdef(pt.resulttype.def);
  338. include(p.propoptions,ppo_indexed);
  339. { concat a longint to the para templates }
  340. hvs:=tvarsym.create('$index',vs_value,p.indextype);
  341. readprocdef.parast.insert(hvs);
  342. readprocdef.concatpara(nil,p.indextype,hvs,nil,false);
  343. hvs:=tvarsym.create('$index',vs_value,p.indextype);
  344. writeprocdef.parast.insert(hvs);
  345. writeprocdef.concatpara(nil,p.indextype,hvs,nil,false);
  346. pt.free;
  347. end;
  348. end
  349. else
  350. begin
  351. { do an property override }
  352. overriden:=search_class_member(aclass.childof,propname);
  353. if assigned(overriden) and (overriden.typ=propertysym) then
  354. begin
  355. p.dooverride(tpropertysym(overriden));
  356. end
  357. else
  358. begin
  359. p.proptype:=generrortype;
  360. message(parser_e_no_property_found_to_override);
  361. end;
  362. end;
  363. if (sp_published in current_object_option) and
  364. not(p.proptype.def.is_publishable) then
  365. Message(parser_e_cant_publish_that_property);
  366. if try_to_consume(_READ) then
  367. begin
  368. p.readaccess.clear;
  369. if parse_symlist(p.readaccess,def) then
  370. begin
  371. sym:=p.readaccess.firstsym^.sym;
  372. case sym.typ of
  373. procsym :
  374. begin
  375. { read is function returning the type of the property }
  376. readprocdef.rettype:=p.proptype;
  377. { Insert hidden parameters }
  378. handle_calling_convention(readprocdef);
  379. calc_parast(readprocdef);
  380. { search procdefs matching readprocdef }
  381. p.readaccess.procdef:=Tprocsym(sym).search_procdef_bypara(readprocdef.para,p.proptype.def,[cpo_allowdefaults,cpo_allowconvert]);
  382. if not assigned(p.readaccess.procdef) then
  383. Message(parser_e_ill_property_access_sym);
  384. end;
  385. varsym :
  386. begin
  387. if not assigned(def) then
  388. internalerror(200310071);
  389. if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
  390. begin
  391. { property parameters are allowed if this is
  392. an indexed property, because the index is then
  393. the parameter.
  394. Note: In the help of Kylix it is written
  395. that it isn't allowed, but the compiler accepts it (PFV) }
  396. if (ppo_hasparameters in p.propoptions) then
  397. Message(parser_e_ill_property_access_sym);
  398. end
  399. else
  400. IncompatibleTypes(def,p.proptype.def);
  401. end;
  402. else
  403. Message(parser_e_ill_property_access_sym);
  404. end;
  405. end;
  406. end;
  407. if try_to_consume(_WRITE) then
  408. begin
  409. p.writeaccess.clear;
  410. if parse_symlist(p.writeaccess,def) then
  411. begin
  412. sym:=p.writeaccess.firstsym^.sym;
  413. case sym.typ of
  414. procsym :
  415. begin
  416. { write is a procedure with an extra value parameter
  417. of the of the property }
  418. writeprocdef.rettype:=voidtype;
  419. hvs:=tvarsym.create('$value',vs_value,p.proptype);
  420. writeprocdef.parast.insert(hvs);
  421. writeprocdef.concatpara(nil,p.proptype,hvs,nil,false);
  422. { Insert hidden parameters }
  423. handle_calling_convention(writeprocdef);
  424. calc_parast(writeprocdef);
  425. { search procdefs matching writeprocdef }
  426. p.writeaccess.procdef:=Tprocsym(sym).search_procdef_bypara(writeprocdef.para,writeprocdef.rettype.def,[cpo_allowdefaults,cpo_allowconvert]);
  427. if not assigned(p.writeaccess.procdef) then
  428. Message(parser_e_ill_property_access_sym);
  429. end;
  430. varsym :
  431. begin
  432. if not assigned(def) then
  433. internalerror(200310072);
  434. if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
  435. begin
  436. { property parameters are allowed if this is
  437. an indexed property, because the index is then
  438. the parameter.
  439. Note: In the help of Kylix it is written
  440. that it isn't allowed, but the compiler accepts it (PFV) }
  441. if (ppo_hasparameters in p.propoptions) then
  442. Message(parser_e_ill_property_access_sym);
  443. end
  444. else
  445. IncompatibleTypes(def,p.proptype.def);
  446. end;
  447. else
  448. Message(parser_e_ill_property_access_sym);
  449. end;
  450. end;
  451. end;
  452. if assigned(aclass) then
  453. begin
  454. include(p.propoptions,ppo_stored);
  455. if try_to_consume(_STORED) then
  456. begin
  457. p.storedaccess.clear;
  458. case token of
  459. _ID:
  460. begin
  461. { in the case that idtoken=_DEFAULT }
  462. { we have to do nothing except }
  463. { setting ppo_stored, it's the same }
  464. { as stored true }
  465. if idtoken<>_DEFAULT then
  466. begin
  467. if parse_symlist(p.storedaccess,def) then
  468. begin
  469. sym:=p.storedaccess.firstsym^.sym;
  470. case sym.typ of
  471. procsym :
  472. begin
  473. p.storedaccess.procdef:=Tprocsym(sym).search_procdef_nopara_boolret;
  474. if not assigned(p.storedaccess.procdef) then
  475. message(parser_e_ill_property_storage_sym);
  476. end;
  477. varsym :
  478. begin
  479. if not assigned(def) then
  480. internalerror(200310073);
  481. if (ppo_hasparameters in p.propoptions) or
  482. not(is_boolean(def)) then
  483. Message(parser_e_stored_property_must_be_boolean);
  484. end;
  485. else
  486. Message(parser_e_ill_property_access_sym);
  487. end;
  488. end;
  489. end;
  490. end;
  491. _FALSE:
  492. begin
  493. consume(_FALSE);
  494. exclude(p.propoptions,ppo_stored);
  495. end;
  496. _TRUE:
  497. consume(_TRUE);
  498. end;
  499. end;
  500. end;
  501. if try_to_consume(_DEFAULT) then
  502. begin
  503. if not(is_ordinal(p.proptype.def) or
  504. {$ifndef cpu64bit}
  505. is_64bitint(p.proptype.def) or
  506. {$endif cpu64bit}
  507. is_class(p.proptype.def) or
  508. is_single(p.proptype.def) or
  509. (p.proptype.def.deftype in [classrefdef,pointerdef]) or
  510. ((p.proptype.def.deftype=setdef) and
  511. (tsetdef(p.proptype.def).settype=smallset))) or
  512. ((p.proptype.def.deftype=arraydef) and
  513. (ppo_indexed in p.propoptions)) or
  514. (ppo_hasparameters in p.propoptions) then
  515. begin
  516. Message(parser_e_property_cant_have_a_default_value);
  517. { Error recovery }
  518. pt:=comp_expr(true);
  519. pt.free;
  520. end
  521. else
  522. begin
  523. { Get the result of the default, the firstpass is
  524. needed to support values like -1 }
  525. pt:=comp_expr(true);
  526. if (p.proptype.def.deftype=setdef) and
  527. (pt.nodetype=arrayconstructorn) then
  528. begin
  529. arrayconstructor_to_set(pt);
  530. do_resulttypepass(pt);
  531. end;
  532. inserttypeconv(pt,p.proptype);
  533. if not(is_constnode(pt)) then
  534. Message(parser_e_property_default_value_must_const);
  535. { Set default value }
  536. case pt.nodetype of
  537. setconstn :
  538. p.default:=plongint(tsetconstnode(pt).value_set)^;
  539. ordconstn :
  540. p.default:=tordconstnode(pt).value;
  541. niln :
  542. p.default:=0;
  543. realconstn:
  544. p.default:=longint(single(trealconstnode(pt).value_real));
  545. end;
  546. pt.free;
  547. end;
  548. end
  549. else if try_to_consume(_NODEFAULT) then
  550. begin
  551. p.default:=0;
  552. end;
  553. { remove temporary procvardefs }
  554. symtablestack:=symtablestack.next;
  555. readprocdef.free;
  556. writeprocdef.free;
  557. result:=p;
  558. end;
  559. const
  560. variantrecordlevel : longint = 0;
  561. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  562. { reads the filed of a record into a }
  563. { symtablestack, if record=false }
  564. { variants are forbidden, so this procedure }
  565. { can be used to read object fields }
  566. { if absolute is true, ABSOLUTE and file }
  567. { types are allowed }
  568. { => the procedure is also used to read }
  569. { a sequence of variable declaration }
  570. procedure insert_syms(sc : tsinglelist;tt : ttype;is_threadvar : boolean; addsymopts : tsymoptions);
  571. { inserts the symbols of sc in st with def as definition or sym as ttypesym, sc is disposed }
  572. var
  573. vs,vs2 : tvarsym;
  574. begin
  575. vs:=tvarsym(sc.first);
  576. while assigned(vs) do
  577. begin
  578. vs.vartype:=tt;
  579. { insert any additional hint directives }
  580. vs.symoptions := vs.symoptions + addsymopts;
  581. if (sp_static in current_object_option) then
  582. include(vs.symoptions,sp_static);
  583. if is_threadvar then
  584. include(vs.varoptions,vo_is_thread_var);
  585. { static data fields are inserted in the globalsymtable }
  586. if (symtablestack.symtabletype=objectsymtable) and
  587. (sp_static in current_object_option) then
  588. begin
  589. vs2:=tvarsym.create('$'+lower(symtablestack.name^)+'_'+vs.name,vs_value,tt);
  590. symtablestack.defowner.owner.insert(vs2);
  591. insertbssdata(vs2);
  592. end
  593. else
  594. begin
  595. { external data is not possible here }
  596. case symtablestack.symtabletype of
  597. globalsymtable,
  598. staticsymtable :
  599. insertbssdata(vs);
  600. recordsymtable,
  601. objectsymtable :
  602. tabstractrecordsymtable(symtablestack).insertfield(vs,false);
  603. end;
  604. end;
  605. vs:=tvarsym(vs.listnext);
  606. end;
  607. end;
  608. procedure read_default_value(sc : tsinglelist;tt : ttype;is_threadvar : boolean);
  609. var
  610. vs : tvarsym;
  611. tcsym : ttypedconstsym;
  612. begin
  613. vs:=tvarsym(sc.first);
  614. if assigned(vs.listnext) then
  615. Message(parser_e_initialized_only_one_var);
  616. if is_threadvar then
  617. Message(parser_e_initialized_not_for_threadvar);
  618. if symtablestack.symtabletype=localsymtable then
  619. begin
  620. consume(_EQUAL);
  621. tcsym:=ttypedconstsym.createtype('default'+vs.realname,tt,false);
  622. vs.defaultconstsym:=tcsym;
  623. symtablestack.insert(tcsym);
  624. insertconstdata(tcsym);
  625. readtypedconst(tt,tcsym,false);
  626. end
  627. else
  628. begin
  629. tcsym:=ttypedconstsym.createtype(vs.realname,tt,true);
  630. tcsym.fileinfo:=vs.fileinfo;
  631. symtablestack.replace(vs,tcsym);
  632. vs.free;
  633. insertconstdata(tcsym);
  634. consume(_EQUAL);
  635. readtypedconst(tt,tcsym,true);
  636. end;
  637. end;
  638. var
  639. sc : tsinglelist;
  640. old_block_type : tblock_type;
  641. symdone : boolean;
  642. { to handle absolute }
  643. abssym : tabsolutesym;
  644. { c var }
  645. newtype : ttypesym;
  646. is_dll,
  647. hasdefaultvalue,
  648. is_gpc_name,is_cdecl,
  649. extern_var,export_var : boolean;
  650. old_current_object_option : tsymoptions;
  651. hs,sorg,C_name,dll_name : string;
  652. tt,casetype : ttype;
  653. { maxsize contains the max. size of a variant }
  654. { startvarrec contains the start of the variant part of a record }
  655. maxsize, startvarrecsize : longint;
  656. usedalign,
  657. maxalignment,startvarrecalign : byte;
  658. hp,pt : tnode;
  659. vs,vs2 : tvarsym;
  660. srsym : tsym;
  661. oldsymtablestack,
  662. srsymtable : tsymtable;
  663. unionsymtable : trecordsymtable;
  664. offset : longint;
  665. uniondef : trecorddef;
  666. unionsym : tvarsym;
  667. uniontype : ttype;
  668. dummysymoptions : tsymoptions;
  669. begin
  670. old_current_object_option:=current_object_option;
  671. { all variables are public if not in a object declaration }
  672. if not is_object then
  673. current_object_option:=[sp_public];
  674. old_block_type:=block_type;
  675. block_type:=bt_type;
  676. is_gpc_name:=false;
  677. { Force an expected ID error message }
  678. if not (token in [_ID,_CASE,_END]) then
  679. consume(_ID);
  680. { read vars }
  681. sc:=tsinglelist.create;
  682. while (token=_ID) and
  683. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  684. begin
  685. sorg:=orgpattern;
  686. sc.reset;
  687. repeat
  688. vs:=tvarsym.create(orgpattern,vs_value,generrortype);
  689. symtablestack.insert(vs);
  690. if assigned(vs.owner) then
  691. sc.insert(vs)
  692. else
  693. vs.free;
  694. consume(_ID);
  695. until not try_to_consume(_COMMA);
  696. consume(_COLON);
  697. if (m_gpc in aktmodeswitches) and
  698. not(is_record or is_object or is_threadvar) and
  699. (token=_ID) and (orgpattern='__asmname__') then
  700. begin
  701. consume(_ID);
  702. C_name:=get_stringconst;
  703. Is_gpc_name:=true;
  704. end;
  705. { this is needed for Delphi mode at least
  706. but should be OK for all modes !! (PM) }
  707. ignore_equal:=true;
  708. if is_record or is_object then
  709. begin
  710. { for records, don't search the recordsymtable for
  711. the symbols of the types }
  712. oldsymtablestack:=symtablestack;
  713. symtablestack:=symtablestack.next;
  714. read_type(tt,'',false);
  715. symtablestack:=oldsymtablestack;
  716. end
  717. else
  718. read_type(tt,'',false);
  719. { Process procvar directives }
  720. if (tt.def.deftype=procvardef) and
  721. (tt.def.typesym=nil) and
  722. is_proc_directive(token,true) then
  723. begin
  724. newtype:=ttypesym.create('unnamed',tt);
  725. parse_var_proc_directives(tsym(newtype));
  726. newtype.restype.def:=nil;
  727. tt.def.typesym:=nil;
  728. newtype.free;
  729. end;
  730. { types that use init/final are not allowed in variant parts, but
  731. classes are allowed }
  732. if (variantrecordlevel>0) and
  733. (tt.def.needs_inittable and not is_class(tt.def)) then
  734. Message(parser_e_cant_use_inittable_here);
  735. ignore_equal:=false;
  736. hasdefaultvalue:=false;
  737. symdone:=false;
  738. if is_gpc_name then
  739. begin
  740. vs:=tvarsym(sc.first);
  741. if assigned(vs.listnext) then
  742. Message(parser_e_absolute_only_one_var);
  743. vs.vartype:=tt;
  744. include(vs.varoptions,vo_is_C_var);
  745. vs.set_mangledname(target_info.Cprefix+sorg);
  746. include(vs.varoptions,vo_is_external);
  747. symdone:=true;
  748. end;
  749. { check for absolute }
  750. if not symdone and
  751. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  752. begin
  753. consume(_ABSOLUTE);
  754. abssym:=nil;
  755. { only allowed for one var }
  756. vs:=tvarsym(sc.first);
  757. if assigned(vs.listnext) then
  758. Message(parser_e_absolute_only_one_var);
  759. { parse the rest }
  760. pt:=expr;
  761. { check allowed absolute types }
  762. if (pt.nodetype=stringconstn) or
  763. (is_constcharnode(pt)) then
  764. begin
  765. abssym:=tabsolutesym.create(vs.realname,tt);
  766. abssym.fileinfo:=vs.fileinfo;
  767. if pt.nodetype=stringconstn then
  768. hs:=strpas(tstringconstnode(pt).value_str)
  769. else
  770. hs:=chr(tordconstnode(pt).value);
  771. consume(token);
  772. abssym.abstyp:=toasm;
  773. abssym.asmname:=stringdup(hs);
  774. { replace the varsym }
  775. symtablestack.replace(vs,abssym);
  776. vs.free;
  777. end
  778. { address }
  779. else if is_constintnode(pt) and
  780. ((target_info.system in [system_i386_go32v2,system_i386_watcom,
  781. system_i386_wdosx,system_i386_win32]) or
  782. (m_objfpc in aktmodeswitches) or
  783. (m_delphi in aktmodeswitches)) then
  784. begin
  785. abssym:=tabsolutesym.create(vs.realname,tt);
  786. abssym.fileinfo:=vs.fileinfo;
  787. abssym.abstyp:=toaddr;
  788. abssym.fieldoffset:=tordconstnode(pt).value;
  789. {$ifdef i386}
  790. abssym.absseg:=false;
  791. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  792. try_to_consume(_COLON) then
  793. begin
  794. pt.free;
  795. pt:=expr;
  796. if is_constintnode(pt) then
  797. begin
  798. abssym.fieldoffset:=abssym.fieldoffset shl 4+tordconstnode(pt).value;
  799. abssym.absseg:=true;
  800. end
  801. else
  802. Message(type_e_ordinal_expr_expected);
  803. end;
  804. {$endif i386}
  805. symtablestack.replace(vs,abssym);
  806. vs.free;
  807. end
  808. { variable }
  809. else
  810. begin
  811. { remove subscriptn before checking for loadn }
  812. hp:=pt;
  813. while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
  814. hp:=tunarynode(hp).left;
  815. if (hp.nodetype=loadn) then
  816. begin
  817. { we should check the result type of loadn }
  818. if not (tloadnode(hp).symtableentry.typ in [varsym,typedconstsym]) then
  819. Message(parser_e_absolute_only_to_var_or_const);
  820. abssym:=tabsolutesym.create(vs.realname,tt);
  821. abssym.fileinfo:=vs.fileinfo;
  822. abssym.abstyp:=tovar;
  823. abssym.ref:=node_to_symlist(pt);
  824. symtablestack.replace(vs,abssym);
  825. vs.free;
  826. end
  827. else
  828. Message(parser_e_absolute_only_to_var_or_const);
  829. end;
  830. if assigned(abssym) then
  831. begin
  832. { try to consume the hint directives with absolute symbols }
  833. dummysymoptions:=[];
  834. try_consume_hintdirective(dummysymoptions);
  835. abssym.symoptions := abssym.symoptions + dummysymoptions;
  836. end;
  837. pt.free;
  838. symdone:=true;
  839. end;
  840. { Process procvar directives before = and ; }
  841. if (tt.def.deftype=procvardef) and
  842. (tt.def.typesym=nil) and
  843. is_proc_directive(token,true) then
  844. begin
  845. newtype:=ttypesym.create('unnamed',tt);
  846. parse_var_proc_directives(tsym(newtype));
  847. newtype.restype.def:=nil;
  848. tt.def.typesym:=nil;
  849. newtype.free;
  850. end;
  851. { try to parse the hint directives }
  852. dummysymoptions:=[];
  853. try_consume_hintdirective(dummysymoptions);
  854. { Records and objects can't have default values }
  855. if is_record or is_object then
  856. begin
  857. { for a record there doesn't need to be a ; before the END or ) }
  858. if not(token in [_END,_RKLAMMER]) then
  859. consume(_SEMICOLON);
  860. end
  861. else
  862. begin
  863. { Handling of Delphi typed const = initialized vars }
  864. if (token=_EQUAL) and
  865. not(m_tp7 in aktmodeswitches) and
  866. (symtablestack.symtabletype<>parasymtable) then
  867. begin
  868. if (tt.def.deftype=procvardef) and
  869. (tt.def.typesym=nil) then
  870. begin
  871. { Add calling convention for procvar }
  872. handle_calling_convention(tprocvardef(tt.def));
  873. calc_parast(tprocvardef(tt.def));
  874. end;
  875. read_default_value(sc,tt,is_threadvar);
  876. { for locals we've created typedconstsym with a different name }
  877. if symtablestack.symtabletype<>localsymtable then
  878. symdone:=true;
  879. hasdefaultvalue:=true;
  880. end;
  881. consume(_SEMICOLON);
  882. end;
  883. { Support calling convention for procvars after semicolon }
  884. if not(hasdefaultvalue) and
  885. (tt.def.deftype=procvardef) and
  886. (tt.def.typesym=nil) then
  887. begin
  888. { Parse procvar directives after ; }
  889. if is_proc_directive(token,true) then
  890. begin
  891. newtype:=ttypesym.create('unnamed',tt);
  892. parse_var_proc_directives(tsym(newtype));
  893. newtype.restype.def:=nil;
  894. tt.def.typesym:=nil;
  895. newtype.free;
  896. end;
  897. { Add calling convention for procvar }
  898. handle_calling_convention(tprocvardef(tt.def));
  899. calc_parast(tprocvardef(tt.def));
  900. { Handling of Delphi typed const = initialized vars }
  901. if (token=_EQUAL) and
  902. not(is_record or is_object) and
  903. not(m_tp7 in aktmodeswitches) and
  904. (symtablestack.symtabletype<>parasymtable) then
  905. begin
  906. read_default_value(sc,tt,is_threadvar);
  907. consume(_SEMICOLON);
  908. symdone:=true;
  909. hasdefaultvalue:=true;
  910. end;
  911. end;
  912. { Check for variable directives }
  913. if not symdone and (token=_ID) then
  914. begin
  915. { Check for C Variable declarations }
  916. if (m_cvar_support in aktmodeswitches) and
  917. not(is_record or is_object or is_threadvar) and
  918. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  919. begin
  920. { only allowed for one var }
  921. vs:=tvarsym(sc.first);
  922. if assigned(vs.listnext) then
  923. Message(parser_e_absolute_only_one_var);
  924. { set type of the var }
  925. vs.vartype:=tt;
  926. vs.symoptions := vs.symoptions + dummysymoptions;
  927. { defaults }
  928. is_dll:=false;
  929. is_cdecl:=false;
  930. extern_var:=false;
  931. export_var:=false;
  932. C_name:=sorg;
  933. { cdecl }
  934. if idtoken=_CVAR then
  935. begin
  936. consume(_CVAR);
  937. consume(_SEMICOLON);
  938. is_cdecl:=true;
  939. C_name:=target_info.Cprefix+sorg;
  940. end;
  941. { external }
  942. if idtoken=_EXTERNAL then
  943. begin
  944. consume(_EXTERNAL);
  945. extern_var:=true;
  946. end;
  947. { export }
  948. if idtoken in [_EXPORT,_PUBLIC] then
  949. begin
  950. consume(_ID);
  951. if extern_var or
  952. (symtablestack.symtabletype in [parasymtable,localsymtable]) then
  953. Message(parser_e_not_external_and_export)
  954. else
  955. export_var:=true;
  956. end;
  957. { external and export need a name after when no cdecl is used }
  958. if not is_cdecl then
  959. begin
  960. { dll name ? }
  961. if (extern_var) and (idtoken<>_NAME) then
  962. begin
  963. is_dll:=true;
  964. dll_name:=get_stringconst;
  965. end;
  966. consume(_NAME);
  967. C_name:=get_stringconst;
  968. end;
  969. { consume the ; when export or external is used }
  970. if extern_var or export_var then
  971. consume(_SEMICOLON);
  972. { set some vars options }
  973. if is_dll then
  974. include(vs.varoptions,vo_is_dll_var)
  975. else
  976. include(vs.varoptions,vo_is_C_var);
  977. if (is_dll) and
  978. (target_info.system = system_powerpc_darwin) then
  979. C_Name := target_info.Cprefix+C_Name;
  980. vs.set_mangledname(C_Name);
  981. if export_var then
  982. begin
  983. inc(vs.refs);
  984. include(vs.varoptions,vo_is_exported);
  985. end;
  986. if extern_var then
  987. include(vs.varoptions,vo_is_external);
  988. { insert in the datasegment when it is not external }
  989. if (not extern_var) then
  990. insertbssdata(vs);
  991. { now we can insert it in the import lib if its a dll, or
  992. add it to the externals }
  993. if extern_var then
  994. begin
  995. if is_dll then
  996. begin
  997. if not(current_module.uses_imports) then
  998. begin
  999. current_module.uses_imports:=true;
  1000. importlib.preparelib(current_module.modulename^);
  1001. end;
  1002. importlib.importvariable(vs,C_name,dll_name);
  1003. end
  1004. else
  1005. if target_info.DllScanSupported then
  1006. current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
  1007. end;
  1008. symdone:=true;
  1009. end
  1010. else
  1011. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  1012. begin
  1013. include(current_object_option,sp_static);
  1014. insert_syms(sc,tt,false,dummysymoptions);
  1015. exclude(current_object_option,sp_static);
  1016. consume(_STATIC);
  1017. consume(_SEMICOLON);
  1018. symdone:=true;
  1019. end;
  1020. end;
  1021. { insert it in the symtable, if not done yet }
  1022. if not symdone then
  1023. begin
  1024. { save object option, because we can turn of the sp_published }
  1025. if (sp_published in current_object_option) and
  1026. not(is_class(tt.def)) then
  1027. begin
  1028. Message(parser_e_cant_publish_that);
  1029. exclude(current_object_option,sp_published);
  1030. { recover by changing access type to public }
  1031. vs2:=tvarsym(sc.first);
  1032. while assigned (vs2) do
  1033. begin
  1034. exclude(vs2.symoptions,sp_published);
  1035. include(vs2.symoptions,sp_public);
  1036. vs2:=tvarsym(vs2.listnext);
  1037. end;
  1038. end
  1039. else
  1040. if (sp_published in current_object_option) and
  1041. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  1042. begin
  1043. Message(parser_e_only_publishable_classes_can__be_published);
  1044. exclude(current_object_option,sp_published);
  1045. end;
  1046. insert_syms(sc,tt,is_threadvar,dummysymoptions);
  1047. current_object_option:=old_current_object_option;
  1048. end;
  1049. end;
  1050. { Check for Case }
  1051. if is_record and (token=_CASE) then
  1052. begin
  1053. maxsize:=0;
  1054. maxalignment:=0;
  1055. consume(_CASE);
  1056. sorg:=orgpattern;
  1057. hs:=pattern;
  1058. searchsym(hs,srsym,srsymtable);
  1059. { may be only a type: }
  1060. if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
  1061. begin
  1062. { for records, don't search the recordsymtable for
  1063. the symbols of the types }
  1064. oldsymtablestack:=symtablestack;
  1065. symtablestack:=symtablestack.next;
  1066. read_type(casetype,'',true);
  1067. symtablestack:=oldsymtablestack;
  1068. end
  1069. else
  1070. begin
  1071. consume(_ID);
  1072. consume(_COLON);
  1073. { for records, don't search the recordsymtable for
  1074. the symbols of the types }
  1075. oldsymtablestack:=symtablestack;
  1076. symtablestack:=symtablestack.next;
  1077. read_type(casetype,'',true);
  1078. symtablestack:=oldsymtablestack;
  1079. vs:=tvarsym.create(sorg,vs_value,casetype);
  1080. tabstractrecordsymtable(symtablestack).insertfield(vs,true);
  1081. end;
  1082. if not(is_ordinal(casetype.def))
  1083. {$ifndef cpu64bit}
  1084. or is_64bitint(casetype.def)
  1085. {$endif cpu64bit}
  1086. then
  1087. Message(type_e_ordinal_expr_expected);
  1088. consume(_OF);
  1089. UnionSymtable:=trecordsymtable.create(aktpackrecords);
  1090. Unionsymtable.next:=symtablestack;
  1091. registerdef:=false;
  1092. UnionDef:=trecorddef.create(unionsymtable);
  1093. uniondef.isunion:=true;
  1094. if assigned(symtablestack.defowner) then
  1095. Uniondef.owner:=symtablestack.defowner.owner;
  1096. registerdef:=true;
  1097. startvarrecsize:=UnionSymtable.datasize;
  1098. startvarrecalign:=UnionSymtable.fieldalignment;
  1099. symtablestack:=UnionSymtable;
  1100. repeat
  1101. repeat
  1102. pt:=comp_expr(true);
  1103. if not(pt.nodetype=ordconstn) then
  1104. Message(parser_e_illegal_expression);
  1105. pt.free;
  1106. if token=_COMMA then
  1107. consume(_COMMA)
  1108. else
  1109. break;
  1110. until false;
  1111. consume(_COLON);
  1112. { read the vars }
  1113. consume(_LKLAMMER);
  1114. inc(variantrecordlevel);
  1115. if token<>_RKLAMMER then
  1116. read_var_decs(true,false,false);
  1117. dec(variantrecordlevel);
  1118. consume(_RKLAMMER);
  1119. { calculates maximal variant size }
  1120. maxsize:=max(maxsize,unionsymtable.datasize);
  1121. maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
  1122. { the items of the next variant are overlayed }
  1123. unionsymtable.datasize:=startvarrecsize;
  1124. unionsymtable.fieldalignment:=startvarrecalign;
  1125. if (token<>_END) and (token<>_RKLAMMER) then
  1126. consume(_SEMICOLON)
  1127. else
  1128. break;
  1129. until (token=_END) or (token=_RKLAMMER);
  1130. { at last set the record size to that of the biggest variant }
  1131. unionsymtable.datasize:=maxsize;
  1132. unionsymtable.fieldalignment:=maxalignment;
  1133. uniontype.def:=uniondef;
  1134. uniontype.sym:=nil;
  1135. UnionSym:=tvarsym.create('$case',vs_value,uniontype);
  1136. symtablestack:=symtablestack.next;
  1137. { Align the offset where the union symtable is added }
  1138. if (trecordsymtable(symtablestack).usefieldalignment=-1) then
  1139. usedalign:=used_align(maxalignment,aktalignment.recordalignmin,aktalignment.maxCrecordalign)
  1140. else
  1141. usedalign:=used_align(maxalignment,aktalignment.recordalignmin,aktalignment.recordalignmax);
  1142. offset:=align(trecordsymtable(symtablestack).datasize,usedalign);
  1143. trecordsymtable(symtablestack).datasize:=offset+unionsymtable.datasize;
  1144. if maxalignment>trecordsymtable(symtablestack).fieldalignment then
  1145. trecordsymtable(symtablestack).fieldalignment:=maxalignment;
  1146. trecordsymtable(symtablestack).insertunionst(Unionsymtable,offset);
  1147. Unionsym.owner:=nil;
  1148. unionsym.free;
  1149. uniondef.owner:=nil;
  1150. uniondef.free;
  1151. end;
  1152. block_type:=old_block_type;
  1153. current_object_option:=old_current_object_option;
  1154. { free the list }
  1155. sc.free;
  1156. end;
  1157. end.
  1158. {
  1159. $Log$
  1160. Revision 1.75 2004-06-20 08:55:30 florian
  1161. * logs truncated
  1162. Revision 1.74 2004/06/16 20:07:09 florian
  1163. * dwarf branch merged
  1164. Revision 1.73 2004/04/11 12:38:16 peter
  1165. * fix calling convention problem when parsing default value before
  1166. the semicolon
  1167. Revision 1.72.2.5 2004/05/02 20:54:04 peter
  1168. * allow convert when choosing property function with index
  1169. Revision 1.72.2.4 2004/05/01 20:51:20 peter
  1170. * fix val code parameter
  1171. Revision 1.72.2.3 2004/04/28 19:55:52 peter
  1172. * new warning for ordinal-pointer when size is different
  1173. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  1174. }