pdecobj.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. cgbase,
  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 (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. { publishable }
  502. if classtype in [odt_interfacecom,odt_class] then
  503. begin
  504. aktclass.objecttype:=classtype;
  505. if (cs_generate_rtti in aktlocalswitches) or
  506. (assigned(aktclass.childof) and
  507. (oo_can_have_published in aktclass.childof.objectoptions)) then
  508. begin
  509. include(aktclass.objectoptions,oo_can_have_published);
  510. { in "publishable" classes the default access type is published }
  511. actmembertype:=[sp_published];
  512. { don't know if this is necessary (FK) }
  513. current_object_option:=[sp_published];
  514. end;
  515. end;
  516. end;
  517. procedure setclassparent;
  518. begin
  519. if assigned(fd) then
  520. aktclass:=fd
  521. else
  522. aktclass:=tobjectdef.create(classtype,n,nil);
  523. { is the current class tobject? }
  524. { so you could define your own tobject }
  525. if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_class) and (upper(n)='TOBJECT') then
  526. class_tobject:=aktclass
  527. else if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  528. interface_iunknown:=aktclass
  529. else
  530. begin
  531. case classtype of
  532. odt_class:
  533. childof:=class_tobject;
  534. odt_interfacecom:
  535. childof:=interface_iunknown;
  536. end;
  537. if (oo_is_forward in childof.objectoptions) then
  538. Message1(parser_e_forward_declaration_must_be_resolved,childof.objname^);
  539. aktclass.set_parent(childof);
  540. end;
  541. end;
  542. procedure setinterfacemethodoptions;
  543. var
  544. i: longint;
  545. defs: TIndexArray;
  546. pd: tprocdef;
  547. begin
  548. include(aktclass.objectoptions,oo_has_virtual);
  549. defs:=aktclass.symtable.defindex;
  550. for i:=1 to defs.count do
  551. begin
  552. pd:=tprocdef(defs.search(i));
  553. if pd.deftype=procdef then
  554. begin
  555. pd.extnumber:=aktclass.lastvtableindex;
  556. inc(aktclass.lastvtableindex);
  557. include(pd.procoptions,po_virtualmethod);
  558. pd.forwarddef:=false;
  559. end;
  560. end;
  561. end;
  562. function readobjecttype : boolean;
  563. begin
  564. readobjecttype:=true;
  565. { distinguish classes and objects }
  566. case token of
  567. _OBJECT:
  568. begin
  569. classtype:=odt_object;
  570. consume(_OBJECT)
  571. end;
  572. _CPPCLASS:
  573. begin
  574. classtype:=odt_cppclass;
  575. consume(_CPPCLASS);
  576. end;
  577. _INTERFACE:
  578. begin
  579. if aktinterfacetype=it_interfacecom then
  580. classtype:=odt_interfacecom
  581. else {it_interfacecorba}
  582. classtype:=odt_interfacecorba;
  583. consume(_INTERFACE);
  584. { forward declaration }
  585. if not(assigned(fd)) and (token=_SEMICOLON) then
  586. begin
  587. { also anonym objects aren't allow (o : object a : longint; end;) }
  588. if n='' then
  589. Message(parser_f_no_anonym_objects);
  590. aktclass:=tobjectdef.create(classtype,n,nil);
  591. if (cs_compilesystem in aktmoduleswitches) and
  592. (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  593. interface_iunknown:=aktclass;
  594. include(aktclass.objectoptions,oo_is_forward);
  595. object_dec:=aktclass;
  596. typecanbeforward:=storetypecanbeforward;
  597. readobjecttype:=false;
  598. exit;
  599. end;
  600. end;
  601. _CLASS:
  602. begin
  603. classtype:=odt_class;
  604. consume(_CLASS);
  605. if not(assigned(fd)) and
  606. (token=_OF) and
  607. { Delphi only allows class of in type blocks.
  608. Note that when parsing the type of a variable declaration
  609. the blocktype is bt_type so the check for typecanbeforward
  610. is also necessary (PFV) }
  611. (((block_type=bt_type) and typecanbeforward) or
  612. not(m_delphi in aktmodeswitches)) then
  613. begin
  614. { a hack, but it's easy to handle }
  615. { class reference type }
  616. consume(_OF);
  617. single_type(tt,hs,typecanbeforward);
  618. { accept hp1, if is a forward def or a class }
  619. if (tt.def.deftype=forwarddef) or
  620. is_class(tt.def) then
  621. begin
  622. pcrd:=tclassrefdef.create(tt);
  623. object_dec:=pcrd;
  624. end
  625. else
  626. begin
  627. object_dec:=generrortype.def;
  628. Message1(type_e_class_type_expected,generrortype.def.typename);
  629. end;
  630. typecanbeforward:=storetypecanbeforward;
  631. readobjecttype:=false;
  632. exit;
  633. end
  634. { forward class }
  635. else if not(assigned(fd)) and (token=_SEMICOLON) then
  636. begin
  637. { also anonym objects aren't allow (o : object a : longint; end;) }
  638. if n='' then
  639. Message(parser_f_no_anonym_objects);
  640. aktclass:=tobjectdef.create(odt_class,n,nil);
  641. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  642. class_tobject:=aktclass;
  643. aktclass.objecttype:=odt_class;
  644. include(aktclass.objectoptions,oo_is_forward);
  645. { all classes must have a vmt !! at offset zero }
  646. if not(oo_has_vmt in aktclass.objectoptions) then
  647. aktclass.insertvmt;
  648. object_dec:=aktclass;
  649. typecanbeforward:=storetypecanbeforward;
  650. readobjecttype:=false;
  651. exit;
  652. end;
  653. end;
  654. else
  655. begin
  656. classtype:=odt_class; { this is error but try to recover }
  657. consume(_OBJECT);
  658. end;
  659. end;
  660. end;
  661. procedure readimplementedinterfaces;
  662. var
  663. implintf: tobjectdef;
  664. tt : ttype;
  665. begin
  666. while try_to_consume(_COMMA) do begin
  667. id_type(tt,pattern,false);
  668. implintf:=tobjectdef(tt.def);
  669. if (tt.def.deftype<>objectdef) then begin
  670. Message1(type_e_interface_type_expected,tt.def.typename);
  671. Continue; { omit }
  672. end;
  673. if not is_interface(implintf) then begin
  674. Message1(type_e_interface_type_expected,implintf.typename);
  675. Continue; { omit }
  676. end;
  677. if aktclass.implementedinterfaces.searchintf(tt.def)<>-1 then
  678. Message1(sym_e_duplicate_id,tt.def.name)
  679. else
  680. aktclass.implementedinterfaces.addintf(tt.def);
  681. end;
  682. end;
  683. procedure readinterfaceiid;
  684. var
  685. p : tnode;
  686. begin
  687. p:=comp_expr(true);
  688. if p.nodetype=stringconstn then
  689. begin
  690. aktclass.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  691. p.free;
  692. aktclass.isiidguidvalid:=string2guid(aktclass.iidstr^,aktclass.iidguid);
  693. if (classtype=odt_interfacecom) and not aktclass.isiidguidvalid then
  694. Message(parser_e_improper_guid_syntax);
  695. end
  696. else
  697. begin
  698. p.free;
  699. Message(cg_e_illegal_expression);
  700. end;
  701. end;
  702. procedure readparentclasses;
  703. begin
  704. { reads the parent class }
  705. if token=_LKLAMMER then
  706. begin
  707. consume(_LKLAMMER);
  708. id_type(tt,pattern,false);
  709. childof:=tobjectdef(tt.def);
  710. if (not assigned(childof)) or
  711. (childof.deftype<>objectdef) then
  712. begin
  713. if assigned(childof) then
  714. Message1(type_e_class_type_expected,childof.typename);
  715. childof:=nil;
  716. aktclass:=tobjectdef.create(classtype,n,nil);
  717. end
  718. else
  719. begin
  720. { a mix of class, interfaces, objects and cppclasses
  721. isn't allowed }
  722. case classtype of
  723. odt_class:
  724. if not(is_class(childof)) and
  725. not(is_interface(childof)) then
  726. Message(parser_e_mix_of_classes_and_objects);
  727. odt_interfacecorba,
  728. odt_interfacecom:
  729. if not(is_interface(childof)) then
  730. Message(parser_e_mix_of_classes_and_objects);
  731. odt_cppclass:
  732. if not(is_cppclass(childof)) then
  733. Message(parser_e_mix_of_classes_and_objects);
  734. odt_object:
  735. if not(is_object(childof)) then
  736. Message(parser_e_mix_of_classes_and_objects);
  737. end;
  738. { the forward of the child must be resolved to get
  739. correct field addresses }
  740. if assigned(fd) then
  741. begin
  742. if (oo_is_forward in childof.objectoptions) then
  743. Message1(parser_e_forward_declaration_must_be_resolved,childof.objname^);
  744. aktclass:=fd;
  745. { we must inherit several options !!
  746. this was missing !!
  747. all is now done in set_parent
  748. including symtable datasize setting PM }
  749. fd.set_parent(childof);
  750. end
  751. else
  752. aktclass:=tobjectdef.create(classtype,n,childof);
  753. if aktclass.objecttype=odt_class then
  754. readimplementedinterfaces;
  755. end;
  756. consume(_RKLAMMER);
  757. end
  758. { if no parent class, then a class get tobject as parent }
  759. else if classtype in [odt_class,odt_interfacecom] then
  760. setclassparent
  761. else
  762. aktclass:=tobjectdef.create(classtype,n,nil);
  763. { read GUID }
  764. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  765. try_to_consume(_LECKKLAMMER) then
  766. begin
  767. readinterfaceiid;
  768. consume(_RECKKLAMMER);
  769. end;
  770. end;
  771. procedure chkcpp;
  772. begin
  773. if is_cppclass(aktclass) then
  774. begin
  775. include(aktprocsym.definition.proccalloptions,pocall_cppdecl);
  776. aktprocsym.definition.setmangledname(
  777. target_info.Cprefix+aktprocsym.definition.cplusplusmangledname);
  778. end;
  779. end;
  780. var
  781. temppd : tprocdef;
  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. { generate vmt space if needed }
  940. if not(oo_has_vmt in aktclass.objectoptions) and
  941. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  942. (classtype in [odt_class])
  943. ) then
  944. aktclass.insertvmt;
  945. if is_interface(aktclass) then
  946. setinterfacemethodoptions;
  947. { reset }
  948. testcurobject:=0;
  949. curobjectname:='';
  950. typecanbeforward:=storetypecanbeforward;
  951. { restore old state }
  952. symtablestack:=symtablestack.next;
  953. aktobjectdef:=nil;
  954. {Restore procinfo}
  955. dispose(procinfo,done);
  956. procinfo:=oldprocinfo;
  957. {Restore the aktprocsym.}
  958. aktprocsym:=oldprocsym;
  959. object_dec:=aktclass;
  960. end;
  961. end.
  962. {
  963. $Log$
  964. Revision 1.29 2001-08-30 20:13:53 peter
  965. * rtti/init table updates
  966. * rttisym for reusable global rtti/init info
  967. * support published for interfaces
  968. Revision 1.28 2001/08/26 13:36:44 florian
  969. * some cg reorganisation
  970. * some PPC updates
  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. }