pdecobj.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. {$ifndef newcg}
  875. parse_object_proc_directives(aktprocsym);
  876. {$endif newcg}
  877. { check if there are duplicates }
  878. check_identical_proc(temppd);
  879. if (po_msgint in aktprocsym.definition.procoptions) then
  880. include(aktclass.objectoptions,oo_has_msgint);
  881. if (po_msgstr in aktprocsym.definition.procoptions) then
  882. include(aktclass.objectoptions,oo_has_msgstr);
  883. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  884. include(aktclass.objectoptions,oo_has_virtual);
  885. chkcpp;
  886. parse_only:=oldparse_only;
  887. end;
  888. _CONSTRUCTOR : begin
  889. if not(sp_public in actmembertype) then
  890. Message(parser_w_constructor_should_be_public);
  891. if is_interface(aktclass) then
  892. Message(parser_e_no_con_des_in_interfaces);
  893. oldparse_only:=parse_only;
  894. parse_only:=true;
  895. constructor_head;
  896. {$ifndef newcg}
  897. parse_object_proc_directives(aktprocsym);
  898. {$endif newcg}
  899. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  900. include(aktclass.objectoptions,oo_has_virtual);
  901. chkcpp;
  902. parse_only:=oldparse_only;
  903. end;
  904. _DESTRUCTOR : begin
  905. if there_is_a_destructor then
  906. Message(parser_n_only_one_destructor);
  907. if is_interface(aktclass) then
  908. Message(parser_e_no_con_des_in_interfaces);
  909. there_is_a_destructor:=true;
  910. if not(sp_public in actmembertype) then
  911. Message(parser_w_destructor_should_be_public);
  912. oldparse_only:=parse_only;
  913. parse_only:=true;
  914. destructor_head;
  915. {$ifndef newcg}
  916. parse_object_proc_directives(aktprocsym);
  917. {$endif newcg}
  918. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  919. include(aktclass.objectoptions,oo_has_virtual);
  920. chkcpp;
  921. parse_only:=oldparse_only;
  922. end;
  923. _END : begin
  924. consume(_END);
  925. break;
  926. end;
  927. else
  928. consume(_ID); { Give a ident expected message, like tp7 }
  929. end;
  930. until false;
  931. current_object_option:=[sp_public];
  932. end;
  933. testcurobject:=0;
  934. curobjectname:='';
  935. typecanbeforward:=storetypecanbeforward;
  936. { generate vmt space if needed }
  937. if not(oo_has_vmt in aktclass.objectoptions) and
  938. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  939. (classtype in [odt_class])
  940. ) then
  941. aktclass.insertvmt;
  942. if (cs_create_smart in aktmoduleswitches) then
  943. dataSegment.concat(Tai_cut.Create);
  944. ch:=cclassheader.create(aktclass);
  945. if is_interface(aktclass) then
  946. ch.writeinterfaceids;
  947. if (oo_has_vmt in aktclass.objectoptions) then
  948. ch.writevmt;
  949. ch.free;
  950. if is_interface(aktclass) then
  951. setinterfacemethodoptions;
  952. { restore old state }
  953. symtablestack:=symtablestack.next;
  954. aktobjectdef:=nil;
  955. {Restore procinfo}
  956. dispose(procinfo,done);
  957. procinfo:=oldprocinfo;
  958. {Restore the aktprocsym.}
  959. aktprocsym:=oldprocsym;
  960. object_dec:=aktclass;
  961. end;
  962. end.
  963. {
  964. $Log$
  965. Revision 1.26 2001-06-03 21:57:36 peter
  966. + hint directive parsing support
  967. Revision 1.25 2001/05/04 15:52:03 florian
  968. * some Delphi incompatibilities fixed:
  969. - out, dispose and new can be used as idenfiers now
  970. - const p = apointerype(nil); is supported now
  971. + support for const p = apointertype(pointer(1234)); added
  972. Revision 1.24 2001/04/21 15:36:00 peter
  973. * check for type block when parsing class of
  974. Revision 1.23 2001/04/21 13:37:16 peter
  975. * made tclassheader using class of to implement cpu dependent code
  976. Revision 1.22 2001/04/18 22:01:54 peter
  977. * registration of targets and assemblers
  978. Revision 1.21 2001/04/13 01:22:11 peter
  979. * symtable change to classes
  980. * range check generation and errors fixed, make cycle DEBUG=1 works
  981. * memory leaks fixed
  982. Revision 1.20 2001/04/04 22:43:51 peter
  983. * remove unnecessary calls to firstpass
  984. Revision 1.19 2001/04/04 21:30:43 florian
  985. * applied several fixes to get the DD8 Delphi Unit compiled
  986. e.g. "forward"-interfaces are working now
  987. Revision 1.18 2001/04/02 21:20:31 peter
  988. * resulttype rewrite
  989. Revision 1.17 2001/03/16 14:56:38 marco
  990. * Pavel's fixes commited (Peter asked). Cycled to test
  991. Revision 1.16 2001/03/11 22:58:49 peter
  992. * getsym redesign, removed the globals srsym,srsymtable
  993. Revision 1.15 2000/12/25 00:07:27 peter
  994. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  995. tlinkedlist objects)
  996. Revision 1.14 2000/11/29 00:30:35 florian
  997. * unused units removed from uses clause
  998. * some changes for widestrings
  999. Revision 1.13 2000/11/17 17:32:58 florian
  1000. * properties can now be used in interfaces
  1001. Revision 1.12 2000/11/17 08:21:07 florian
  1002. *** empty log message ***
  1003. Revision 1.11 2000/11/12 23:24:11 florian
  1004. * interfaces are basically running
  1005. Revision 1.10 2000/11/12 22:17:47 peter
  1006. * some realname updates for messages
  1007. Revision 1.9 2000/11/06 23:05:52 florian
  1008. * more fixes
  1009. Revision 1.8 2000/11/06 20:30:55 peter
  1010. * more fixes to get make cycle working
  1011. Revision 1.7 2000/11/04 18:03:57 florian
  1012. * fixed upper/lower case problem
  1013. Revision 1.6 2000/11/04 17:31:00 florian
  1014. * fixed some problems of previous commit
  1015. Revision 1.5 2000/11/04 14:25:20 florian
  1016. + merged Attila's changes for interfaces, not tested yet
  1017. Revision 1.4 2000/10/31 22:02:49 peter
  1018. * symtable splitted, no real code changes
  1019. Revision 1.3 2000/10/26 21:54:03 peter
  1020. * fixed crash with error in child definition (merged)
  1021. Revision 1.2 2000/10/21 18:16:11 florian
  1022. * a lot of changes:
  1023. - basic dyn. array support
  1024. - basic C++ support
  1025. - some work for interfaces done
  1026. ....
  1027. Revision 1.1 2000/10/14 10:14:51 peter
  1028. * moehrendorf oct 2000 rewrite
  1029. }