pdecobj.pas 48 KB

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