pdecobj.pas 48 KB

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