pdecobj.pas 47 KB

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