pdecobj.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Does object types for Free Pascal
  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 pdecobj;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype,symdef;
  23. { parses a object declaration }
  24. function object_dec(const n : stringid;fd : tobjectdef) : tdef;
  25. implementation
  26. uses
  27. cutils,cclasses,
  28. globals,verbose,systems,tokens,
  29. symconst,symbase,symsym,symtable,defutil,defcmp,
  30. cgbase,
  31. node,nld,nmem,ncon,ncnv,ncal,pass_1,
  32. scanner,
  33. pbase,pexpr,pdecsub,pdecvar,ptype
  34. {$ifdef delphi}
  35. ,dmisc
  36. ,sysutils
  37. {$endif}
  38. ;
  39. const
  40. { Please leave this here, this module should NOT use
  41. these variables.
  42. Declaring it as string here results in an error when compiling (PFV) }
  43. current_procdef = 'error';
  44. function object_dec(const n : stringid;fd : tobjectdef) : tdef;
  45. { this function parses an object or class declaration }
  46. var
  47. there_is_a_destructor : boolean;
  48. classtype : tobjectdeftype;
  49. childof : tobjectdef;
  50. aktclass : tobjectdef;
  51. function constructor_head:tprocdef;
  52. var
  53. pd : tprocdef;
  54. begin
  55. consume(_CONSTRUCTOR);
  56. { must be at same level as in implementation }
  57. pd:=parse_proc_head(aktclass,potype_constructor);
  58. if (cs_constructor_name in aktglobalswitches) and
  59. (pd.procsym.name<>'INIT') then
  60. Message(parser_e_constructorname_must_be_init);
  61. consume(_SEMICOLON);
  62. include(aktclass.objectoptions,oo_has_constructor);
  63. { Set return type, class constructors return the
  64. created instance, object constructors return boolean }
  65. if is_class(pd._class) then
  66. pd.rettype.setdef(pd._class)
  67. else
  68. pd.rettype:=booltype;
  69. constructor_head:=pd;
  70. end;
  71. procedure property_dec;
  72. { convert a node tree to symlist and return the last
  73. symbol }
  74. function parse_symlist(pl:tsymlist):boolean;
  75. var
  76. idx : longint;
  77. sym : tsym;
  78. def : tdef;
  79. st : tsymtable;
  80. begin
  81. parse_symlist:=true;
  82. def:=nil;
  83. if token=_ID then
  84. begin
  85. sym:=search_class_member(aktclass,pattern);
  86. if assigned(sym) then
  87. begin
  88. case sym.typ of
  89. varsym :
  90. begin
  91. pl.addsym(sl_load,sym);
  92. def:=tvarsym(sym).vartype.def;
  93. end;
  94. procsym :
  95. begin
  96. pl.addsym(sl_call,sym);
  97. end;
  98. end;
  99. end
  100. else
  101. begin
  102. Message1(parser_e_illegal_field_or_method,pattern);
  103. parse_symlist:=false;
  104. end;
  105. consume(_ID);
  106. repeat
  107. case token of
  108. _ID,
  109. _SEMICOLON :
  110. begin
  111. break;
  112. end;
  113. _POINT :
  114. begin
  115. consume(_POINT);
  116. if assigned(def) then
  117. begin
  118. st:=def.getsymtable(gs_record);
  119. if assigned(st) then
  120. begin
  121. sym:=searchsymonlyin(st,pattern);
  122. if assigned(sym) then
  123. begin
  124. pl.addsym(sl_subscript,sym);
  125. case sym.typ of
  126. varsym :
  127. def:=tvarsym(sym).vartype.def;
  128. else
  129. begin
  130. Message1(sym_e_illegal_field,pattern);
  131. parse_symlist:=false;
  132. end;
  133. end;
  134. end
  135. else
  136. begin
  137. Message1(sym_e_illegal_field,pattern);
  138. parse_symlist:=false;
  139. end;
  140. end
  141. else
  142. begin
  143. Message(cg_e_invalid_qualifier);
  144. parse_symlist:=false;
  145. end;
  146. end;
  147. consume(_ID);
  148. end;
  149. _LECKKLAMMER :
  150. begin
  151. consume(_LECKKLAMMER);
  152. repeat
  153. if def.deftype=arraydef then
  154. begin
  155. idx:=get_intconst;
  156. pl.addconst(sl_vec,idx);
  157. def:=tarraydef(def).elementtype.def;
  158. end
  159. else
  160. begin
  161. Message(cg_e_invalid_qualifier);
  162. parse_symlist:=false;
  163. end;
  164. until not try_to_consume(_COMMA);
  165. consume(_RECKKLAMMER);
  166. end;
  167. else
  168. begin
  169. Message(parser_e_ill_property_access_sym);
  170. parse_symlist:=false;
  171. break;
  172. end;
  173. end;
  174. until false;
  175. end
  176. else
  177. begin
  178. Message(parser_e_ill_property_access_sym);
  179. parse_symlist:=false;
  180. end;
  181. pl.def:=def;
  182. end;
  183. var
  184. sym : tsym;
  185. p : tpropertysym;
  186. overriden : tsym;
  187. hs : string;
  188. varspez : tvarspez;
  189. s : string;
  190. tt : ttype;
  191. arraytype : ttype;
  192. pp : Tprocdef;
  193. pd : tprocdef;
  194. pt : tnode;
  195. propname : stringid;
  196. sc : tsinglelist;
  197. oldregisterdef : boolean;
  198. readvs,
  199. hvs : tvarsym;
  200. readprocdef,
  201. writeprocdef : tprocvardef;
  202. begin
  203. { check for a class }
  204. if not((is_class_or_interface(aktclass)) or
  205. ((m_delphi in aktmodeswitches) and (is_object(aktclass)))) then
  206. Message(parser_e_syntax_error);
  207. consume(_PROPERTY);
  208. { Generate temp procvardefs to search for matching read/write
  209. procedures. the readprocdef will store all definitions }
  210. oldregisterdef:=registerdef;
  211. registerdef:=false;
  212. readprocdef:=tprocvardef.create(normal_function_level);
  213. writeprocdef:=tprocvardef.create(normal_function_level);
  214. registerdef:=oldregisterdef;
  215. if token<>_ID then
  216. begin
  217. consume(_ID);
  218. consume(_SEMICOLON);
  219. exit;
  220. end;
  221. { Generate propertysym and insert in symtablestack }
  222. p:=tpropertysym.create(orgpattern);
  223. symtablestack.insert(p);
  224. propname:=pattern;
  225. consume(_ID);
  226. { Set the symtablestack to the parast of readprop so
  227. temp defs will be destroyed after declaration }
  228. readprocdef.parast.next:=symtablestack;
  229. symtablestack:=readprocdef.parast;
  230. { property parameters ? }
  231. if token=_LECKKLAMMER then
  232. begin
  233. if (sp_published in current_object_option) then
  234. Message(parser_e_cant_publish_that_property);
  235. { create a list of the parameters }
  236. sc:=tsinglelist.create;
  237. consume(_LECKKLAMMER);
  238. inc(testcurobject);
  239. repeat
  240. if token=_VAR then
  241. begin
  242. consume(_VAR);
  243. varspez:=vs_var;
  244. end
  245. else if token=_CONST then
  246. begin
  247. consume(_CONST);
  248. varspez:=vs_const;
  249. end
  250. else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  251. begin
  252. consume(_OUT);
  253. varspez:=vs_out;
  254. end
  255. else
  256. varspez:=vs_value;
  257. sc.reset;
  258. repeat
  259. readvs:=tvarsym.create(orgpattern,generrortype);
  260. readprocdef.parast.insert(readvs);
  261. sc.insert(readvs);
  262. consume(_ID);
  263. until not try_to_consume(_COMMA);
  264. if token=_COLON then
  265. begin
  266. consume(_COLON);
  267. if token=_ARRAY then
  268. begin
  269. consume(_ARRAY);
  270. consume(_OF);
  271. { define range and type of range }
  272. tt.setdef(tarraydef.create(0,-1,s32bittype));
  273. { define field type }
  274. single_type(arraytype,s,false);
  275. tarraydef(tt.def).setelementtype(arraytype);
  276. end
  277. else
  278. single_type(tt,s,false);
  279. end
  280. else
  281. tt:=cformaltype;
  282. readvs:=tvarsym(sc.first);
  283. while assigned(readvs) do
  284. begin
  285. readprocdef.concatpara(nil,tt,readvs,varspez,nil);
  286. { also update the writeprocdef }
  287. hvs:=tvarsym.create(readvs.realname,generrortype);
  288. writeprocdef.parast.insert(hvs);
  289. writeprocdef.concatpara(nil,tt,hvs,varspez,nil);
  290. readvs:=tvarsym(readvs.listnext);
  291. end;
  292. until not try_to_consume(_SEMICOLON);
  293. sc.free;
  294. dec(testcurobject);
  295. consume(_RECKKLAMMER);
  296. { the parser need to know if a property has parameters, the
  297. index parameter doesn't count (PFV) }
  298. if readprocdef.minparacount>0 then
  299. include(p.propoptions,ppo_hasparameters);
  300. end;
  301. { overriden property ? }
  302. { force property interface, if there is a property parameter }
  303. if (token=_COLON) or (readprocdef.minparacount>0) then
  304. begin
  305. consume(_COLON);
  306. single_type(p.proptype,hs,false);
  307. if (idtoken=_INDEX) then
  308. begin
  309. consume(_INDEX);
  310. pt:=comp_expr(true);
  311. if is_constnode(pt) and
  312. is_ordinal(pt.resulttype.def) and
  313. (not is_64bitint(pt.resulttype.def)) then
  314. p.index:=tordconstnode(pt).value
  315. else
  316. begin
  317. Message(parser_e_invalid_property_index_value);
  318. p.index:=0;
  319. end;
  320. p.indextype.setdef(pt.resulttype.def);
  321. include(p.propoptions,ppo_indexed);
  322. { concat a longint to the para templates }
  323. hvs:=tvarsym.create('$index',p.indextype);
  324. readprocdef.parast.insert(hvs);
  325. readprocdef.concatpara(nil,p.indextype,hvs,vs_value,nil);
  326. hvs:=tvarsym.create('$index',p.indextype);
  327. writeprocdef.parast.insert(hvs);
  328. writeprocdef.concatpara(nil,p.indextype,hvs,vs_value,nil);
  329. pt.free;
  330. end;
  331. end
  332. else
  333. begin
  334. { do an property override }
  335. overriden:=search_class_member(aktclass.childof,propname);
  336. if assigned(overriden) and (overriden.typ=propertysym) then
  337. begin
  338. p.dooverride(tpropertysym(overriden));
  339. end
  340. else
  341. begin
  342. p.proptype:=generrortype;
  343. message(parser_e_no_property_found_to_override);
  344. end;
  345. end;
  346. if (sp_published in current_object_option) and
  347. not(p.proptype.def.is_publishable) then
  348. Message(parser_e_cant_publish_that_property);
  349. if try_to_consume(_READ) then
  350. begin
  351. p.readaccess.clear;
  352. if parse_symlist(p.readaccess) then
  353. begin
  354. sym:=p.readaccess.firstsym^.sym;
  355. case sym.typ of
  356. procsym :
  357. begin
  358. { read is function returning the type of the property }
  359. readprocdef.rettype:=p.proptype;
  360. { Insert hidden parameters }
  361. calc_parast(readprocdef);
  362. { search procdefs matching readprocdef }
  363. pd:=Tprocsym(sym).search_procdef_bypara(readprocdef.para,p.proptype.def,true,false);
  364. if not(assigned(pd)) then
  365. Message(parser_e_ill_property_access_sym);
  366. p.readaccess.setdef(pd);
  367. end;
  368. varsym :
  369. begin
  370. if compare_defs(p.readaccess.def,p.proptype.def,nothingn)>=te_equal then
  371. begin
  372. { property parameters are allowed if this is
  373. an indexed property, because the index is then
  374. the parameter.
  375. Note: In the help of Kylix it is written
  376. that it isn't allowed, but the compiler accepts it (PFV) }
  377. if (ppo_hasparameters in p.propoptions) then
  378. Message(parser_e_ill_property_access_sym);
  379. end
  380. else
  381. CGMessage2(type_e_incompatible_types,p.readaccess.def.typename,p.proptype.def.typename);
  382. end;
  383. else
  384. Message(parser_e_ill_property_access_sym);
  385. end;
  386. end;
  387. end;
  388. if try_to_consume(_WRITE) then
  389. begin
  390. p.writeaccess.clear;
  391. if parse_symlist(p.writeaccess) then
  392. begin
  393. sym:=p.writeaccess.firstsym^.sym;
  394. case sym.typ of
  395. procsym :
  396. begin
  397. { write is a procedure with an extra value parameter
  398. of the of the property }
  399. writeprocdef.rettype:=voidtype;
  400. hvs:=tvarsym.create('$value',p.proptype);
  401. writeprocdef.parast.insert(hvs);
  402. writeprocdef.concatpara(nil,p.proptype,hvs,vs_value,nil);
  403. { Insert hidden parameters }
  404. calc_parast(writeprocdef);
  405. { search procdefs matching writeprocdef }
  406. pd:=Tprocsym(sym).search_procdef_bypara(writeprocdef.para,writeprocdef.rettype.def,true,false);
  407. if not(assigned(pd)) then
  408. Message(parser_e_ill_property_access_sym);
  409. p.writeaccess.setdef(pd);
  410. end;
  411. varsym :
  412. begin
  413. if compare_defs(p.writeaccess.def,p.proptype.def,nothingn)>=te_equal then
  414. begin
  415. { property parameters are allowed if this is
  416. an indexed property, because the index is then
  417. the parameter.
  418. Note: In the help of Kylix it is written
  419. that it isn't allowed, but the compiler accepts it (PFV) }
  420. if (ppo_hasparameters in p.propoptions) then
  421. Message(parser_e_ill_property_access_sym);
  422. end
  423. else
  424. CGMessage2(type_e_incompatible_types,p.readaccess.def.typename,p.proptype.def.typename);
  425. end;
  426. else
  427. Message(parser_e_ill_property_access_sym);
  428. end;
  429. end;
  430. end;
  431. include(p.propoptions,ppo_stored);
  432. if try_to_consume(_STORED) then
  433. begin
  434. p.storedaccess.clear;
  435. case token of
  436. _ID:
  437. begin
  438. { in the case that idtoken=_DEFAULT }
  439. { we have to do nothing except }
  440. { setting ppo_stored, it's the same }
  441. { as stored true }
  442. if idtoken<>_DEFAULT then
  443. begin
  444. if parse_symlist(p.storedaccess) then
  445. begin
  446. sym:=p.storedaccess.firstsym^.sym;
  447. case sym.typ of
  448. procsym :
  449. begin
  450. pp:=Tprocsym(sym).search_procdef_nopara_boolret;
  451. if assigned(pp) then
  452. p.storedaccess.setdef(pp)
  453. else
  454. message(parser_e_ill_property_storage_sym);
  455. end;
  456. varsym :
  457. begin
  458. if (ppo_hasparameters in p.propoptions) or
  459. not(is_boolean(p.storedaccess.def)) then
  460. Message(parser_e_stored_property_must_be_boolean);
  461. end;
  462. else
  463. Message(parser_e_ill_property_access_sym);
  464. end;
  465. end;
  466. end;
  467. end;
  468. _FALSE:
  469. begin
  470. consume(_FALSE);
  471. exclude(p.propoptions,ppo_stored);
  472. end;
  473. _TRUE:
  474. consume(_TRUE);
  475. end;
  476. end;
  477. if try_to_consume(_DEFAULT) then
  478. begin
  479. if not(is_ordinal(p.proptype.def) or
  480. is_64bitint(p.proptype.def) or
  481. ((p.proptype.def.deftype=setdef) and
  482. (tsetdef(p.proptype.def).settype=smallset))) or
  483. ((p.proptype.def.deftype=arraydef) and
  484. (ppo_indexed in p.propoptions)) or
  485. (ppo_hasparameters in p.propoptions) then
  486. Message(parser_e_property_cant_have_a_default_value);
  487. { Get the result of the default, the firstpass is
  488. needed to support values like -1 }
  489. pt:=comp_expr(true);
  490. if (p.proptype.def.deftype=setdef) and
  491. (pt.nodetype=arrayconstructorn) then
  492. begin
  493. arrayconstructor_to_set(pt);
  494. do_resulttypepass(pt);
  495. end;
  496. inserttypeconv(pt,p.proptype);
  497. if not(is_constnode(pt)) then
  498. Message(parser_e_property_default_value_must_const);
  499. if pt.nodetype=setconstn then
  500. p.default:=plongint(tsetconstnode(pt).value_set)^
  501. else
  502. p.default:=tordconstnode(pt).value;
  503. pt.free;
  504. end
  505. else if try_to_consume(_NODEFAULT) then
  506. begin
  507. p.default:=0;
  508. end;
  509. consume(_SEMICOLON);
  510. { default property ? }
  511. if try_to_consume(_DEFAULT) then
  512. begin
  513. include(p.propoptions,ppo_defaultproperty);
  514. if readprocdef.maxparacount=0 then
  515. message(parser_e_property_need_paras);
  516. consume(_SEMICOLON);
  517. end;
  518. { remove temporary procvardefs }
  519. symtablestack:=symtablestack.next;
  520. readprocdef.free;
  521. writeprocdef.free;
  522. end;
  523. function destructor_head:tprocdef;
  524. var
  525. pd : tprocdef;
  526. begin
  527. consume(_DESTRUCTOR);
  528. pd:=parse_proc_head(aktclass,potype_destructor);
  529. if (cs_constructor_name in aktglobalswitches) and
  530. (pd.procsym.name<>'DONE') then
  531. Message(parser_e_destructorname_must_be_done);
  532. if not(pd.Para.empty) and
  533. (m_fpc in aktmodeswitches) then
  534. Message(parser_e_no_paras_for_destructor);
  535. consume(_SEMICOLON);
  536. include(aktclass.objectoptions,oo_has_destructor);
  537. { no return value }
  538. pd.rettype:=voidtype;
  539. destructor_head:=pd;
  540. end;
  541. var
  542. hs : string;
  543. pcrd : tclassrefdef;
  544. tt : ttype;
  545. old_object_option : tsymoptions;
  546. old_current_procinfo : tprocinfo;
  547. oldparse_only : boolean;
  548. storetypecanbeforward : boolean;
  549. procedure setclassattributes;
  550. begin
  551. { publishable }
  552. if classtype in [odt_interfacecom,odt_class] then
  553. begin
  554. aktclass.objecttype:=classtype;
  555. if (cs_generate_rtti in aktlocalswitches) or
  556. (assigned(aktclass.childof) and
  557. (oo_can_have_published in aktclass.childof.objectoptions)) then
  558. begin
  559. include(aktclass.objectoptions,oo_can_have_published);
  560. { in "publishable" classes the default access type is published }
  561. current_object_option:=[sp_published];
  562. end;
  563. end;
  564. end;
  565. procedure setclassparent;
  566. begin
  567. if assigned(fd) then
  568. aktclass:=fd
  569. else
  570. aktclass:=tobjectdef.create(classtype,n,nil);
  571. { is the current class tobject? }
  572. { so you could define your own tobject }
  573. if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_class) and (upper(n)='TOBJECT') then
  574. class_tobject:=aktclass
  575. else if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  576. interface_iunknown:=aktclass
  577. else
  578. begin
  579. case classtype of
  580. odt_class:
  581. childof:=class_tobject;
  582. odt_interfacecom:
  583. childof:=interface_iunknown;
  584. end;
  585. if (oo_is_forward in childof.objectoptions) then
  586. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  587. aktclass.set_parent(childof);
  588. end;
  589. end;
  590. procedure setinterfacemethodoptions;
  591. var
  592. i: longint;
  593. defs: TIndexArray;
  594. pd: tprocdef;
  595. begin
  596. include(aktclass.objectoptions,oo_has_virtual);
  597. defs:=aktclass.symtable.defindex;
  598. for i:=1 to defs.count do
  599. begin
  600. pd:=tprocdef(defs.search(i));
  601. if pd.deftype=procdef then
  602. begin
  603. pd.extnumber:=aktclass.lastvtableindex;
  604. inc(aktclass.lastvtableindex);
  605. include(pd.procoptions,po_virtualmethod);
  606. pd.forwarddef:=false;
  607. end;
  608. end;
  609. end;
  610. function readobjecttype : boolean;
  611. begin
  612. readobjecttype:=true;
  613. { distinguish classes and objects }
  614. case token of
  615. _OBJECT:
  616. begin
  617. classtype:=odt_object;
  618. consume(_OBJECT)
  619. end;
  620. _CPPCLASS:
  621. begin
  622. classtype:=odt_cppclass;
  623. consume(_CPPCLASS);
  624. end;
  625. _INTERFACE:
  626. begin
  627. { need extra check here since interface is a keyword
  628. in all pascal modes }
  629. if not(m_class in aktmodeswitches) then
  630. Message(parser_f_need_objfpc_or_delphi_mode);
  631. if aktinterfacetype=it_interfacecom then
  632. classtype:=odt_interfacecom
  633. else {it_interfacecorba}
  634. classtype:=odt_interfacecorba;
  635. consume(_INTERFACE);
  636. { forward declaration }
  637. if not(assigned(fd)) and (token=_SEMICOLON) then
  638. begin
  639. { also anonym objects aren't allow (o : object a : longint; end;) }
  640. if n='' then
  641. Message(parser_f_no_anonym_objects);
  642. aktclass:=tobjectdef.create(classtype,n,nil);
  643. if (cs_compilesystem in aktmoduleswitches) and
  644. (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  645. interface_iunknown:=aktclass;
  646. include(aktclass.objectoptions,oo_is_forward);
  647. object_dec:=aktclass;
  648. typecanbeforward:=storetypecanbeforward;
  649. readobjecttype:=false;
  650. exit;
  651. end;
  652. end;
  653. _CLASS:
  654. begin
  655. classtype:=odt_class;
  656. consume(_CLASS);
  657. if not(assigned(fd)) and
  658. (token=_OF) and
  659. { Delphi only allows class of in type blocks.
  660. Note that when parsing the type of a variable declaration
  661. the blocktype is bt_type so the check for typecanbeforward
  662. is also necessary (PFV) }
  663. (((block_type=bt_type) and typecanbeforward) or
  664. not(m_delphi in aktmodeswitches)) then
  665. begin
  666. { a hack, but it's easy to handle }
  667. { class reference type }
  668. consume(_OF);
  669. single_type(tt,hs,typecanbeforward);
  670. { accept hp1, if is a forward def or a class }
  671. if (tt.def.deftype=forwarddef) or
  672. is_class(tt.def) then
  673. begin
  674. pcrd:=tclassrefdef.create(tt);
  675. object_dec:=pcrd;
  676. end
  677. else
  678. begin
  679. object_dec:=generrortype.def;
  680. Message1(type_e_class_type_expected,generrortype.def.typename);
  681. end;
  682. typecanbeforward:=storetypecanbeforward;
  683. readobjecttype:=false;
  684. exit;
  685. end
  686. { forward class }
  687. else if not(assigned(fd)) and (token=_SEMICOLON) then
  688. begin
  689. { also anonym objects aren't allow (o : object a : longint; end;) }
  690. if n='' then
  691. Message(parser_f_no_anonym_objects);
  692. aktclass:=tobjectdef.create(odt_class,n,nil);
  693. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  694. class_tobject:=aktclass;
  695. aktclass.objecttype:=odt_class;
  696. include(aktclass.objectoptions,oo_is_forward);
  697. { all classes must have a vmt !! at offset zero }
  698. if not(oo_has_vmt in aktclass.objectoptions) then
  699. aktclass.insertvmt;
  700. object_dec:=aktclass;
  701. typecanbeforward:=storetypecanbeforward;
  702. readobjecttype:=false;
  703. exit;
  704. end;
  705. end;
  706. else
  707. begin
  708. classtype:=odt_class; { this is error but try to recover }
  709. consume(_OBJECT);
  710. end;
  711. end;
  712. end;
  713. procedure handleimplementedinterface(implintf : tobjectdef);
  714. begin
  715. if not is_interface(implintf) then
  716. begin
  717. Message1(type_e_interface_type_expected,implintf.typename);
  718. exit;
  719. end;
  720. if aktclass.implementedinterfaces.searchintf(implintf)<>-1 then
  721. Message1(sym_e_duplicate_id,implintf.name)
  722. else
  723. begin
  724. { allocate and prepare the GUID only if the class
  725. implements some interfaces.
  726. }
  727. if aktclass.implementedinterfaces.count = 0 then
  728. aktclass.prepareguid;
  729. aktclass.implementedinterfaces.addintf(implintf);
  730. end;
  731. end;
  732. procedure readimplementedinterfaces;
  733. var
  734. tt : ttype;
  735. begin
  736. while try_to_consume(_COMMA) do
  737. begin
  738. id_type(tt,pattern,false);
  739. if (tt.def.deftype<>objectdef) then
  740. begin
  741. Message1(type_e_interface_type_expected,tt.def.typename);
  742. continue;
  743. end;
  744. handleimplementedinterface(tobjectdef(tt.def));
  745. end;
  746. end;
  747. procedure readinterfaceiid;
  748. var
  749. p : tnode;
  750. valid : boolean;
  751. begin
  752. p:=comp_expr(true);
  753. if p.nodetype=stringconstn then
  754. begin
  755. stringdispose(aktclass.iidstr);
  756. aktclass.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  757. p.free;
  758. valid:=string2guid(aktclass.iidstr^,aktclass.iidguid^);
  759. if (classtype=odt_interfacecom) and not assigned(aktclass.iidguid) and not valid then
  760. Message(parser_e_improper_guid_syntax);
  761. end
  762. else
  763. begin
  764. p.free;
  765. Message(cg_e_illegal_expression);
  766. end;
  767. end;
  768. procedure readparentclasses;
  769. var
  770. hp : tobjectdef;
  771. begin
  772. hp:=nil;
  773. { reads the parent class }
  774. if token=_LKLAMMER then
  775. begin
  776. consume(_LKLAMMER);
  777. id_type(tt,pattern,false);
  778. childof:=tobjectdef(tt.def);
  779. if (not assigned(childof)) or
  780. (childof.deftype<>objectdef) then
  781. begin
  782. if assigned(childof) then
  783. Message1(type_e_class_type_expected,childof.typename);
  784. childof:=nil;
  785. aktclass:=tobjectdef.create(classtype,n,nil);
  786. end
  787. else
  788. begin
  789. { a mix of class, interfaces, objects and cppclasses
  790. isn't allowed }
  791. case classtype of
  792. odt_class:
  793. if not(is_class(childof)) then
  794. begin
  795. if is_interface(childof) then
  796. begin
  797. { we insert the interface after the child
  798. is set, see below
  799. }
  800. hp:=childof;
  801. childof:=class_tobject;
  802. end
  803. else
  804. Message(parser_e_mix_of_classes_and_objects);
  805. end;
  806. odt_interfacecorba,
  807. odt_interfacecom:
  808. if not(is_interface(childof)) then
  809. Message(parser_e_mix_of_classes_and_objects);
  810. odt_cppclass:
  811. if not(is_cppclass(childof)) then
  812. Message(parser_e_mix_of_classes_and_objects);
  813. odt_object:
  814. if not(is_object(childof)) then
  815. Message(parser_e_mix_of_classes_and_objects);
  816. end;
  817. { the forward of the child must be resolved to get
  818. correct field addresses }
  819. if assigned(fd) then
  820. begin
  821. if (oo_is_forward in childof.objectoptions) then
  822. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  823. aktclass:=fd;
  824. { we must inherit several options !!
  825. this was missing !!
  826. all is now done in set_parent
  827. including symtable datasize setting PM }
  828. fd.set_parent(childof);
  829. end
  830. else
  831. aktclass:=tobjectdef.create(classtype,n,childof);
  832. if aktclass.objecttype=odt_class then
  833. begin
  834. if assigned(hp) then
  835. handleimplementedinterface(hp);
  836. readimplementedinterfaces;
  837. end;
  838. end;
  839. consume(_RKLAMMER);
  840. end
  841. { if no parent class, then a class get tobject as parent }
  842. else if classtype in [odt_class,odt_interfacecom] then
  843. setclassparent
  844. else
  845. aktclass:=tobjectdef.create(classtype,n,nil);
  846. { read GUID }
  847. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  848. try_to_consume(_LECKKLAMMER) then
  849. begin
  850. readinterfaceiid;
  851. consume(_RECKKLAMMER);
  852. end;
  853. end;
  854. procedure chkcpp(pd:tprocdef);
  855. begin
  856. if is_cppclass(pd._class) then
  857. begin
  858. pd.proccalloption:=pocall_cppdecl;
  859. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  860. end;
  861. end;
  862. var
  863. pd : tprocdef;
  864. begin
  865. old_object_option:=current_object_option;
  866. { forward is resolved }
  867. if assigned(fd) then
  868. exclude(fd.objectoptions,oo_is_forward);
  869. { objects and class types can't be declared local }
  870. if not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) then
  871. Message(parser_e_no_local_objects);
  872. storetypecanbeforward:=typecanbeforward;
  873. { for tp7 don't allow forward types }
  874. if (m_tp7 in aktmodeswitches) then
  875. typecanbeforward:=false;
  876. if not(readobjecttype) then
  877. exit;
  878. { also anonym objects aren't allow (o : object a : longint; end;) }
  879. if n='' then
  880. Message(parser_f_no_anonym_objects);
  881. { read list of parent classes }
  882. readparentclasses;
  883. { default access is public }
  884. there_is_a_destructor:=false;
  885. current_object_option:=[sp_public];
  886. { set class flags and inherits published }
  887. setclassattributes;
  888. aktobjectdef:=aktclass;
  889. aktclass.symtable.next:=symtablestack;
  890. symtablestack:=aktclass.symtable;
  891. testcurobject:=1;
  892. curobjectname:=Upper(n);
  893. { temp procinfo }
  894. old_current_procinfo:=current_procinfo;
  895. current_procinfo:=cprocinfo.create(nil);
  896. { short class declaration ? }
  897. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  898. begin
  899. { Parse componenten }
  900. repeat
  901. case token of
  902. _ID :
  903. begin
  904. case idtoken of
  905. _PRIVATE :
  906. begin
  907. if is_interface(aktclass) then
  908. Message(parser_e_no_access_specifier_in_interfaces);
  909. consume(_PRIVATE);
  910. current_object_option:=[sp_private];
  911. include(aktclass.objectoptions,oo_has_private);
  912. end;
  913. _PROTECTED :
  914. begin
  915. if is_interface(aktclass) then
  916. Message(parser_e_no_access_specifier_in_interfaces);
  917. consume(_PROTECTED);
  918. current_object_option:=[sp_protected];
  919. include(aktclass.objectoptions,oo_has_protected);
  920. end;
  921. _PUBLIC :
  922. begin
  923. if is_interface(aktclass) then
  924. Message(parser_e_no_access_specifier_in_interfaces);
  925. consume(_PUBLIC);
  926. current_object_option:=[sp_public];
  927. end;
  928. _PUBLISHED :
  929. begin
  930. { we've to check for a pushlished section in non- }
  931. { publishable classes later, if a real declaration }
  932. { this is the way, delphi does it }
  933. if is_interface(aktclass) then
  934. Message(parser_e_no_access_specifier_in_interfaces);
  935. consume(_PUBLISHED);
  936. current_object_option:=[sp_published];
  937. end;
  938. else
  939. begin
  940. if is_interface(aktclass) then
  941. Message(parser_e_no_vars_in_interfaces);
  942. if (sp_published in current_object_option) and
  943. not(oo_can_have_published in aktclass.objectoptions) then
  944. Message(parser_e_cant_have_published);
  945. read_var_decs(false,true,false);
  946. end;
  947. end;
  948. end;
  949. _PROPERTY :
  950. begin
  951. property_dec;
  952. end;
  953. _PROCEDURE,
  954. _FUNCTION,
  955. _CLASS :
  956. begin
  957. if (sp_published in current_object_option) and
  958. not(oo_can_have_published in aktclass.objectoptions) then
  959. Message(parser_e_cant_have_published);
  960. oldparse_only:=parse_only;
  961. parse_only:=true;
  962. pd:=parse_proc_dec(aktclass);
  963. { this is for error recovery as well as forward }
  964. { interface mappings, i.e. mapping to a method }
  965. { which isn't declared yet }
  966. if assigned(pd) then
  967. begin
  968. parse_object_proc_directives(pd);
  969. calc_parast(pd);
  970. { add definition to procsym }
  971. proc_add_definition(pd);
  972. { add procdef options to objectdef options }
  973. if (po_msgint in pd.procoptions) then
  974. include(aktclass.objectoptions,oo_has_msgint);
  975. if (po_msgstr in pd.procoptions) then
  976. include(aktclass.objectoptions,oo_has_msgstr);
  977. if (po_virtualmethod in pd.procoptions) then
  978. include(aktclass.objectoptions,oo_has_virtual);
  979. chkcpp(pd);
  980. end;
  981. parse_only:=oldparse_only;
  982. end;
  983. _CONSTRUCTOR :
  984. begin
  985. if (sp_published in current_object_option) and
  986. not(oo_can_have_published in aktclass.objectoptions) then
  987. Message(parser_e_cant_have_published);
  988. if not(sp_public in current_object_option) then
  989. Message(parser_w_constructor_should_be_public);
  990. if is_interface(aktclass) then
  991. Message(parser_e_no_con_des_in_interfaces);
  992. oldparse_only:=parse_only;
  993. parse_only:=true;
  994. pd:=constructor_head;
  995. parse_object_proc_directives(pd);
  996. calc_parast(pd);
  997. { add definition to procsym }
  998. proc_add_definition(pd);
  999. { add procdef options to objectdef options }
  1000. if (po_virtualmethod in pd.procoptions) then
  1001. include(aktclass.objectoptions,oo_has_virtual);
  1002. chkcpp(pd);
  1003. parse_only:=oldparse_only;
  1004. end;
  1005. _DESTRUCTOR :
  1006. begin
  1007. if (sp_published in current_object_option) and
  1008. not(oo_can_have_published in aktclass.objectoptions) then
  1009. Message(parser_e_cant_have_published);
  1010. if there_is_a_destructor then
  1011. Message(parser_n_only_one_destructor);
  1012. if is_interface(aktclass) then
  1013. Message(parser_e_no_con_des_in_interfaces);
  1014. if not(sp_public in current_object_option) then
  1015. Message(parser_w_destructor_should_be_public);
  1016. there_is_a_destructor:=true;
  1017. oldparse_only:=parse_only;
  1018. parse_only:=true;
  1019. pd:=destructor_head;
  1020. parse_object_proc_directives(pd);
  1021. calc_parast(pd);
  1022. { add definition to procsym }
  1023. proc_add_definition(pd);
  1024. { add procdef options to objectdef options }
  1025. if (po_virtualmethod in pd.procoptions) then
  1026. include(aktclass.objectoptions,oo_has_virtual);
  1027. chkcpp(pd);
  1028. parse_only:=oldparse_only;
  1029. end;
  1030. _END :
  1031. begin
  1032. consume(_END);
  1033. break;
  1034. end;
  1035. else
  1036. consume(_ID); { Give a ident expected message, like tp7 }
  1037. end;
  1038. until false;
  1039. end;
  1040. { generate vmt space if needed }
  1041. if not(oo_has_vmt in aktclass.objectoptions) and
  1042. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  1043. (classtype in [odt_class])
  1044. ) then
  1045. aktclass.insertvmt;
  1046. if is_interface(aktclass) then
  1047. setinterfacemethodoptions;
  1048. { reset }
  1049. testcurobject:=0;
  1050. curobjectname:='';
  1051. typecanbeforward:=storetypecanbeforward;
  1052. { restore old state }
  1053. symtablestack:=symtablestack.next;
  1054. aktobjectdef:=nil;
  1055. {Restore procinfo}
  1056. current_procinfo.free;
  1057. current_procinfo:=old_current_procinfo;
  1058. current_object_option:=old_object_option;
  1059. object_dec:=aktclass;
  1060. end;
  1061. end.
  1062. {
  1063. $Log$
  1064. Revision 1.63 2003-04-27 11:21:33 peter
  1065. * aktprocdef renamed to current_procdef
  1066. * procinfo renamed to current_procinfo
  1067. * procinfo will now be stored in current_module so it can be
  1068. cleaned up properly
  1069. * gen_main_procsym changed to create_main_proc and release_main_proc
  1070. to also generate a tprocinfo structure
  1071. * fixed unit implicit initfinal
  1072. Revision 1.62 2003/04/27 07:29:50 peter
  1073. * current_procdef cleanup, current_procdef is now always nil when parsing
  1074. a new procdef declaration
  1075. * aktprocsym removed
  1076. * lexlevel removed, use symtable.symtablelevel instead
  1077. * implicit init/final code uses the normal genentry/genexit
  1078. * funcret state checking updated for new funcret handling
  1079. Revision 1.61 2003/04/26 00:32:37 peter
  1080. * start search for overriden properties in the parent class
  1081. Revision 1.60 2003/04/25 20:59:33 peter
  1082. * removed funcretn,funcretsym, function result is now in varsym
  1083. and aliases for result and function name are added using absolutesym
  1084. * vs_hidden parameter for funcret passed in parameter
  1085. * vs_hidden fixes
  1086. * writenode changed to printnode and released from extdebug
  1087. * -vp option added to generate a tree.log with the nodetree
  1088. * nicer printnode for statements, callnode
  1089. Revision 1.59 2003/04/10 17:57:52 peter
  1090. * vs_hidden released
  1091. Revision 1.58 2003/01/09 21:52:37 peter
  1092. * merged some verbosity options.
  1093. * V_LineInfo is a verbosity flag to include line info
  1094. Revision 1.57 2002/11/25 17:43:21 peter
  1095. * splitted defbase in defutil,symutil,defcmp
  1096. * merged isconvertable and is_equal into compare_defs(_ext)
  1097. * made operator search faster by walking the list only once
  1098. Revision 1.56 2002/11/17 16:31:56 carl
  1099. * memory optimization (3-4%) : cleanup of tai fields,
  1100. cleanup of tdef and tsym fields.
  1101. * make it work for m68k
  1102. Revision 1.55 2002/10/05 12:43:25 carl
  1103. * fixes for Delphi 6 compilation
  1104. (warning : Some features do not work under Delphi)
  1105. Revision 1.54 2002/10/02 18:20:20 peter
  1106. * don't allow interface without m_class mode
  1107. Revision 1.53 2002/09/27 21:13:28 carl
  1108. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  1109. Revision 1.52 2002/09/16 14:11:13 peter
  1110. * add argument to equal_paras() to support default values or not
  1111. Revision 1.51 2002/09/09 17:34:15 peter
  1112. * tdicationary.replace added to replace and item in a dictionary. This
  1113. is only allowed for the same name
  1114. * varsyms are inserted in symtable before the types are parsed. This
  1115. fixes the long standing "var longint : longint" bug
  1116. - consume_idlist and idstringlist removed. The loops are inserted
  1117. at the callers place and uses the symtable for duplicate id checking
  1118. Revision 1.50 2002/09/03 16:26:26 daniel
  1119. * Make Tprocdef.defs protected
  1120. Revision 1.49 2002/08/17 09:23:38 florian
  1121. * first part of procinfo rewrite
  1122. Revision 1.48 2002/08/09 07:33:02 florian
  1123. * a couple of interface related fixes
  1124. Revision 1.47 2002/07/20 11:57:55 florian
  1125. * types.pas renamed to defbase.pas because D6 contains a types
  1126. unit so this would conflicts if D6 programms are compiled
  1127. + Willamette/SSE2 instructions to assembler added
  1128. Revision 1.46 2002/07/01 16:23:53 peter
  1129. * cg64 patch
  1130. * basics for currency
  1131. * asnode updates for class and interface (not finished)
  1132. Revision 1.45 2002/05/18 13:34:12 peter
  1133. * readded missing revisions
  1134. Revision 1.44 2002/05/16 19:46:42 carl
  1135. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1136. + try to fix temp allocation (still in ifdef)
  1137. + generic constructor calls
  1138. + start of tassembler / tmodulebase class cleanup
  1139. Revision 1.42 2002/05/12 16:53:08 peter
  1140. * moved entry and exitcode to ncgutil and cgobj
  1141. * foreach gets extra argument for passing local data to the
  1142. iterator function
  1143. * -CR checks also class typecasts at runtime by changing them
  1144. into as
  1145. * fixed compiler to cycle with the -CR option
  1146. * fixed stabs with elf writer, finally the global variables can
  1147. be watched
  1148. * removed a lot of routines from cga unit and replaced them by
  1149. calls to cgobj
  1150. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1151. u32bit then the other is typecasted also to u32bit without giving
  1152. a rangecheck warning/error.
  1153. * fixed pascal calling method with reversing also the high tree in
  1154. the parast, detected by tcalcst3 test
  1155. Revision 1.41 2002/04/21 19:02:04 peter
  1156. * removed newn and disposen nodes, the code is now directly
  1157. inlined from pexpr
  1158. * -an option that will write the secondpass nodes to the .s file, this
  1159. requires EXTDEBUG define to actually write the info
  1160. * fixed various internal errors and crashes due recent code changes
  1161. Revision 1.40 2002/04/19 15:46:02 peter
  1162. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1163. in most cases and not written to the ppu
  1164. * add mangeledname_prefix() routine to generate the prefix of
  1165. manglednames depending on the current procedure, object and module
  1166. * removed static procprefix since the mangledname is now build only
  1167. on demand from tprocdef.mangledname
  1168. Revision 1.39 2002/04/04 19:06:00 peter
  1169. * removed unused units
  1170. * use tlocation.size in cg.a_*loc*() routines
  1171. Revision 1.38 2002/01/25 17:38:19 peter
  1172. * fixed default value for properties with index values
  1173. Revision 1.37 2002/01/24 18:25:48 peter
  1174. * implicit result variable generation for assembler routines
  1175. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1176. Revision 1.36 2002/01/06 12:08:15 peter
  1177. * removed uauto from orddef, use new range_to_basetype generating
  1178. the correct ordinal type for a range
  1179. }