pdecobj.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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,hcgdata,
  31. node,nld,ncon,ncnv,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 token=_OUT then
  131. begin
  132. consume(_OUT);
  133. varspez:=vs_out;
  134. end
  135. else varspez:=vs_value;
  136. sc:=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 (token=_OF) then
  605. begin
  606. { a hack, but it's easy to handle }
  607. { class reference type }
  608. consume(_OF);
  609. single_type(tt,hs,typecanbeforward);
  610. { accept hp1, if is a forward def or a class }
  611. if (tt.def.deftype=forwarddef) or
  612. is_class(tt.def) then
  613. begin
  614. pcrd:=tclassrefdef.create(tt);
  615. object_dec:=pcrd;
  616. end
  617. else
  618. begin
  619. object_dec:=generrortype.def;
  620. Message1(type_e_class_type_expected,generrortype.def.typename);
  621. end;
  622. typecanbeforward:=storetypecanbeforward;
  623. readobjecttype:=false;
  624. exit;
  625. end
  626. { forward class }
  627. else if not(assigned(fd)) and (token=_SEMICOLON) then
  628. begin
  629. { also anonym objects aren't allow (o : object a : longint; end;) }
  630. if n='' then
  631. Message(parser_f_no_anonym_objects);
  632. aktclass:=tobjectdef.create(odt_class,n,nil);
  633. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  634. class_tobject:=aktclass;
  635. aktclass.objecttype:=odt_class;
  636. include(aktclass.objectoptions,oo_is_forward);
  637. { all classes must have a vmt !! at offset zero }
  638. if not(oo_has_vmt in aktclass.objectoptions) then
  639. aktclass.insertvmt;
  640. object_dec:=aktclass;
  641. typecanbeforward:=storetypecanbeforward;
  642. readobjecttype:=false;
  643. exit;
  644. end;
  645. end;
  646. else
  647. begin
  648. classtype:=odt_class; { this is error but try to recover }
  649. consume(_OBJECT);
  650. end;
  651. end;
  652. end;
  653. procedure readimplementedinterfaces;
  654. var
  655. implintf: tobjectdef;
  656. tt : ttype;
  657. begin
  658. while try_to_consume(_COMMA) do begin
  659. id_type(tt,pattern,false);
  660. implintf:=tobjectdef(tt.def);
  661. if (tt.def.deftype<>objectdef) then begin
  662. Message1(type_e_interface_type_expected,tt.def.typename);
  663. Continue; { omit }
  664. end;
  665. if not is_interface(implintf) then begin
  666. Message1(type_e_interface_type_expected,implintf.typename);
  667. Continue; { omit }
  668. end;
  669. if aktclass.implementedinterfaces.searchintf(tt.def)<>-1 then
  670. Message1(sym_e_duplicate_id,tt.def.name)
  671. else
  672. aktclass.implementedinterfaces.addintf(tt.def);
  673. end;
  674. end;
  675. procedure readinterfaceiid;
  676. var
  677. p : tnode;
  678. begin
  679. p:=comp_expr(true);
  680. if p.nodetype=stringconstn then
  681. begin
  682. aktclass.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  683. p.free;
  684. aktclass.isiidguidvalid:=string2guid(aktclass.iidstr^,aktclass.iidguid);
  685. if (classtype=odt_interfacecom) and not aktclass.isiidguidvalid then
  686. Message(parser_e_improper_guid_syntax);
  687. end
  688. else
  689. begin
  690. p.free;
  691. Message(cg_e_illegal_expression);
  692. end;
  693. end;
  694. procedure readparentclasses;
  695. begin
  696. { reads the parent class }
  697. if token=_LKLAMMER then
  698. begin
  699. consume(_LKLAMMER);
  700. id_type(tt,pattern,false);
  701. childof:=tobjectdef(tt.def);
  702. if (not assigned(childof)) or
  703. (childof.deftype<>objectdef) then
  704. begin
  705. if assigned(childof) then
  706. Message1(type_e_class_type_expected,childof.typename);
  707. childof:=nil;
  708. aktclass:=tobjectdef.create(classtype,n,nil);
  709. end
  710. else
  711. begin
  712. { a mix of class, interfaces, objects and cppclasses
  713. isn't allowed }
  714. case classtype of
  715. odt_class:
  716. if not(is_class(childof)) and
  717. not(is_interface(childof)) then
  718. Message(parser_e_mix_of_classes_and_objects);
  719. odt_interfacecorba,
  720. odt_interfacecom:
  721. if not(is_interface(childof)) then
  722. Message(parser_e_mix_of_classes_and_objects);
  723. odt_cppclass:
  724. if not(is_cppclass(childof)) then
  725. Message(parser_e_mix_of_classes_and_objects);
  726. odt_object:
  727. if not(is_object(childof)) then
  728. Message(parser_e_mix_of_classes_and_objects);
  729. end;
  730. { the forward of the child must be resolved to get
  731. correct field addresses }
  732. if assigned(fd) then
  733. begin
  734. if (oo_is_forward in childof.objectoptions) then
  735. Message1(parser_e_forward_declaration_must_be_resolved,childof.objname^);
  736. aktclass:=fd;
  737. { we must inherit several options !!
  738. this was missing !!
  739. all is now done in set_parent
  740. including symtable datasize setting PM }
  741. fd.set_parent(childof);
  742. end
  743. else
  744. aktclass:=tobjectdef.create(classtype,n,childof);
  745. if aktclass.objecttype=odt_class then
  746. readimplementedinterfaces;
  747. end;
  748. consume(_RKLAMMER);
  749. end
  750. { if no parent class, then a class get tobject as parent }
  751. else if classtype in [odt_class,odt_interfacecom] then
  752. setclassparent
  753. else
  754. aktclass:=tobjectdef.create(classtype,n,nil);
  755. { read GUID }
  756. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  757. try_to_consume(_LECKKLAMMER) then
  758. begin
  759. readinterfaceiid;
  760. consume(_RECKKLAMMER);
  761. end;
  762. end;
  763. procedure chkcpp;
  764. begin
  765. if is_cppclass(aktclass) then
  766. begin
  767. include(aktprocsym.definition.proccalloptions,pocall_cppdecl);
  768. aktprocsym.definition.setmangledname(
  769. target_os.Cprefix+aktprocsym.definition.cplusplusmangledname);
  770. end;
  771. end;
  772. var
  773. temppd : tprocdef;
  774. ch : tclassheader;
  775. begin
  776. {Nowadays aktprocsym may already have a value, so we need to save
  777. it.}
  778. oldprocsym:=aktprocsym;
  779. { forward is resolved }
  780. if assigned(fd) then
  781. exclude(fd.objectoptions,oo_is_forward);
  782. there_is_a_destructor:=false;
  783. actmembertype:=[sp_public];
  784. { objects and class types can't be declared local }
  785. if not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) then
  786. Message(parser_e_no_local_objects);
  787. storetypecanbeforward:=typecanbeforward;
  788. { for tp mode don't allow forward types }
  789. if (m_tp in aktmodeswitches) and
  790. not (m_delphi in aktmodeswitches) then
  791. typecanbeforward:=false;
  792. if not(readobjecttype) then
  793. exit;
  794. { also anonym objects aren't allow (o : object a : longint; end;) }
  795. if n='' then
  796. Message(parser_f_no_anonym_objects);
  797. readparentclasses;
  798. { default access is public }
  799. actmembertype:=[sp_public];
  800. { set class flags and inherits published, if necessary? }
  801. setclassattributes;
  802. aktobjectdef:=aktclass;
  803. aktclass.symtable.next:=symtablestack;
  804. symtablestack:=aktclass.symtable;
  805. testcurobject:=1;
  806. curobjectname:=Upper(n);
  807. { new procinfo }
  808. oldprocinfo:=procinfo;
  809. new(procinfo,init);
  810. procinfo^._class:=aktclass;
  811. { short class declaration ? }
  812. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  813. begin
  814. { Parse componenten }
  815. repeat
  816. if (sp_private in actmembertype) then
  817. include(aktclass.objectoptions,oo_has_private);
  818. if (sp_protected in actmembertype) then
  819. include(aktclass.objectoptions,oo_has_protected);
  820. case token of
  821. _ID : begin
  822. case idtoken of
  823. _PRIVATE : begin
  824. if is_interface(aktclass) then
  825. Message(parser_e_no_access_specifier_in_interfaces);
  826. consume(_PRIVATE);
  827. actmembertype:=[sp_private];
  828. current_object_option:=[sp_private];
  829. end;
  830. _PROTECTED : begin
  831. if is_interface(aktclass) then
  832. Message(parser_e_no_access_specifier_in_interfaces);
  833. consume(_PROTECTED);
  834. current_object_option:=[sp_protected];
  835. actmembertype:=[sp_protected];
  836. end;
  837. _PUBLIC : begin
  838. if is_interface(aktclass) then
  839. Message(parser_e_no_access_specifier_in_interfaces);
  840. consume(_PUBLIC);
  841. current_object_option:=[sp_public];
  842. actmembertype:=[sp_public];
  843. end;
  844. _PUBLISHED : begin
  845. if is_interface(aktclass) then
  846. Message(parser_e_no_access_specifier_in_interfaces)
  847. else
  848. if not(oo_can_have_published in aktclass.objectoptions) then
  849. Message(parser_e_cant_have_published);
  850. consume(_PUBLISHED);
  851. current_object_option:=[sp_published];
  852. actmembertype:=[sp_published];
  853. end;
  854. else
  855. if is_interface(aktclass) then
  856. Message(parser_e_no_vars_in_interfaces);
  857. read_var_decs(false,true,false);
  858. end;
  859. end;
  860. _PROPERTY : property_dec;
  861. _PROCEDURE,
  862. _FUNCTION,
  863. _CLASS : begin
  864. oldparse_only:=parse_only;
  865. parse_only:=true;
  866. parse_proc_dec;
  867. {$ifndef newcg}
  868. parse_object_proc_directives(aktprocsym);
  869. {$endif newcg}
  870. { check if there are duplicates }
  871. check_identical_proc(temppd);
  872. if (po_msgint in aktprocsym.definition.procoptions) then
  873. include(aktclass.objectoptions,oo_has_msgint);
  874. if (po_msgstr in aktprocsym.definition.procoptions) then
  875. include(aktclass.objectoptions,oo_has_msgstr);
  876. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  877. include(aktclass.objectoptions,oo_has_virtual);
  878. chkcpp;
  879. parse_only:=oldparse_only;
  880. end;
  881. _CONSTRUCTOR : begin
  882. if not(sp_public in actmembertype) then
  883. Message(parser_w_constructor_should_be_public);
  884. if is_interface(aktclass) then
  885. Message(parser_e_no_con_des_in_interfaces);
  886. oldparse_only:=parse_only;
  887. parse_only:=true;
  888. constructor_head;
  889. {$ifndef newcg}
  890. parse_object_proc_directives(aktprocsym);
  891. {$endif newcg}
  892. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  893. include(aktclass.objectoptions,oo_has_virtual);
  894. chkcpp;
  895. parse_only:=oldparse_only;
  896. end;
  897. _DESTRUCTOR : begin
  898. if there_is_a_destructor then
  899. Message(parser_n_only_one_destructor);
  900. if is_interface(aktclass) then
  901. Message(parser_e_no_con_des_in_interfaces);
  902. there_is_a_destructor:=true;
  903. if not(sp_public in actmembertype) then
  904. Message(parser_w_destructor_should_be_public);
  905. oldparse_only:=parse_only;
  906. parse_only:=true;
  907. destructor_head;
  908. {$ifndef newcg}
  909. parse_object_proc_directives(aktprocsym);
  910. {$endif newcg}
  911. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  912. include(aktclass.objectoptions,oo_has_virtual);
  913. chkcpp;
  914. parse_only:=oldparse_only;
  915. end;
  916. _END : begin
  917. consume(_END);
  918. break;
  919. end;
  920. else
  921. consume(_ID); { Give a ident expected message, like tp7 }
  922. end;
  923. until false;
  924. current_object_option:=[sp_public];
  925. end;
  926. testcurobject:=0;
  927. curobjectname:='';
  928. typecanbeforward:=storetypecanbeforward;
  929. { generate vmt space if needed }
  930. if not(oo_has_vmt in aktclass.objectoptions) and
  931. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  932. (classtype in [odt_class])
  933. ) then
  934. aktclass.insertvmt;
  935. if (cs_create_smart in aktmoduleswitches) then
  936. dataSegment.concat(Tai_cut.Create);
  937. ch:=tclassheader.create(aktclass);
  938. if is_interface(aktclass) then
  939. ch.writeinterfaceids;
  940. if (oo_has_vmt in aktclass.objectoptions) then
  941. ch.writevmt;
  942. ch.free;
  943. if is_interface(aktclass) then
  944. setinterfacemethodoptions;
  945. { restore old state }
  946. symtablestack:=symtablestack.next;
  947. aktobjectdef:=nil;
  948. {Restore procinfo}
  949. dispose(procinfo,done);
  950. procinfo:=oldprocinfo;
  951. {Restore the aktprocsym.}
  952. aktprocsym:=oldprocsym;
  953. object_dec:=aktclass;
  954. end;
  955. end.
  956. {
  957. $Log$
  958. Revision 1.21 2001-04-13 01:22:11 peter
  959. * symtable change to classes
  960. * range check generation and errors fixed, make cycle DEBUG=1 works
  961. * memory leaks fixed
  962. Revision 1.20 2001/04/04 22:43:51 peter
  963. * remove unnecessary calls to firstpass
  964. Revision 1.19 2001/04/04 21:30:43 florian
  965. * applied several fixes to get the DD8 Delphi Unit compiled
  966. e.g. "forward"-interfaces are working now
  967. Revision 1.18 2001/04/02 21:20:31 peter
  968. * resulttype rewrite
  969. Revision 1.17 2001/03/16 14:56:38 marco
  970. * Pavel's fixes commited (Peter asked). Cycled to test
  971. Revision 1.16 2001/03/11 22:58:49 peter
  972. * getsym redesign, removed the globals srsym,srsymtable
  973. Revision 1.15 2000/12/25 00:07:27 peter
  974. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  975. tlinkedlist objects)
  976. Revision 1.14 2000/11/29 00:30:35 florian
  977. * unused units removed from uses clause
  978. * some changes for widestrings
  979. Revision 1.13 2000/11/17 17:32:58 florian
  980. * properties can now be used in interfaces
  981. Revision 1.12 2000/11/17 08:21:07 florian
  982. *** empty log message ***
  983. Revision 1.11 2000/11/12 23:24:11 florian
  984. * interfaces are basically running
  985. Revision 1.10 2000/11/12 22:17:47 peter
  986. * some realname updates for messages
  987. Revision 1.9 2000/11/06 23:05:52 florian
  988. * more fixes
  989. Revision 1.8 2000/11/06 20:30:55 peter
  990. * more fixes to get make cycle working
  991. Revision 1.7 2000/11/04 18:03:57 florian
  992. * fixed upper/lower case problem
  993. Revision 1.6 2000/11/04 17:31:00 florian
  994. * fixed some problems of previous commit
  995. Revision 1.5 2000/11/04 14:25:20 florian
  996. + merged Attila's changes for interfaces, not tested yet
  997. Revision 1.4 2000/10/31 22:02:49 peter
  998. * symtable splitted, no real code changes
  999. Revision 1.3 2000/10/26 21:54:03 peter
  1000. * fixed crash with error in child definition (merged)
  1001. Revision 1.2 2000/10/21 18:16:11 florian
  1002. * a lot of changes:
  1003. - basic dyn. array support
  1004. - basic C++ support
  1005. - some work for interfaces done
  1006. ....
  1007. Revision 1.1 2000/10/14 10:14:51 peter
  1008. * moehrendorf oct 2000 rewrite
  1009. }