pdecobj.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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. aasm,symconst,symbase,symsym,symtable,types,
  30. hcodegen,
  31. node,nld,ncon,ncnv,nobj,pass_1,
  32. scanner,
  33. pbase,pexpr,pdecsub,pdecvar,ptype;
  34. function object_dec(const n : stringid;fd : tobjectdef) : tdef;
  35. { this function parses an object or class declaration }
  36. var
  37. actmembertype : tsymoptions;
  38. there_is_a_destructor : boolean;
  39. classtype : tobjectdeftype;
  40. childof : tobjectdef;
  41. aktclass : tobjectdef;
  42. procedure constructor_head;
  43. begin
  44. consume(_CONSTRUCTOR);
  45. { must be at same level as in implementation }
  46. inc(lexlevel);
  47. parse_proc_head(potype_constructor);
  48. dec(lexlevel);
  49. if (cs_constructor_name in aktglobalswitches) and (aktprocsym.name<>'INIT') then
  50. Message(parser_e_constructorname_must_be_init);
  51. include(aktclass.objectoptions,oo_has_constructor);
  52. consume(_SEMICOLON);
  53. begin
  54. if is_class(aktclass) then
  55. begin
  56. { CLASS constructors return the created instance }
  57. aktprocsym.definition.rettype.def:=aktclass;
  58. end
  59. else
  60. begin
  61. { OBJECT constructors return a boolean }
  62. aktprocsym.definition.rettype:=booltype;
  63. end;
  64. end;
  65. end;
  66. procedure property_dec;
  67. var
  68. sym : tsym;
  69. propertyparas : tlinkedlist;
  70. { returns the matching procedure to access a property }
  71. function get_procdef : tprocdef;
  72. var
  73. p : tprocdef;
  74. begin
  75. p:=tprocsym(sym).definition;
  76. get_procdef:=nil;
  77. while assigned(p) do
  78. begin
  79. if equal_paras(p.para,propertyparas,cp_value_equal_const) or convertable_paras(p.para,propertyparas,cp_value_equal_const) then {MvdV: Ozerski 14.03.01}
  80. break;
  81. p:=p.nextoverloaded;
  82. end;
  83. get_procdef:=p;
  84. end;
  85. var
  86. hp2,datacoll : tparaitem;
  87. p : tpropertysym;
  88. overriden : tsym;
  89. hs : string;
  90. varspez : tvarspez;
  91. sc : tidstringlist;
  92. s : string;
  93. tt : ttype;
  94. declarepos : tfileposinfo;
  95. pp : tprocdef;
  96. pt : tnode;
  97. propname : stringid;
  98. begin
  99. { check for a class }
  100. if not((is_class_or_interface(aktclass)) or
  101. ((m_delphi in aktmodeswitches) and (is_object(aktclass)))) then
  102. Message(parser_e_syntax_error);
  103. consume(_PROPERTY);
  104. propertyparas:=TParaLinkedList.Create;
  105. datacoll:=nil;
  106. if token=_ID then
  107. begin
  108. p:=tpropertysym.create(orgpattern);
  109. propname:=pattern;
  110. consume(_ID);
  111. { property parameters ? }
  112. if token=_LECKKLAMMER then
  113. begin
  114. if (sp_published in current_object_option) then
  115. Message(parser_e_cant_publish_that_property);
  116. { create a list of the parameters in propertyparas }
  117. consume(_LECKKLAMMER);
  118. inc(testcurobject);
  119. repeat
  120. if token=_VAR then
  121. begin
  122. consume(_VAR);
  123. varspez:=vs_var;
  124. end
  125. else if token=_CONST then
  126. begin
  127. consume(_CONST);
  128. varspez:=vs_const;
  129. end
  130. else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  131. begin
  132. consume(_OUT);
  133. varspez:=vs_out;
  134. end
  135. else varspez:=vs_value;
  136. sc:=consume_idlist;
  137. {$ifdef fixLeaksOnError}
  138. strContStack.push(sc);
  139. {$endif fixLeaksOnError}
  140. if token=_COLON then
  141. begin
  142. consume(_COLON);
  143. if token=_ARRAY then
  144. begin
  145. {
  146. if (varspez<>vs_const) and
  147. (varspez<>vs_var) then
  148. begin
  149. varspez:=vs_const;
  150. Message(parser_e_illegal_open_parameter);
  151. end;
  152. }
  153. consume(_ARRAY);
  154. consume(_OF);
  155. { define range and type of range }
  156. tt.setdef(tarraydef.create(0,-1,s32bittype));
  157. { define field type }
  158. single_type(tarraydef(tt.def).elementtype,s,false);
  159. end
  160. else
  161. single_type(tt,s,false);
  162. end
  163. else
  164. tt:=cformaltype;
  165. repeat
  166. s:=sc.get(declarepos);
  167. if s='' then
  168. break;
  169. hp2:=TParaItem.create;
  170. hp2.paratyp:=varspez;
  171. hp2.paratype:=tt;
  172. propertyparas.insert(hp2);
  173. until false;
  174. {$ifdef fixLeaksOnError}
  175. if strContStack.pop <> sc then
  176. writeln('problem with strContStack in ptype');
  177. {$endif fixLeaksOnError}
  178. sc.free;
  179. until not try_to_consume(_SEMICOLON);
  180. dec(testcurobject);
  181. consume(_RECKKLAMMER);
  182. end;
  183. { overriden property ? }
  184. { force property interface, if there is a property parameter }
  185. if (token=_COLON) or not(propertyparas.empty) then
  186. begin
  187. consume(_COLON);
  188. single_type(p.proptype,hs,false);
  189. if (idtoken=_INDEX) then
  190. begin
  191. consume(_INDEX);
  192. pt:=comp_expr(true);
  193. if is_constnode(pt) and
  194. is_ordinal(pt.resulttype.def) and
  195. (not is_64bitint(pt.resulttype.def)) then
  196. p.index:=tordconstnode(pt).value
  197. else
  198. begin
  199. Message(parser_e_invalid_property_index_value);
  200. p.index:=0;
  201. end;
  202. p.indextype.setdef(pt.resulttype.def);
  203. include(p.propoptions,ppo_indexed);
  204. { concat a longint to the para template }
  205. hp2:=TParaItem.Create;
  206. hp2.paratyp:=vs_value;
  207. hp2.paratype:=p.indextype;
  208. propertyparas.insert(hp2);
  209. pt.free;
  210. end;
  211. { the parser need to know if a property has parameters }
  212. if not(propertyparas.empty) then
  213. include(p.propoptions,ppo_hasparameters);
  214. end
  215. else
  216. begin
  217. { do an property override }
  218. overriden:=search_class_member(aktclass,propname);
  219. if assigned(overriden) and (overriden.typ=propertysym) then
  220. begin
  221. p.dooverride(tpropertysym(overriden));
  222. end
  223. else
  224. begin
  225. p.proptype:=generrortype;
  226. message(parser_e_no_property_found_to_override);
  227. end;
  228. end;
  229. if (sp_published in current_object_option) and
  230. not(p.proptype.def.is_publishable) then
  231. Message(parser_e_cant_publish_that_property);
  232. { create data defcoll to allow correct parameter checks }
  233. datacoll:=TParaItem.Create;
  234. datacoll.paratyp:=vs_value;
  235. datacoll.paratype:=p.proptype;
  236. if (idtoken=_READ) then
  237. begin
  238. p.readaccess.clear;
  239. consume(_READ);
  240. sym:=search_class_member(aktclass,pattern);
  241. if not(assigned(sym)) then
  242. begin
  243. Message1(sym_e_unknown_id,pattern);
  244. consume(_ID);
  245. end
  246. else
  247. begin
  248. consume(_ID);
  249. while (token=_POINT) and
  250. ((sym.typ=varsym) and
  251. (tvarsym(sym).vartype.def.deftype=recorddef)) do
  252. begin
  253. p.readaccess.addsym(sym);
  254. consume(_POINT);
  255. sym:=searchsymonlyin(trecorddef(tvarsym(sym).vartype.def).symtable,pattern);
  256. if not assigned(sym) then
  257. Message1(sym_e_illegal_field,pattern);
  258. consume(_ID);
  259. end;
  260. end;
  261. if assigned(sym) then
  262. begin
  263. { search the matching definition }
  264. case sym.typ of
  265. procsym :
  266. begin
  267. pp:=get_procdef;
  268. if not(assigned(pp)) or
  269. not(is_equal(pp.rettype.def,p.proptype.def)) then
  270. Message(parser_e_ill_property_access_sym);
  271. p.readaccess.setdef(pp);
  272. end;
  273. varsym :
  274. begin
  275. if not(propertyparas.empty) or
  276. not(is_equal(tvarsym(sym).vartype.def,p.proptype.def)) then
  277. Message(parser_e_ill_property_access_sym);
  278. end;
  279. else
  280. Message(parser_e_ill_property_access_sym);
  281. end;
  282. p.readaccess.addsym(sym);
  283. end;
  284. end;
  285. if (idtoken=_WRITE) then
  286. begin
  287. p.writeaccess.clear;
  288. consume(_WRITE);
  289. sym:=search_class_member(aktclass,pattern);
  290. if not(assigned(sym)) then
  291. begin
  292. Message1(sym_e_unknown_id,pattern);
  293. consume(_ID);
  294. end
  295. else
  296. begin
  297. consume(_ID);
  298. while (token=_POINT) and
  299. ((sym.typ=varsym) and
  300. (tvarsym(sym).vartype.def.deftype=recorddef)) do
  301. begin
  302. p.writeaccess.addsym(sym);
  303. consume(_POINT);
  304. sym:=searchsymonlyin(trecorddef(tvarsym(sym).vartype.def).symtable,pattern);
  305. if not assigned(sym) then
  306. Message1(sym_e_illegal_field,pattern);
  307. consume(_ID);
  308. end;
  309. end;
  310. if assigned(sym) then
  311. begin
  312. { search the matching definition }
  313. case sym.typ of
  314. procsym :
  315. begin
  316. { insert data entry to check access method }
  317. propertyparas.insert(datacoll);
  318. pp:=get_procdef;
  319. { ... and remove it }
  320. propertyparas.remove(datacoll);
  321. if not(assigned(pp)) then
  322. Message(parser_e_ill_property_access_sym);
  323. p.writeaccess.setdef(pp);
  324. end;
  325. varsym :
  326. begin
  327. if not(propertyparas.empty) or
  328. not(is_equal(tvarsym(sym).vartype.def,p.proptype.def)) then
  329. Message(parser_e_ill_property_access_sym);
  330. end
  331. else
  332. Message(parser_e_ill_property_access_sym);
  333. end;
  334. p.writeaccess.addsym(sym);
  335. end;
  336. end;
  337. include(p.propoptions,ppo_stored);
  338. if (idtoken=_STORED) then
  339. begin
  340. consume(_STORED);
  341. p.storedaccess.clear;
  342. case token of
  343. _ID:
  344. { in the case that idtoken=_DEFAULT }
  345. { we have to do nothing except }
  346. { setting ppo_stored, it's the same }
  347. { as stored true }
  348. if idtoken<>_DEFAULT then
  349. begin
  350. sym:=search_class_member(aktclass,pattern);
  351. if not(assigned(sym)) then
  352. begin
  353. Message1(sym_e_unknown_id,pattern);
  354. consume(_ID);
  355. end
  356. else
  357. begin
  358. consume(_ID);
  359. while (token=_POINT) and
  360. ((sym.typ=varsym) and
  361. (tvarsym(sym).vartype.def.deftype=recorddef)) do
  362. begin
  363. p.storedaccess.addsym(sym);
  364. consume(_POINT);
  365. sym:=searchsymonlyin(trecorddef(tvarsym(sym).vartype.def).symtable,pattern);
  366. if not assigned(sym) then
  367. Message1(sym_e_illegal_field,pattern);
  368. consume(_ID);
  369. end;
  370. end;
  371. if assigned(sym) then
  372. begin
  373. { only non array properties can be stored }
  374. case sym.typ of
  375. procsym :
  376. begin
  377. pp:=tprocsym(sym).definition;
  378. while assigned(pp) do
  379. begin
  380. { the stored function shouldn't have any parameters }
  381. if pp.Para.empty then
  382. break;
  383. pp:=pp.nextoverloaded;
  384. end;
  385. { found we a procedure and does it really return a bool? }
  386. if not(assigned(pp)) or
  387. not(is_boolean(pp.rettype.def)) then
  388. Message(parser_e_ill_property_storage_sym);
  389. p.storedaccess.setdef(pp);
  390. end;
  391. varsym :
  392. begin
  393. if not(propertyparas.empty) or
  394. not(is_boolean(tvarsym(sym).vartype.def)) then
  395. Message(parser_e_stored_property_must_be_boolean);
  396. end;
  397. else
  398. Message(parser_e_ill_property_storage_sym);
  399. end;
  400. p.storedaccess.addsym(sym);
  401. end;
  402. end;
  403. _FALSE:
  404. begin
  405. consume(_FALSE);
  406. exclude(p.propoptions,ppo_stored);
  407. end;
  408. _TRUE:
  409. consume(_TRUE);
  410. end;
  411. end;
  412. if (idtoken=_DEFAULT) then
  413. begin
  414. consume(_DEFAULT);
  415. if not(is_ordinal(p.proptype.def) or
  416. is_64bitint(p.proptype.def) or
  417. ((p.proptype.def.deftype=setdef) and
  418. (tsetdef(p.proptype.def).settype=smallset))) or
  419. not(propertyparas.empty) then
  420. Message(parser_e_property_cant_have_a_default_value);
  421. { Get the result of the default, the firstpass is
  422. needed to support values like -1 }
  423. pt:=comp_expr(true);
  424. if (p.proptype.def.deftype=setdef) and
  425. (pt.nodetype=arrayconstructorn) then
  426. begin
  427. arrayconstructor_to_set(tarrayconstructornode(pt));
  428. do_resulttypepass(pt);
  429. end;
  430. inserttypeconv(pt,p.proptype);
  431. if not(is_constnode(pt)) then
  432. Message(parser_e_property_default_value_must_const);
  433. if pt.nodetype=setconstn then
  434. p.default:=plongint(tsetconstnode(pt).value_set)^
  435. else
  436. p.default:=tordconstnode(pt).value;
  437. pt.free;
  438. end
  439. else if (idtoken=_NODEFAULT) then
  440. begin
  441. consume(_NODEFAULT);
  442. p.default:=0;
  443. end;
  444. symtablestack.insert(p);
  445. { default property ? }
  446. consume(_SEMICOLON);
  447. if (idtoken=_DEFAULT) then
  448. begin
  449. consume(_DEFAULT);
  450. { overriding a default propertyp is allowed
  451. p2:=search_default_property(aktclass);
  452. if assigned(p2) then
  453. message1(parser_e_only_one_default_property,
  454. tobjectdef(p2.owner.defowner)^.objname^)
  455. else
  456. }
  457. begin
  458. include(p.propoptions,ppo_defaultproperty);
  459. if propertyparas.empty then
  460. message(parser_e_property_need_paras);
  461. end;
  462. consume(_SEMICOLON);
  463. end;
  464. { clean up }
  465. if assigned(datacoll) then
  466. datacoll.free;
  467. end
  468. else
  469. begin
  470. consume(_ID);
  471. consume(_SEMICOLON);
  472. end;
  473. propertyparas.free;
  474. end;
  475. procedure destructor_head;
  476. begin
  477. consume(_DESTRUCTOR);
  478. inc(lexlevel);
  479. parse_proc_head(potype_destructor);
  480. dec(lexlevel);
  481. if (cs_constructor_name in aktglobalswitches) and (aktprocsym.name<>'DONE') then
  482. Message(parser_e_destructorname_must_be_done);
  483. include(aktclass.objectoptions,oo_has_destructor);
  484. consume(_SEMICOLON);
  485. if not(aktprocsym.definition.Para.empty) then
  486. if not (m_tp in aktmodeswitches) then
  487. Message(parser_e_no_paras_for_destructor);
  488. { no return value }
  489. aktprocsym.definition.rettype:=voidtype;
  490. end;
  491. var
  492. hs : string;
  493. pcrd : tclassrefdef;
  494. tt : ttype;
  495. oldprocinfo : pprocinfo;
  496. oldprocsym : tprocsym;
  497. oldparse_only : boolean;
  498. storetypecanbeforward : boolean;
  499. procedure setclassattributes;
  500. begin
  501. if classtype=odt_class then
  502. begin
  503. aktclass.objecttype:=odt_class;
  504. if (cs_generate_rtti in aktlocalswitches) or
  505. (assigned(aktclass.childof) and
  506. (oo_can_have_published in aktclass.childof.objectoptions)) then
  507. begin
  508. include(aktclass.objectoptions,oo_can_have_published);
  509. { in "publishable" classes the default access type is published }
  510. actmembertype:=[sp_published];
  511. { don't know if this is necessary (FK) }
  512. current_object_option:=[sp_published];
  513. end;
  514. end;
  515. end;
  516. procedure setclassparent;
  517. begin
  518. if assigned(fd) then
  519. aktclass:=fd
  520. else
  521. aktclass:=tobjectdef.create(classtype,n,nil);
  522. { is the current class tobject? }
  523. { so you could define your own tobject }
  524. if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_class) and (upper(n)='TOBJECT') then
  525. class_tobject:=aktclass
  526. else if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  527. interface_iunknown:=aktclass
  528. else
  529. begin
  530. case classtype of
  531. odt_class:
  532. childof:=class_tobject;
  533. odt_interfacecom:
  534. childof:=interface_iunknown;
  535. end;
  536. if (oo_is_forward in childof.objectoptions) then
  537. Message1(parser_e_forward_declaration_must_be_resolved,childof.objname^);
  538. aktclass.set_parent(childof);
  539. end;
  540. end;
  541. procedure setinterfacemethodoptions;
  542. var
  543. i: longint;
  544. defs: TIndexArray;
  545. pd: tprocdef;
  546. begin
  547. include(aktclass.objectoptions,oo_has_virtual);
  548. defs:=aktclass.symtable.defindex;
  549. for i:=1 to defs.count do
  550. begin
  551. pd:=tprocdef(defs.search(i));
  552. if pd.deftype=procdef then
  553. begin
  554. pd.extnumber:=aktclass.lastvtableindex;
  555. inc(aktclass.lastvtableindex);
  556. include(pd.procoptions,po_virtualmethod);
  557. pd.forwarddef:=false;
  558. end;
  559. end;
  560. end;
  561. function readobjecttype : boolean;
  562. begin
  563. readobjecttype:=true;
  564. { distinguish classes and objects }
  565. case token of
  566. _OBJECT:
  567. begin
  568. classtype:=odt_object;
  569. consume(_OBJECT)
  570. end;
  571. _CPPCLASS:
  572. begin
  573. classtype:=odt_cppclass;
  574. consume(_CPPCLASS);
  575. end;
  576. _INTERFACE:
  577. begin
  578. if aktinterfacetype=it_interfacecom then
  579. classtype:=odt_interfacecom
  580. else {it_interfacecorba}
  581. classtype:=odt_interfacecorba;
  582. consume(_INTERFACE);
  583. { forward declaration }
  584. if not(assigned(fd)) and (token=_SEMICOLON) then
  585. begin
  586. { also anonym objects aren't allow (o : object a : longint; end;) }
  587. if n='' then
  588. Message(parser_f_no_anonym_objects);
  589. aktclass:=tobjectdef.create(classtype,n,nil);
  590. if (cs_compilesystem in aktmoduleswitches) and
  591. (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  592. interface_iunknown:=aktclass;
  593. include(aktclass.objectoptions,oo_is_forward);
  594. object_dec:=aktclass;
  595. typecanbeforward:=storetypecanbeforward;
  596. readobjecttype:=false;
  597. exit;
  598. end;
  599. end;
  600. _CLASS:
  601. begin
  602. classtype:=odt_class;
  603. consume(_CLASS);
  604. if not(assigned(fd)) and
  605. (token=_OF) and
  606. { Delphi only allows class of in type blocks.
  607. Note that when parsing the type of a variable declaration
  608. the blocktype is bt_type so the check for typecanbeforward
  609. is also necessary (PFV) }
  610. (((block_type=bt_type) and typecanbeforward) or
  611. not(m_delphi in aktmodeswitches)) then
  612. begin
  613. { a hack, but it's easy to handle }
  614. { class reference type }
  615. consume(_OF);
  616. single_type(tt,hs,typecanbeforward);
  617. { accept hp1, if is a forward def or a class }
  618. if (tt.def.deftype=forwarddef) or
  619. is_class(tt.def) then
  620. begin
  621. pcrd:=tclassrefdef.create(tt);
  622. object_dec:=pcrd;
  623. end
  624. else
  625. begin
  626. object_dec:=generrortype.def;
  627. Message1(type_e_class_type_expected,generrortype.def.typename);
  628. end;
  629. typecanbeforward:=storetypecanbeforward;
  630. readobjecttype:=false;
  631. exit;
  632. end
  633. { forward class }
  634. else if not(assigned(fd)) and (token=_SEMICOLON) then
  635. begin
  636. { also anonym objects aren't allow (o : object a : longint; end;) }
  637. if n='' then
  638. Message(parser_f_no_anonym_objects);
  639. aktclass:=tobjectdef.create(odt_class,n,nil);
  640. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  641. class_tobject:=aktclass;
  642. aktclass.objecttype:=odt_class;
  643. include(aktclass.objectoptions,oo_is_forward);
  644. { all classes must have a vmt !! at offset zero }
  645. if not(oo_has_vmt in aktclass.objectoptions) then
  646. aktclass.insertvmt;
  647. object_dec:=aktclass;
  648. typecanbeforward:=storetypecanbeforward;
  649. readobjecttype:=false;
  650. exit;
  651. end;
  652. end;
  653. else
  654. begin
  655. classtype:=odt_class; { this is error but try to recover }
  656. consume(_OBJECT);
  657. end;
  658. end;
  659. end;
  660. procedure readimplementedinterfaces;
  661. var
  662. implintf: tobjectdef;
  663. tt : ttype;
  664. begin
  665. while try_to_consume(_COMMA) do begin
  666. id_type(tt,pattern,false);
  667. implintf:=tobjectdef(tt.def);
  668. if (tt.def.deftype<>objectdef) then begin
  669. Message1(type_e_interface_type_expected,tt.def.typename);
  670. Continue; { omit }
  671. end;
  672. if not is_interface(implintf) then begin
  673. Message1(type_e_interface_type_expected,implintf.typename);
  674. Continue; { omit }
  675. end;
  676. if aktclass.implementedinterfaces.searchintf(tt.def)<>-1 then
  677. Message1(sym_e_duplicate_id,tt.def.name)
  678. else
  679. aktclass.implementedinterfaces.addintf(tt.def);
  680. end;
  681. end;
  682. procedure readinterfaceiid;
  683. var
  684. p : tnode;
  685. begin
  686. p:=comp_expr(true);
  687. if p.nodetype=stringconstn then
  688. begin
  689. aktclass.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  690. p.free;
  691. aktclass.isiidguidvalid:=string2guid(aktclass.iidstr^,aktclass.iidguid);
  692. if (classtype=odt_interfacecom) and not aktclass.isiidguidvalid then
  693. Message(parser_e_improper_guid_syntax);
  694. end
  695. else
  696. begin
  697. p.free;
  698. Message(cg_e_illegal_expression);
  699. end;
  700. end;
  701. procedure readparentclasses;
  702. begin
  703. { reads the parent class }
  704. if token=_LKLAMMER then
  705. begin
  706. consume(_LKLAMMER);
  707. id_type(tt,pattern,false);
  708. childof:=tobjectdef(tt.def);
  709. if (not assigned(childof)) or
  710. (childof.deftype<>objectdef) then
  711. begin
  712. if assigned(childof) then
  713. Message1(type_e_class_type_expected,childof.typename);
  714. childof:=nil;
  715. aktclass:=tobjectdef.create(classtype,n,nil);
  716. end
  717. else
  718. begin
  719. { a mix of class, interfaces, objects and cppclasses
  720. isn't allowed }
  721. case classtype of
  722. odt_class:
  723. if not(is_class(childof)) and
  724. not(is_interface(childof)) then
  725. Message(parser_e_mix_of_classes_and_objects);
  726. odt_interfacecorba,
  727. odt_interfacecom:
  728. if not(is_interface(childof)) then
  729. Message(parser_e_mix_of_classes_and_objects);
  730. odt_cppclass:
  731. if not(is_cppclass(childof)) then
  732. Message(parser_e_mix_of_classes_and_objects);
  733. odt_object:
  734. if not(is_object(childof)) then
  735. Message(parser_e_mix_of_classes_and_objects);
  736. end;
  737. { the forward of the child must be resolved to get
  738. correct field addresses }
  739. if assigned(fd) then
  740. begin
  741. if (oo_is_forward in childof.objectoptions) then
  742. Message1(parser_e_forward_declaration_must_be_resolved,childof.objname^);
  743. aktclass:=fd;
  744. { we must inherit several options !!
  745. this was missing !!
  746. all is now done in set_parent
  747. including symtable datasize setting PM }
  748. fd.set_parent(childof);
  749. end
  750. else
  751. aktclass:=tobjectdef.create(classtype,n,childof);
  752. if aktclass.objecttype=odt_class then
  753. readimplementedinterfaces;
  754. end;
  755. consume(_RKLAMMER);
  756. end
  757. { if no parent class, then a class get tobject as parent }
  758. else if classtype in [odt_class,odt_interfacecom] then
  759. setclassparent
  760. else
  761. aktclass:=tobjectdef.create(classtype,n,nil);
  762. { read GUID }
  763. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  764. try_to_consume(_LECKKLAMMER) then
  765. begin
  766. readinterfaceiid;
  767. consume(_RECKKLAMMER);
  768. end;
  769. end;
  770. procedure chkcpp;
  771. begin
  772. if is_cppclass(aktclass) then
  773. begin
  774. include(aktprocsym.definition.proccalloptions,pocall_cppdecl);
  775. aktprocsym.definition.setmangledname(
  776. target_info.Cprefix+aktprocsym.definition.cplusplusmangledname);
  777. end;
  778. end;
  779. var
  780. temppd : tprocdef;
  781. ch : tclassheader;
  782. begin
  783. {Nowadays aktprocsym may already have a value, so we need to save
  784. it.}
  785. oldprocsym:=aktprocsym;
  786. { forward is resolved }
  787. if assigned(fd) then
  788. exclude(fd.objectoptions,oo_is_forward);
  789. there_is_a_destructor:=false;
  790. actmembertype:=[sp_public];
  791. { objects and class types can't be declared local }
  792. if not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) then
  793. Message(parser_e_no_local_objects);
  794. storetypecanbeforward:=typecanbeforward;
  795. { for tp mode don't allow forward types }
  796. if (m_tp in aktmodeswitches) and
  797. not (m_delphi in aktmodeswitches) then
  798. typecanbeforward:=false;
  799. if not(readobjecttype) then
  800. exit;
  801. { also anonym objects aren't allow (o : object a : longint; end;) }
  802. if n='' then
  803. Message(parser_f_no_anonym_objects);
  804. readparentclasses;
  805. { default access is public }
  806. actmembertype:=[sp_public];
  807. { set class flags and inherits published, if necessary? }
  808. setclassattributes;
  809. aktobjectdef:=aktclass;
  810. aktclass.symtable.next:=symtablestack;
  811. symtablestack:=aktclass.symtable;
  812. testcurobject:=1;
  813. curobjectname:=Upper(n);
  814. { new procinfo }
  815. oldprocinfo:=procinfo;
  816. new(procinfo,init);
  817. procinfo^._class:=aktclass;
  818. { short class declaration ? }
  819. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  820. begin
  821. { Parse componenten }
  822. repeat
  823. if (sp_private in actmembertype) then
  824. include(aktclass.objectoptions,oo_has_private);
  825. if (sp_protected in actmembertype) then
  826. include(aktclass.objectoptions,oo_has_protected);
  827. case token of
  828. _ID : begin
  829. case idtoken of
  830. _PRIVATE : begin
  831. if is_interface(aktclass) then
  832. Message(parser_e_no_access_specifier_in_interfaces);
  833. consume(_PRIVATE);
  834. actmembertype:=[sp_private];
  835. current_object_option:=[sp_private];
  836. end;
  837. _PROTECTED : begin
  838. if is_interface(aktclass) then
  839. Message(parser_e_no_access_specifier_in_interfaces);
  840. consume(_PROTECTED);
  841. current_object_option:=[sp_protected];
  842. actmembertype:=[sp_protected];
  843. end;
  844. _PUBLIC : begin
  845. if is_interface(aktclass) then
  846. Message(parser_e_no_access_specifier_in_interfaces);
  847. consume(_PUBLIC);
  848. current_object_option:=[sp_public];
  849. actmembertype:=[sp_public];
  850. end;
  851. _PUBLISHED : begin
  852. if is_interface(aktclass) then
  853. Message(parser_e_no_access_specifier_in_interfaces)
  854. else
  855. if not(oo_can_have_published in aktclass.objectoptions) then
  856. Message(parser_e_cant_have_published);
  857. consume(_PUBLISHED);
  858. current_object_option:=[sp_published];
  859. actmembertype:=[sp_published];
  860. end;
  861. else
  862. if is_interface(aktclass) then
  863. Message(parser_e_no_vars_in_interfaces);
  864. read_var_decs(false,true,false);
  865. end;
  866. end;
  867. _PROPERTY : property_dec;
  868. _PROCEDURE,
  869. _FUNCTION,
  870. _CLASS : begin
  871. oldparse_only:=parse_only;
  872. parse_only:=true;
  873. parse_proc_dec;
  874. { this is for error recovery as well as forward }
  875. { interface mappings, i.e. mapping to a method }
  876. { which isn't declared yet }
  877. if assigned(aktprocsym) then
  878. begin
  879. {$ifndef newcg}
  880. parse_object_proc_directives(aktprocsym);
  881. {$endif newcg}
  882. { check if there are duplicates }
  883. check_identical_proc(temppd);
  884. if (po_msgint in aktprocsym.definition.procoptions) then
  885. include(aktclass.objectoptions,oo_has_msgint);
  886. if (po_msgstr in aktprocsym.definition.procoptions) then
  887. include(aktclass.objectoptions,oo_has_msgstr);
  888. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  889. include(aktclass.objectoptions,oo_has_virtual);
  890. chkcpp;
  891. end;
  892. parse_only:=oldparse_only;
  893. end;
  894. _CONSTRUCTOR : begin
  895. if not(sp_public in actmembertype) then
  896. Message(parser_w_constructor_should_be_public);
  897. if is_interface(aktclass) then
  898. Message(parser_e_no_con_des_in_interfaces);
  899. oldparse_only:=parse_only;
  900. parse_only:=true;
  901. constructor_head;
  902. {$ifndef newcg}
  903. parse_object_proc_directives(aktprocsym);
  904. {$endif newcg}
  905. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  906. include(aktclass.objectoptions,oo_has_virtual);
  907. chkcpp;
  908. parse_only:=oldparse_only;
  909. end;
  910. _DESTRUCTOR : begin
  911. if there_is_a_destructor then
  912. Message(parser_n_only_one_destructor);
  913. if is_interface(aktclass) then
  914. Message(parser_e_no_con_des_in_interfaces);
  915. there_is_a_destructor:=true;
  916. if not(sp_public in actmembertype) then
  917. Message(parser_w_destructor_should_be_public);
  918. oldparse_only:=parse_only;
  919. parse_only:=true;
  920. destructor_head;
  921. {$ifndef newcg}
  922. parse_object_proc_directives(aktprocsym);
  923. {$endif newcg}
  924. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  925. include(aktclass.objectoptions,oo_has_virtual);
  926. chkcpp;
  927. parse_only:=oldparse_only;
  928. end;
  929. _END : begin
  930. consume(_END);
  931. break;
  932. end;
  933. else
  934. consume(_ID); { Give a ident expected message, like tp7 }
  935. end;
  936. until false;
  937. current_object_option:=[sp_public];
  938. end;
  939. testcurobject:=0;
  940. curobjectname:='';
  941. typecanbeforward:=storetypecanbeforward;
  942. { generate vmt space if needed }
  943. if not(oo_has_vmt in aktclass.objectoptions) and
  944. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  945. (classtype in [odt_class])
  946. ) then
  947. aktclass.insertvmt;
  948. if (cs_create_smart in aktmoduleswitches) then
  949. dataSegment.concat(Tai_cut.Create);
  950. ch:=cclassheader.create(aktclass);
  951. if is_interface(aktclass) then
  952. ch.writeinterfaceids;
  953. if (oo_has_vmt in aktclass.objectoptions) then
  954. ch.writevmt;
  955. ch.free;
  956. if is_interface(aktclass) then
  957. setinterfacemethodoptions;
  958. { restore old state }
  959. symtablestack:=symtablestack.next;
  960. aktobjectdef:=nil;
  961. {Restore procinfo}
  962. dispose(procinfo,done);
  963. procinfo:=oldprocinfo;
  964. {Restore the aktprocsym.}
  965. aktprocsym:=oldprocsym;
  966. object_dec:=aktclass;
  967. end;
  968. end.
  969. {
  970. $Log$
  971. Revision 1.27 2001-08-22 21:16:20 florian
  972. * some interfaces related problems regarding
  973. mapping of interface implementions fixed
  974. Revision 1.26 2001/06/03 21:57:36 peter
  975. + hint directive parsing support
  976. Revision 1.25 2001/05/04 15:52:03 florian
  977. * some Delphi incompatibilities fixed:
  978. - out, dispose and new can be used as idenfiers now
  979. - const p = apointerype(nil); is supported now
  980. + support for const p = apointertype(pointer(1234)); added
  981. Revision 1.24 2001/04/21 15:36:00 peter
  982. * check for type block when parsing class of
  983. Revision 1.23 2001/04/21 13:37:16 peter
  984. * made tclassheader using class of to implement cpu dependent code
  985. Revision 1.22 2001/04/18 22:01:54 peter
  986. * registration of targets and assemblers
  987. Revision 1.21 2001/04/13 01:22:11 peter
  988. * symtable change to classes
  989. * range check generation and errors fixed, make cycle DEBUG=1 works
  990. * memory leaks fixed
  991. Revision 1.20 2001/04/04 22:43:51 peter
  992. * remove unnecessary calls to firstpass
  993. Revision 1.19 2001/04/04 21:30:43 florian
  994. * applied several fixes to get the DD8 Delphi Unit compiled
  995. e.g. "forward"-interfaces are working now
  996. Revision 1.18 2001/04/02 21:20:31 peter
  997. * resulttype rewrite
  998. Revision 1.17 2001/03/16 14:56:38 marco
  999. * Pavel's fixes commited (Peter asked). Cycled to test
  1000. Revision 1.16 2001/03/11 22:58:49 peter
  1001. * getsym redesign, removed the globals srsym,srsymtable
  1002. Revision 1.15 2000/12/25 00:07:27 peter
  1003. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1004. tlinkedlist objects)
  1005. Revision 1.14 2000/11/29 00:30:35 florian
  1006. * unused units removed from uses clause
  1007. * some changes for widestrings
  1008. Revision 1.13 2000/11/17 17:32:58 florian
  1009. * properties can now be used in interfaces
  1010. Revision 1.12 2000/11/17 08:21:07 florian
  1011. *** empty log message ***
  1012. Revision 1.11 2000/11/12 23:24:11 florian
  1013. * interfaces are basically running
  1014. Revision 1.10 2000/11/12 22:17:47 peter
  1015. * some realname updates for messages
  1016. Revision 1.9 2000/11/06 23:05:52 florian
  1017. * more fixes
  1018. Revision 1.8 2000/11/06 20:30:55 peter
  1019. * more fixes to get make cycle working
  1020. Revision 1.7 2000/11/04 18:03:57 florian
  1021. * fixed upper/lower case problem
  1022. Revision 1.6 2000/11/04 17:31:00 florian
  1023. * fixed some problems of previous commit
  1024. Revision 1.5 2000/11/04 14:25:20 florian
  1025. + merged Attila's changes for interfaces, not tested yet
  1026. Revision 1.4 2000/10/31 22:02:49 peter
  1027. * symtable splitted, no real code changes
  1028. Revision 1.3 2000/10/26 21:54:03 peter
  1029. * fixed crash with error in child definition (merged)
  1030. Revision 1.2 2000/10/21 18:16:11 florian
  1031. * a lot of changes:
  1032. - basic dyn. array support
  1033. - basic C++ support
  1034. - some work for interfaces done
  1035. ....
  1036. Revision 1.1 2000/10/14 10:14:51 peter
  1037. * moehrendorf oct 2000 rewrite
  1038. }