pdecobj.pas 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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. symconst,symbase,symsym,symtable,defbase,
  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 : tparalinkedlist;
  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. s : string;
  205. tt : ttype;
  206. arraytype : ttype;
  207. declarepos : tfileposinfo;
  208. pp : Tprocdef;
  209. pd : tprocdef;
  210. pt : tnode;
  211. propname : stringid;
  212. dummyst : tparasymtable;
  213. vs : tvarsym;
  214. sc : tsinglelist;
  215. begin
  216. { check for a class }
  217. aktprocsym:=nil;
  218. aktprocdef:=nil;
  219. if not((is_class_or_interface(aktclass)) or
  220. ((m_delphi in aktmodeswitches) and (is_object(aktclass)))) then
  221. Message(parser_e_syntax_error);
  222. consume(_PROPERTY);
  223. propertyparas:=TParaLinkedList.Create;
  224. datacoll:=nil;
  225. if token=_ID then
  226. begin
  227. p:=tpropertysym.create(orgpattern);
  228. propname:=pattern;
  229. consume(_ID);
  230. { property parameters ? }
  231. if token=_LECKKLAMMER then
  232. begin
  233. if (sp_published in current_object_option) then
  234. Message(parser_e_cant_publish_that_property);
  235. { create a list of the parameters in propertyparas }
  236. dummyst:=tparasymtable.create;
  237. dummyst.next:=symtablestack;
  238. symtablestack:=dummyst;
  239. sc:=tsinglelist.create;
  240. consume(_LECKKLAMMER);
  241. inc(testcurobject);
  242. repeat
  243. if token=_VAR then
  244. begin
  245. consume(_VAR);
  246. varspez:=vs_var;
  247. end
  248. else if token=_CONST then
  249. begin
  250. consume(_CONST);
  251. varspez:=vs_const;
  252. end
  253. else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  254. begin
  255. consume(_OUT);
  256. varspez:=vs_out;
  257. end
  258. else
  259. varspez:=vs_value;
  260. sc.reset;
  261. repeat
  262. vs:=tvarsym.create(orgpattern,generrortype);
  263. dummyst.insert(vs);
  264. sc.insert(vs);
  265. consume(_ID);
  266. until not try_to_consume(_COMMA);
  267. if token=_COLON then
  268. begin
  269. consume(_COLON);
  270. if token=_ARRAY then
  271. begin
  272. consume(_ARRAY);
  273. consume(_OF);
  274. { define range and type of range }
  275. tt.setdef(tarraydef.create(0,-1,s32bittype));
  276. { define field type }
  277. single_type(arraytype,s,false);
  278. tarraydef(tt.def).setelementtype(arraytype);
  279. end
  280. else
  281. single_type(tt,s,false);
  282. end
  283. else
  284. tt:=cformaltype;
  285. vs:=tvarsym(sc.first);
  286. while assigned(vs) do
  287. begin
  288. hp2:=TParaItem.create;
  289. hp2.paratyp:=varspez;
  290. hp2.paratype:=tt;
  291. propertyparas.insert(hp2);
  292. vs:=tvarsym(vs.listnext);
  293. end;
  294. until not try_to_consume(_SEMICOLON);
  295. dec(testcurobject);
  296. consume(_RECKKLAMMER);
  297. { remove dummy symtable }
  298. symtablestack:=symtablestack.next;
  299. dummyst.free;
  300. sc.free;
  301. { the parser need to know if a property has parameters, the
  302. index parameter doesn't count (PFV) }
  303. if not(propertyparas.empty) then
  304. include(p.propoptions,ppo_hasparameters);
  305. end;
  306. { overriden property ? }
  307. { force property interface, if there is a property parameter }
  308. if (token=_COLON) or not(propertyparas.empty) then
  309. begin
  310. consume(_COLON);
  311. single_type(p.proptype,hs,false);
  312. if (idtoken=_INDEX) then
  313. begin
  314. consume(_INDEX);
  315. pt:=comp_expr(true);
  316. if is_constnode(pt) and
  317. is_ordinal(pt.resulttype.def) and
  318. (not is_64bitint(pt.resulttype.def)) then
  319. p.index:=tordconstnode(pt).value
  320. else
  321. begin
  322. Message(parser_e_invalid_property_index_value);
  323. p.index:=0;
  324. end;
  325. p.indextype.setdef(pt.resulttype.def);
  326. include(p.propoptions,ppo_indexed);
  327. { concat a longint to the para template }
  328. hp2:=TParaItem.Create;
  329. hp2.paratyp:=vs_value;
  330. hp2.paratype:=p.indextype;
  331. propertyparas.insert(hp2);
  332. pt.free;
  333. end;
  334. end
  335. else
  336. begin
  337. { do an property override }
  338. overriden:=search_class_member(aktclass,propname);
  339. if assigned(overriden) and (overriden.typ=propertysym) then
  340. begin
  341. p.dooverride(tpropertysym(overriden));
  342. end
  343. else
  344. begin
  345. p.proptype:=generrortype;
  346. message(parser_e_no_property_found_to_override);
  347. end;
  348. end;
  349. if (sp_published in current_object_option) and
  350. not(p.proptype.def.is_publishable) then
  351. Message(parser_e_cant_publish_that_property);
  352. { create data defcoll to allow correct parameter checks }
  353. datacoll:=TParaItem.Create;
  354. datacoll.paratyp:=vs_value;
  355. datacoll.paratype:=p.proptype;
  356. if try_to_consume(_READ) then
  357. begin
  358. p.readaccess.clear;
  359. if parse_symlist(p.readaccess) then
  360. begin
  361. sym:=p.readaccess.firstsym^.sym;
  362. case sym.typ of
  363. procsym :
  364. begin
  365. pd:=Tprocsym(sym).search_procdef_bypara(propertyparas,true,false);
  366. if not(assigned(pd)) or
  367. not(is_equal(pd.rettype.def,p.proptype.def)) then
  368. Message(parser_e_ill_property_access_sym);
  369. p.readaccess.setdef(pd);
  370. end;
  371. varsym :
  372. begin
  373. if CheckTypes(p.readaccess.def,p.proptype.def) then
  374. begin
  375. { property parameters are allowed if this is
  376. an indexed property, because the index is then
  377. the parameter.
  378. Note: In the help of Kylix it is written
  379. that it isn't allowed, but the compiler accepts it (PFV) }
  380. if (ppo_hasparameters in p.propoptions) then
  381. Message(parser_e_ill_property_access_sym);
  382. end;
  383. end;
  384. else
  385. Message(parser_e_ill_property_access_sym);
  386. end;
  387. end;
  388. end;
  389. if try_to_consume(_WRITE) then
  390. begin
  391. p.writeaccess.clear;
  392. if parse_symlist(p.writeaccess) then
  393. begin
  394. sym:=p.writeaccess.firstsym^.sym;
  395. case sym.typ of
  396. procsym :
  397. begin
  398. { insert data entry to check access method }
  399. propertyparas.insert(datacoll);
  400. pd:=Tprocsym(sym).search_procdef_bypara(propertyparas,true,false);
  401. { ... and remove it }
  402. propertyparas.remove(datacoll);
  403. if not(assigned(pd)) then
  404. Message(parser_e_ill_property_access_sym);
  405. p.writeaccess.setdef(pd);
  406. end;
  407. varsym :
  408. begin
  409. if CheckTypes(p.writeaccess.def,p.proptype.def) then
  410. begin
  411. { property parameters are allowed if this is
  412. an indexed property, because the index is then
  413. the parameter.
  414. Note: In the help of Kylix it is written
  415. that it isn't allowed, but the compiler accepts it (PFV) }
  416. if (ppo_hasparameters in p.propoptions) then
  417. Message(parser_e_ill_property_access_sym);
  418. end;
  419. end;
  420. else
  421. Message(parser_e_ill_property_access_sym);
  422. end;
  423. end;
  424. end;
  425. include(p.propoptions,ppo_stored);
  426. if try_to_consume(_STORED) then
  427. begin
  428. p.storedaccess.clear;
  429. case token of
  430. _ID:
  431. begin
  432. { in the case that idtoken=_DEFAULT }
  433. { we have to do nothing except }
  434. { setting ppo_stored, it's the same }
  435. { as stored true }
  436. if idtoken<>_DEFAULT then
  437. begin
  438. if parse_symlist(p.storedaccess) then
  439. begin
  440. sym:=p.storedaccess.firstsym^.sym;
  441. case sym.typ of
  442. procsym :
  443. begin
  444. pp:=Tprocsym(sym).search_procdef_nopara_boolret;
  445. if assigned(pp) then
  446. p.storedaccess.setdef(pp)
  447. else
  448. message(parser_e_ill_property_storage_sym);
  449. end;
  450. varsym :
  451. begin
  452. if (ppo_hasparameters in p.propoptions) or
  453. not(is_boolean(p.storedaccess.def)) then
  454. Message(parser_e_stored_property_must_be_boolean);
  455. end;
  456. else
  457. Message(parser_e_ill_property_access_sym);
  458. end;
  459. end;
  460. end;
  461. end;
  462. _FALSE:
  463. begin
  464. consume(_FALSE);
  465. exclude(p.propoptions,ppo_stored);
  466. end;
  467. _TRUE:
  468. consume(_TRUE);
  469. end;
  470. end;
  471. if try_to_consume(_DEFAULT) then
  472. begin
  473. if not(is_ordinal(p.proptype.def) or
  474. is_64bitint(p.proptype.def) or
  475. ((p.proptype.def.deftype=setdef) and
  476. (tsetdef(p.proptype.def).settype=smallset))) or
  477. ((p.proptype.def.deftype=arraydef) and
  478. (ppo_indexed in p.propoptions)) or
  479. (ppo_hasparameters in p.propoptions) 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(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 try_to_consume(_DEFAULT) then
  507. begin
  508. { overriding a default propertyp is allowed
  509. p2:=search_default_property(aktclass);
  510. if assigned(p2) then
  511. message1(parser_e_only_one_default_property,
  512. tobjectdef(p2.owner.defowner)^.objrealname^)
  513. else
  514. }
  515. begin
  516. include(p.propoptions,ppo_defaultproperty);
  517. if propertyparas.empty then
  518. message(parser_e_property_need_paras);
  519. end;
  520. consume(_SEMICOLON);
  521. end;
  522. { clean up }
  523. if assigned(datacoll) then
  524. datacoll.free;
  525. end
  526. else
  527. begin
  528. consume(_ID);
  529. consume(_SEMICOLON);
  530. end;
  531. propertyparas.free;
  532. end;
  533. procedure destructor_head;
  534. begin
  535. consume(_DESTRUCTOR);
  536. inc(lexlevel);
  537. parse_proc_head(potype_destructor);
  538. dec(lexlevel);
  539. if (cs_constructor_name in aktglobalswitches) and (aktprocsym.name<>'DONE') then
  540. Message(parser_e_destructorname_must_be_done);
  541. include(aktclass.objectoptions,oo_has_destructor);
  542. consume(_SEMICOLON);
  543. if not(aktprocdef.Para.empty) then
  544. if (m_fpc in aktmodeswitches) then
  545. Message(parser_e_no_paras_for_destructor);
  546. { no return value }
  547. aktprocdef.rettype:=voidtype;
  548. end;
  549. var
  550. hs : string;
  551. pcrd : tclassrefdef;
  552. tt : ttype;
  553. old_object_option : tsymoptions;
  554. oldprocinfo : tprocinfo;
  555. oldprocsym : tprocsym;
  556. oldprocdef : tprocdef;
  557. oldparse_only : boolean;
  558. storetypecanbeforward : boolean;
  559. procedure setclassattributes;
  560. begin
  561. { publishable }
  562. if classtype in [odt_interfacecom,odt_class] then
  563. begin
  564. aktclass.objecttype:=classtype;
  565. if (cs_generate_rtti in aktlocalswitches) or
  566. (assigned(aktclass.childof) and
  567. (oo_can_have_published in aktclass.childof.objectoptions)) then
  568. begin
  569. include(aktclass.objectoptions,oo_can_have_published);
  570. { in "publishable" classes the default access type is published }
  571. current_object_option:=[sp_published];
  572. end;
  573. end;
  574. end;
  575. procedure setclassparent;
  576. begin
  577. if assigned(fd) then
  578. aktclass:=fd
  579. else
  580. aktclass:=tobjectdef.create(classtype,n,nil);
  581. { is the current class tobject? }
  582. { so you could define your own tobject }
  583. if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_class) and (upper(n)='TOBJECT') then
  584. class_tobject:=aktclass
  585. else if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  586. interface_iunknown:=aktclass
  587. else
  588. begin
  589. case classtype of
  590. odt_class:
  591. childof:=class_tobject;
  592. odt_interfacecom:
  593. childof:=interface_iunknown;
  594. end;
  595. if (oo_is_forward in childof.objectoptions) then
  596. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  597. aktclass.set_parent(childof);
  598. end;
  599. end;
  600. procedure setinterfacemethodoptions;
  601. var
  602. i: longint;
  603. defs: TIndexArray;
  604. pd: tprocdef;
  605. begin
  606. include(aktclass.objectoptions,oo_has_virtual);
  607. defs:=aktclass.symtable.defindex;
  608. for i:=1 to defs.count do
  609. begin
  610. pd:=tprocdef(defs.search(i));
  611. if pd.deftype=procdef then
  612. begin
  613. pd.extnumber:=aktclass.lastvtableindex;
  614. inc(aktclass.lastvtableindex);
  615. include(pd.procoptions,po_virtualmethod);
  616. pd.forwarddef:=false;
  617. end;
  618. end;
  619. end;
  620. function readobjecttype : boolean;
  621. begin
  622. readobjecttype:=true;
  623. { distinguish classes and objects }
  624. case token of
  625. _OBJECT:
  626. begin
  627. classtype:=odt_object;
  628. consume(_OBJECT)
  629. end;
  630. _CPPCLASS:
  631. begin
  632. classtype:=odt_cppclass;
  633. consume(_CPPCLASS);
  634. end;
  635. _INTERFACE:
  636. begin
  637. { need extra check here since interface is a keyword
  638. in all pascal modes }
  639. if not(m_class in aktmodeswitches) then
  640. Message(parser_f_need_objfpc_or_delphi_mode);
  641. if aktinterfacetype=it_interfacecom then
  642. classtype:=odt_interfacecom
  643. else {it_interfacecorba}
  644. classtype:=odt_interfacecorba;
  645. consume(_INTERFACE);
  646. { forward declaration }
  647. if not(assigned(fd)) and (token=_SEMICOLON) then
  648. begin
  649. { also anonym objects aren't allow (o : object a : longint; end;) }
  650. if n='' then
  651. Message(parser_f_no_anonym_objects);
  652. aktclass:=tobjectdef.create(classtype,n,nil);
  653. if (cs_compilesystem in aktmoduleswitches) and
  654. (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  655. interface_iunknown:=aktclass;
  656. include(aktclass.objectoptions,oo_is_forward);
  657. object_dec:=aktclass;
  658. typecanbeforward:=storetypecanbeforward;
  659. readobjecttype:=false;
  660. exit;
  661. end;
  662. end;
  663. _CLASS:
  664. begin
  665. classtype:=odt_class;
  666. consume(_CLASS);
  667. if not(assigned(fd)) and
  668. (token=_OF) and
  669. { Delphi only allows class of in type blocks.
  670. Note that when parsing the type of a variable declaration
  671. the blocktype is bt_type so the check for typecanbeforward
  672. is also necessary (PFV) }
  673. (((block_type=bt_type) and typecanbeforward) or
  674. not(m_delphi in aktmodeswitches)) then
  675. begin
  676. { a hack, but it's easy to handle }
  677. { class reference type }
  678. consume(_OF);
  679. single_type(tt,hs,typecanbeforward);
  680. { accept hp1, if is a forward def or a class }
  681. if (tt.def.deftype=forwarddef) or
  682. is_class(tt.def) then
  683. begin
  684. pcrd:=tclassrefdef.create(tt);
  685. object_dec:=pcrd;
  686. end
  687. else
  688. begin
  689. object_dec:=generrortype.def;
  690. Message1(type_e_class_type_expected,generrortype.def.typename);
  691. end;
  692. typecanbeforward:=storetypecanbeforward;
  693. readobjecttype:=false;
  694. exit;
  695. end
  696. { forward class }
  697. else if not(assigned(fd)) and (token=_SEMICOLON) then
  698. begin
  699. { also anonym objects aren't allow (o : object a : longint; end;) }
  700. if n='' then
  701. Message(parser_f_no_anonym_objects);
  702. aktclass:=tobjectdef.create(odt_class,n,nil);
  703. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  704. class_tobject:=aktclass;
  705. aktclass.objecttype:=odt_class;
  706. include(aktclass.objectoptions,oo_is_forward);
  707. { all classes must have a vmt !! at offset zero }
  708. if not(oo_has_vmt in aktclass.objectoptions) then
  709. aktclass.insertvmt;
  710. object_dec:=aktclass;
  711. typecanbeforward:=storetypecanbeforward;
  712. readobjecttype:=false;
  713. exit;
  714. end;
  715. end;
  716. else
  717. begin
  718. classtype:=odt_class; { this is error but try to recover }
  719. consume(_OBJECT);
  720. end;
  721. end;
  722. end;
  723. procedure handleimplementedinterface(implintf : tobjectdef);
  724. begin
  725. if not is_interface(implintf) then
  726. begin
  727. Message1(type_e_interface_type_expected,implintf.typename);
  728. exit;
  729. end;
  730. if aktclass.implementedinterfaces.searchintf(implintf)<>-1 then
  731. Message1(sym_e_duplicate_id,implintf.name)
  732. else
  733. aktclass.implementedinterfaces.addintf(implintf);
  734. end;
  735. procedure readimplementedinterfaces;
  736. var
  737. tt : ttype;
  738. begin
  739. while try_to_consume(_COMMA) do
  740. begin
  741. id_type(tt,pattern,false);
  742. if (tt.def.deftype<>objectdef) then
  743. begin
  744. Message1(type_e_interface_type_expected,tt.def.typename);
  745. continue;
  746. end;
  747. handleimplementedinterface(tobjectdef(tt.def));
  748. end;
  749. end;
  750. procedure readinterfaceiid;
  751. var
  752. p : tnode;
  753. begin
  754. p:=comp_expr(true);
  755. if p.nodetype=stringconstn then
  756. begin
  757. stringdispose(aktclass.iidstr);
  758. aktclass.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  759. p.free;
  760. aktclass.isiidguidvalid:=string2guid(aktclass.iidstr^,aktclass.iidguid);
  761. if (classtype=odt_interfacecom) and not aktclass.isiidguidvalid then
  762. Message(parser_e_improper_guid_syntax);
  763. end
  764. else
  765. begin
  766. p.free;
  767. Message(cg_e_illegal_expression);
  768. end;
  769. end;
  770. procedure readparentclasses;
  771. var
  772. hp : tobjectdef;
  773. begin
  774. hp:=nil;
  775. { reads the parent class }
  776. if token=_LKLAMMER then
  777. begin
  778. consume(_LKLAMMER);
  779. id_type(tt,pattern,false);
  780. childof:=tobjectdef(tt.def);
  781. if (not assigned(childof)) or
  782. (childof.deftype<>objectdef) then
  783. begin
  784. if assigned(childof) then
  785. Message1(type_e_class_type_expected,childof.typename);
  786. childof:=nil;
  787. aktclass:=tobjectdef.create(classtype,n,nil);
  788. end
  789. else
  790. begin
  791. { a mix of class, interfaces, objects and cppclasses
  792. isn't allowed }
  793. case classtype of
  794. odt_class:
  795. if not(is_class(childof)) then
  796. begin
  797. if is_interface(childof) then
  798. begin
  799. { we insert the interface after the child
  800. is set, see below
  801. }
  802. hp:=childof;
  803. childof:=class_tobject;
  804. end
  805. else
  806. Message(parser_e_mix_of_classes_and_objects);
  807. end;
  808. odt_interfacecorba,
  809. odt_interfacecom:
  810. if not(is_interface(childof)) then
  811. Message(parser_e_mix_of_classes_and_objects);
  812. odt_cppclass:
  813. if not(is_cppclass(childof)) then
  814. Message(parser_e_mix_of_classes_and_objects);
  815. odt_object:
  816. if not(is_object(childof)) then
  817. Message(parser_e_mix_of_classes_and_objects);
  818. end;
  819. { the forward of the child must be resolved to get
  820. correct field addresses }
  821. if assigned(fd) then
  822. begin
  823. if (oo_is_forward in childof.objectoptions) then
  824. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  825. aktclass:=fd;
  826. { we must inherit several options !!
  827. this was missing !!
  828. all is now done in set_parent
  829. including symtable datasize setting PM }
  830. fd.set_parent(childof);
  831. end
  832. else
  833. aktclass:=tobjectdef.create(classtype,n,childof);
  834. if aktclass.objecttype=odt_class then
  835. begin
  836. if assigned(hp) then
  837. handleimplementedinterface(hp);
  838. readimplementedinterfaces;
  839. end;
  840. end;
  841. consume(_RKLAMMER);
  842. end
  843. { if no parent class, then a class get tobject as parent }
  844. else if classtype in [odt_class,odt_interfacecom] then
  845. setclassparent
  846. else
  847. aktclass:=tobjectdef.create(classtype,n,nil);
  848. { read GUID }
  849. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  850. try_to_consume(_LECKKLAMMER) then
  851. begin
  852. readinterfaceiid;
  853. consume(_RECKKLAMMER);
  854. end;
  855. end;
  856. procedure chkcpp;
  857. begin
  858. if is_cppclass(aktclass) then
  859. begin
  860. aktprocdef.proccalloption:=pocall_cppdecl;
  861. aktprocdef.setmangledname(
  862. target_info.Cprefix+aktprocdef.cplusplusmangledname);
  863. end;
  864. end;
  865. begin
  866. {Nowadays aktprocsym may already have a value, so we need to save
  867. it.}
  868. oldprocdef:=aktprocdef;
  869. oldprocsym:=aktprocsym;
  870. old_object_option:=current_object_option;
  871. { forward is resolved }
  872. if assigned(fd) then
  873. exclude(fd.objectoptions,oo_is_forward);
  874. { objects and class types can't be declared local }
  875. if not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) then
  876. Message(parser_e_no_local_objects);
  877. storetypecanbeforward:=typecanbeforward;
  878. { for tp7 don't allow forward types }
  879. if (m_tp7 in aktmodeswitches) then
  880. typecanbeforward:=false;
  881. if not(readobjecttype) then
  882. exit;
  883. { also anonym objects aren't allow (o : object a : longint; end;) }
  884. if n='' then
  885. Message(parser_f_no_anonym_objects);
  886. { read list of parent classes }
  887. readparentclasses;
  888. { default access is public }
  889. there_is_a_destructor:=false;
  890. current_object_option:=[sp_public];
  891. { set class flags and inherits published }
  892. setclassattributes;
  893. aktobjectdef:=aktclass;
  894. aktclass.symtable.next:=symtablestack;
  895. symtablestack:=aktclass.symtable;
  896. testcurobject:=1;
  897. curobjectname:=Upper(n);
  898. { new procinfo }
  899. oldprocinfo:=procinfo;
  900. procinfo:=cprocinfo.create;
  901. procinfo._class:=aktclass;
  902. { short class declaration ? }
  903. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  904. begin
  905. { Parse componenten }
  906. repeat
  907. case token of
  908. _ID :
  909. begin
  910. case idtoken of
  911. _PRIVATE :
  912. begin
  913. if is_interface(aktclass) then
  914. Message(parser_e_no_access_specifier_in_interfaces);
  915. consume(_PRIVATE);
  916. current_object_option:=[sp_private];
  917. include(aktclass.objectoptions,oo_has_private);
  918. end;
  919. _PROTECTED :
  920. begin
  921. if is_interface(aktclass) then
  922. Message(parser_e_no_access_specifier_in_interfaces);
  923. consume(_PROTECTED);
  924. current_object_option:=[sp_protected];
  925. include(aktclass.objectoptions,oo_has_protected);
  926. end;
  927. _PUBLIC :
  928. begin
  929. if is_interface(aktclass) then
  930. Message(parser_e_no_access_specifier_in_interfaces);
  931. consume(_PUBLIC);
  932. current_object_option:=[sp_public];
  933. end;
  934. _PUBLISHED :
  935. begin
  936. { we've to check for a pushlished section in non- }
  937. { publishable classes later, if a real declaration }
  938. { this is the way, delphi does it }
  939. if is_interface(aktclass) then
  940. Message(parser_e_no_access_specifier_in_interfaces);
  941. consume(_PUBLISHED);
  942. current_object_option:=[sp_published];
  943. end;
  944. else
  945. begin
  946. if is_interface(aktclass) then
  947. Message(parser_e_no_vars_in_interfaces);
  948. if (sp_published in current_object_option) and
  949. not(oo_can_have_published in aktclass.objectoptions) then
  950. Message(parser_e_cant_have_published);
  951. read_var_decs(false,true,false);
  952. end;
  953. end;
  954. end;
  955. _PROPERTY :
  956. begin
  957. property_dec;
  958. end;
  959. _PROCEDURE,
  960. _FUNCTION,
  961. _CLASS :
  962. begin
  963. if (sp_published in current_object_option) and
  964. not(oo_can_have_published in aktclass.objectoptions) then
  965. Message(parser_e_cant_have_published);
  966. oldparse_only:=parse_only;
  967. parse_only:=true;
  968. parse_proc_dec;
  969. { this is for error recovery as well as forward }
  970. { interface mappings, i.e. mapping to a method }
  971. { which isn't declared yet }
  972. if assigned(aktprocsym) then
  973. begin
  974. parse_object_proc_directives(aktprocsym);
  975. { add definition to procsym }
  976. proc_add_definition(aktprocsym,aktprocdef);
  977. { add procdef options to objectdef options }
  978. if (po_msgint in aktprocdef.procoptions) then
  979. include(aktclass.objectoptions,oo_has_msgint);
  980. if (po_msgstr in aktprocdef.procoptions) then
  981. include(aktclass.objectoptions,oo_has_msgstr);
  982. if (po_virtualmethod in aktprocdef.procoptions) then
  983. include(aktclass.objectoptions,oo_has_virtual);
  984. chkcpp;
  985. end;
  986. parse_only:=oldparse_only;
  987. end;
  988. _CONSTRUCTOR :
  989. begin
  990. if (sp_published in current_object_option) and
  991. not(oo_can_have_published in aktclass.objectoptions) then
  992. Message(parser_e_cant_have_published);
  993. if not(sp_public in current_object_option) then
  994. Message(parser_w_constructor_should_be_public);
  995. if is_interface(aktclass) then
  996. Message(parser_e_no_con_des_in_interfaces);
  997. oldparse_only:=parse_only;
  998. parse_only:=true;
  999. constructor_head;
  1000. parse_object_proc_directives(aktprocsym);
  1001. { add definition to procsym }
  1002. proc_add_definition(aktprocsym,aktprocdef);
  1003. { add procdef options to objectdef options }
  1004. if (po_virtualmethod in aktprocdef.procoptions) then
  1005. include(aktclass.objectoptions,oo_has_virtual);
  1006. chkcpp;
  1007. parse_only:=oldparse_only;
  1008. end;
  1009. _DESTRUCTOR :
  1010. begin
  1011. if (sp_published in current_object_option) and
  1012. not(oo_can_have_published in aktclass.objectoptions) then
  1013. Message(parser_e_cant_have_published);
  1014. if there_is_a_destructor then
  1015. Message(parser_n_only_one_destructor);
  1016. if is_interface(aktclass) then
  1017. Message(parser_e_no_con_des_in_interfaces);
  1018. if not(sp_public in current_object_option) then
  1019. Message(parser_w_destructor_should_be_public);
  1020. there_is_a_destructor:=true;
  1021. oldparse_only:=parse_only;
  1022. parse_only:=true;
  1023. destructor_head;
  1024. parse_object_proc_directives(aktprocsym);
  1025. { add definition to procsym }
  1026. proc_add_definition(aktprocsym,aktprocdef);
  1027. { add procdef options to objectdef options }
  1028. if (po_virtualmethod in aktprocdef.procoptions) then
  1029. include(aktclass.objectoptions,oo_has_virtual);
  1030. chkcpp;
  1031. parse_only:=oldparse_only;
  1032. end;
  1033. _END :
  1034. begin
  1035. consume(_END);
  1036. break;
  1037. end;
  1038. else
  1039. consume(_ID); { Give a ident expected message, like tp7 }
  1040. end;
  1041. until false;
  1042. end;
  1043. { generate vmt space if needed }
  1044. if not(oo_has_vmt in aktclass.objectoptions) and
  1045. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  1046. (classtype in [odt_class])
  1047. ) then
  1048. aktclass.insertvmt;
  1049. if is_interface(aktclass) then
  1050. setinterfacemethodoptions;
  1051. { reset }
  1052. testcurobject:=0;
  1053. curobjectname:='';
  1054. typecanbeforward:=storetypecanbeforward;
  1055. { restore old state }
  1056. symtablestack:=symtablestack.next;
  1057. aktobjectdef:=nil;
  1058. {Restore procinfo}
  1059. procinfo.free;
  1060. procinfo:=oldprocinfo;
  1061. {Restore the aktprocsym.}
  1062. aktprocsym:=oldprocsym;
  1063. aktprocdef:=oldprocdef;
  1064. current_object_option:=old_object_option;
  1065. object_dec:=aktclass;
  1066. end;
  1067. end.
  1068. {
  1069. $Log$
  1070. Revision 1.54 2002-10-02 18:20:20 peter
  1071. * don't allow interface without m_class mode
  1072. Revision 1.53 2002/09/27 21:13:28 carl
  1073. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  1074. Revision 1.52 2002/09/16 14:11:13 peter
  1075. * add argument to equal_paras() to support default values or not
  1076. Revision 1.51 2002/09/09 17:34:15 peter
  1077. * tdicationary.replace added to replace and item in a dictionary. This
  1078. is only allowed for the same name
  1079. * varsyms are inserted in symtable before the types are parsed. This
  1080. fixes the long standing "var longint : longint" bug
  1081. - consume_idlist and idstringlist removed. The loops are inserted
  1082. at the callers place and uses the symtable for duplicate id checking
  1083. Revision 1.50 2002/09/03 16:26:26 daniel
  1084. * Make Tprocdef.defs protected
  1085. Revision 1.49 2002/08/17 09:23:38 florian
  1086. * first part of procinfo rewrite
  1087. Revision 1.48 2002/08/09 07:33:02 florian
  1088. * a couple of interface related fixes
  1089. Revision 1.47 2002/07/20 11:57:55 florian
  1090. * types.pas renamed to defbase.pas because D6 contains a types
  1091. unit so this would conflicts if D6 programms are compiled
  1092. + Willamette/SSE2 instructions to assembler added
  1093. Revision 1.46 2002/07/01 16:23:53 peter
  1094. * cg64 patch
  1095. * basics for currency
  1096. * asnode updates for class and interface (not finished)
  1097. Revision 1.45 2002/05/18 13:34:12 peter
  1098. * readded missing revisions
  1099. Revision 1.44 2002/05/16 19:46:42 carl
  1100. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1101. + try to fix temp allocation (still in ifdef)
  1102. + generic constructor calls
  1103. + start of tassembler / tmodulebase class cleanup
  1104. Revision 1.42 2002/05/12 16:53:08 peter
  1105. * moved entry and exitcode to ncgutil and cgobj
  1106. * foreach gets extra argument for passing local data to the
  1107. iterator function
  1108. * -CR checks also class typecasts at runtime by changing them
  1109. into as
  1110. * fixed compiler to cycle with the -CR option
  1111. * fixed stabs with elf writer, finally the global variables can
  1112. be watched
  1113. * removed a lot of routines from cga unit and replaced them by
  1114. calls to cgobj
  1115. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1116. u32bit then the other is typecasted also to u32bit without giving
  1117. a rangecheck warning/error.
  1118. * fixed pascal calling method with reversing also the high tree in
  1119. the parast, detected by tcalcst3 test
  1120. Revision 1.41 2002/04/21 19:02:04 peter
  1121. * removed newn and disposen nodes, the code is now directly
  1122. inlined from pexpr
  1123. * -an option that will write the secondpass nodes to the .s file, this
  1124. requires EXTDEBUG define to actually write the info
  1125. * fixed various internal errors and crashes due recent code changes
  1126. Revision 1.40 2002/04/19 15:46:02 peter
  1127. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1128. in most cases and not written to the ppu
  1129. * add mangeledname_prefix() routine to generate the prefix of
  1130. manglednames depending on the current procedure, object and module
  1131. * removed static procprefix since the mangledname is now build only
  1132. on demand from tprocdef.mangledname
  1133. Revision 1.39 2002/04/04 19:06:00 peter
  1134. * removed unused units
  1135. * use tlocation.size in cg.a_*loc*() routines
  1136. Revision 1.38 2002/01/25 17:38:19 peter
  1137. * fixed default value for properties with index values
  1138. Revision 1.37 2002/01/24 18:25:48 peter
  1139. * implicit result variable generation for assembler routines
  1140. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1141. Revision 1.36 2002/01/06 12:08:15 peter
  1142. * removed uauto from orddef, use new range_to_basetype generating
  1143. the correct ordinal type for a range
  1144. }