pdecobj.pas 49 KB

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