pdecobj.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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. begin
  739. { allocate and prepare the GUID only if the class
  740. implements some interfaces.
  741. }
  742. if aktclass.implementedinterfaces.count = 0 then
  743. aktclass.prepareguid;
  744. aktclass.implementedinterfaces.addintf(implintf);
  745. end;
  746. end;
  747. procedure readimplementedinterfaces;
  748. var
  749. tt : ttype;
  750. begin
  751. while try_to_consume(_COMMA) do
  752. begin
  753. id_type(tt,pattern,false);
  754. if (tt.def.deftype<>objectdef) then
  755. begin
  756. Message1(type_e_interface_type_expected,tt.def.typename);
  757. continue;
  758. end;
  759. handleimplementedinterface(tobjectdef(tt.def));
  760. end;
  761. end;
  762. procedure readinterfaceiid;
  763. var
  764. p : tnode;
  765. valid : boolean;
  766. begin
  767. p:=comp_expr(true);
  768. if p.nodetype=stringconstn then
  769. begin
  770. stringdispose(aktclass.iidstr);
  771. aktclass.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  772. p.free;
  773. valid:=string2guid(aktclass.iidstr^,aktclass.iidguid^);
  774. if (classtype=odt_interfacecom) and not assigned(aktclass.iidguid) and not valid then
  775. Message(parser_e_improper_guid_syntax);
  776. end
  777. else
  778. begin
  779. p.free;
  780. Message(cg_e_illegal_expression);
  781. end;
  782. end;
  783. procedure readparentclasses;
  784. var
  785. hp : tobjectdef;
  786. begin
  787. hp:=nil;
  788. { reads the parent class }
  789. if token=_LKLAMMER then
  790. begin
  791. consume(_LKLAMMER);
  792. id_type(tt,pattern,false);
  793. childof:=tobjectdef(tt.def);
  794. if (not assigned(childof)) or
  795. (childof.deftype<>objectdef) then
  796. begin
  797. if assigned(childof) then
  798. Message1(type_e_class_type_expected,childof.typename);
  799. childof:=nil;
  800. aktclass:=tobjectdef.create(classtype,n,nil);
  801. end
  802. else
  803. begin
  804. { a mix of class, interfaces, objects and cppclasses
  805. isn't allowed }
  806. case classtype of
  807. odt_class:
  808. if not(is_class(childof)) then
  809. begin
  810. if is_interface(childof) then
  811. begin
  812. { we insert the interface after the child
  813. is set, see below
  814. }
  815. hp:=childof;
  816. childof:=class_tobject;
  817. end
  818. else
  819. Message(parser_e_mix_of_classes_and_objects);
  820. end;
  821. odt_interfacecorba,
  822. odt_interfacecom:
  823. if not(is_interface(childof)) then
  824. Message(parser_e_mix_of_classes_and_objects);
  825. odt_cppclass:
  826. if not(is_cppclass(childof)) then
  827. Message(parser_e_mix_of_classes_and_objects);
  828. odt_object:
  829. if not(is_object(childof)) then
  830. Message(parser_e_mix_of_classes_and_objects);
  831. end;
  832. { the forward of the child must be resolved to get
  833. correct field addresses }
  834. if assigned(fd) then
  835. begin
  836. if (oo_is_forward in childof.objectoptions) then
  837. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  838. aktclass:=fd;
  839. { we must inherit several options !!
  840. this was missing !!
  841. all is now done in set_parent
  842. including symtable datasize setting PM }
  843. fd.set_parent(childof);
  844. end
  845. else
  846. aktclass:=tobjectdef.create(classtype,n,childof);
  847. if aktclass.objecttype=odt_class then
  848. begin
  849. if assigned(hp) then
  850. handleimplementedinterface(hp);
  851. readimplementedinterfaces;
  852. end;
  853. end;
  854. consume(_RKLAMMER);
  855. end
  856. { if no parent class, then a class get tobject as parent }
  857. else if classtype in [odt_class,odt_interfacecom] then
  858. setclassparent
  859. else
  860. aktclass:=tobjectdef.create(classtype,n,nil);
  861. { read GUID }
  862. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  863. try_to_consume(_LECKKLAMMER) then
  864. begin
  865. readinterfaceiid;
  866. consume(_RECKKLAMMER);
  867. end;
  868. end;
  869. procedure chkcpp;
  870. begin
  871. if is_cppclass(aktclass) then
  872. begin
  873. aktprocdef.proccalloption:=pocall_cppdecl;
  874. aktprocdef.setmangledname(
  875. target_info.Cprefix+aktprocdef.cplusplusmangledname);
  876. end;
  877. end;
  878. begin
  879. {Nowadays aktprocsym may already have a value, so we need to save
  880. it.}
  881. oldprocdef:=aktprocdef;
  882. oldprocsym:=aktprocsym;
  883. old_object_option:=current_object_option;
  884. { forward is resolved }
  885. if assigned(fd) then
  886. exclude(fd.objectoptions,oo_is_forward);
  887. { objects and class types can't be declared local }
  888. if not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) then
  889. Message(parser_e_no_local_objects);
  890. storetypecanbeforward:=typecanbeforward;
  891. { for tp7 don't allow forward types }
  892. if (m_tp7 in aktmodeswitches) then
  893. typecanbeforward:=false;
  894. if not(readobjecttype) then
  895. exit;
  896. { also anonym objects aren't allow (o : object a : longint; end;) }
  897. if n='' then
  898. Message(parser_f_no_anonym_objects);
  899. { read list of parent classes }
  900. readparentclasses;
  901. { default access is public }
  902. there_is_a_destructor:=false;
  903. current_object_option:=[sp_public];
  904. { set class flags and inherits published }
  905. setclassattributes;
  906. aktobjectdef:=aktclass;
  907. aktclass.symtable.next:=symtablestack;
  908. symtablestack:=aktclass.symtable;
  909. testcurobject:=1;
  910. curobjectname:=Upper(n);
  911. { new procinfo }
  912. oldprocinfo:=procinfo;
  913. procinfo:=cprocinfo.create;
  914. procinfo._class:=aktclass;
  915. { short class declaration ? }
  916. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  917. begin
  918. { Parse componenten }
  919. repeat
  920. case token of
  921. _ID :
  922. begin
  923. case idtoken of
  924. _PRIVATE :
  925. begin
  926. if is_interface(aktclass) then
  927. Message(parser_e_no_access_specifier_in_interfaces);
  928. consume(_PRIVATE);
  929. current_object_option:=[sp_private];
  930. include(aktclass.objectoptions,oo_has_private);
  931. end;
  932. _PROTECTED :
  933. begin
  934. if is_interface(aktclass) then
  935. Message(parser_e_no_access_specifier_in_interfaces);
  936. consume(_PROTECTED);
  937. current_object_option:=[sp_protected];
  938. include(aktclass.objectoptions,oo_has_protected);
  939. end;
  940. _PUBLIC :
  941. begin
  942. if is_interface(aktclass) then
  943. Message(parser_e_no_access_specifier_in_interfaces);
  944. consume(_PUBLIC);
  945. current_object_option:=[sp_public];
  946. end;
  947. _PUBLISHED :
  948. begin
  949. { we've to check for a pushlished section in non- }
  950. { publishable classes later, if a real declaration }
  951. { this is the way, delphi does it }
  952. if is_interface(aktclass) then
  953. Message(parser_e_no_access_specifier_in_interfaces);
  954. consume(_PUBLISHED);
  955. current_object_option:=[sp_published];
  956. end;
  957. else
  958. begin
  959. if is_interface(aktclass) then
  960. Message(parser_e_no_vars_in_interfaces);
  961. if (sp_published in current_object_option) and
  962. not(oo_can_have_published in aktclass.objectoptions) then
  963. Message(parser_e_cant_have_published);
  964. read_var_decs(false,true,false);
  965. end;
  966. end;
  967. end;
  968. _PROPERTY :
  969. begin
  970. property_dec;
  971. end;
  972. _PROCEDURE,
  973. _FUNCTION,
  974. _CLASS :
  975. begin
  976. if (sp_published in current_object_option) and
  977. not(oo_can_have_published in aktclass.objectoptions) then
  978. Message(parser_e_cant_have_published);
  979. oldparse_only:=parse_only;
  980. parse_only:=true;
  981. parse_proc_dec;
  982. { this is for error recovery as well as forward }
  983. { interface mappings, i.e. mapping to a method }
  984. { which isn't declared yet }
  985. if assigned(aktprocsym) then
  986. begin
  987. parse_object_proc_directives(aktprocsym);
  988. { add definition to procsym }
  989. proc_add_definition(aktprocsym,aktprocdef);
  990. { add procdef options to objectdef options }
  991. if (po_msgint in aktprocdef.procoptions) then
  992. include(aktclass.objectoptions,oo_has_msgint);
  993. if (po_msgstr in aktprocdef.procoptions) then
  994. include(aktclass.objectoptions,oo_has_msgstr);
  995. if (po_virtualmethod in aktprocdef.procoptions) then
  996. include(aktclass.objectoptions,oo_has_virtual);
  997. chkcpp;
  998. end;
  999. parse_only:=oldparse_only;
  1000. end;
  1001. _CONSTRUCTOR :
  1002. begin
  1003. if (sp_published in current_object_option) and
  1004. not(oo_can_have_published in aktclass.objectoptions) then
  1005. Message(parser_e_cant_have_published);
  1006. if not(sp_public in current_object_option) then
  1007. Message(parser_w_constructor_should_be_public);
  1008. if is_interface(aktclass) then
  1009. Message(parser_e_no_con_des_in_interfaces);
  1010. oldparse_only:=parse_only;
  1011. parse_only:=true;
  1012. constructor_head;
  1013. parse_object_proc_directives(aktprocsym);
  1014. { add definition to procsym }
  1015. proc_add_definition(aktprocsym,aktprocdef);
  1016. { add procdef options to objectdef options }
  1017. if (po_virtualmethod in aktprocdef.procoptions) then
  1018. include(aktclass.objectoptions,oo_has_virtual);
  1019. chkcpp;
  1020. parse_only:=oldparse_only;
  1021. end;
  1022. _DESTRUCTOR :
  1023. begin
  1024. if (sp_published in current_object_option) and
  1025. not(oo_can_have_published in aktclass.objectoptions) then
  1026. Message(parser_e_cant_have_published);
  1027. if there_is_a_destructor then
  1028. Message(parser_n_only_one_destructor);
  1029. if is_interface(aktclass) then
  1030. Message(parser_e_no_con_des_in_interfaces);
  1031. if not(sp_public in current_object_option) then
  1032. Message(parser_w_destructor_should_be_public);
  1033. there_is_a_destructor:=true;
  1034. oldparse_only:=parse_only;
  1035. parse_only:=true;
  1036. destructor_head;
  1037. parse_object_proc_directives(aktprocsym);
  1038. { add definition to procsym }
  1039. proc_add_definition(aktprocsym,aktprocdef);
  1040. { add procdef options to objectdef options }
  1041. if (po_virtualmethod in aktprocdef.procoptions) then
  1042. include(aktclass.objectoptions,oo_has_virtual);
  1043. chkcpp;
  1044. parse_only:=oldparse_only;
  1045. end;
  1046. _END :
  1047. begin
  1048. consume(_END);
  1049. break;
  1050. end;
  1051. else
  1052. consume(_ID); { Give a ident expected message, like tp7 }
  1053. end;
  1054. until false;
  1055. end;
  1056. { generate vmt space if needed }
  1057. if not(oo_has_vmt in aktclass.objectoptions) and
  1058. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  1059. (classtype in [odt_class])
  1060. ) then
  1061. aktclass.insertvmt;
  1062. if is_interface(aktclass) then
  1063. setinterfacemethodoptions;
  1064. { reset }
  1065. testcurobject:=0;
  1066. curobjectname:='';
  1067. typecanbeforward:=storetypecanbeforward;
  1068. { restore old state }
  1069. symtablestack:=symtablestack.next;
  1070. aktobjectdef:=nil;
  1071. {Restore procinfo}
  1072. procinfo.free;
  1073. procinfo:=oldprocinfo;
  1074. {Restore the aktprocsym.}
  1075. aktprocsym:=oldprocsym;
  1076. aktprocdef:=oldprocdef;
  1077. current_object_option:=old_object_option;
  1078. object_dec:=aktclass;
  1079. end;
  1080. end.
  1081. {
  1082. $Log$
  1083. Revision 1.56 2002-11-17 16:31:56 carl
  1084. * memory optimization (3-4%) : cleanup of tai fields,
  1085. cleanup of tdef and tsym fields.
  1086. * make it work for m68k
  1087. Revision 1.55 2002/10/05 12:43:25 carl
  1088. * fixes for Delphi 6 compilation
  1089. (warning : Some features do not work under Delphi)
  1090. Revision 1.54 2002/10/02 18:20:20 peter
  1091. * don't allow interface without m_class mode
  1092. Revision 1.53 2002/09/27 21:13:28 carl
  1093. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  1094. Revision 1.52 2002/09/16 14:11:13 peter
  1095. * add argument to equal_paras() to support default values or not
  1096. Revision 1.51 2002/09/09 17:34:15 peter
  1097. * tdicationary.replace added to replace and item in a dictionary. This
  1098. is only allowed for the same name
  1099. * varsyms are inserted in symtable before the types are parsed. This
  1100. fixes the long standing "var longint : longint" bug
  1101. - consume_idlist and idstringlist removed. The loops are inserted
  1102. at the callers place and uses the symtable for duplicate id checking
  1103. Revision 1.50 2002/09/03 16:26:26 daniel
  1104. * Make Tprocdef.defs protected
  1105. Revision 1.49 2002/08/17 09:23:38 florian
  1106. * first part of procinfo rewrite
  1107. Revision 1.48 2002/08/09 07:33:02 florian
  1108. * a couple of interface related fixes
  1109. Revision 1.47 2002/07/20 11:57:55 florian
  1110. * types.pas renamed to defbase.pas because D6 contains a types
  1111. unit so this would conflicts if D6 programms are compiled
  1112. + Willamette/SSE2 instructions to assembler added
  1113. Revision 1.46 2002/07/01 16:23:53 peter
  1114. * cg64 patch
  1115. * basics for currency
  1116. * asnode updates for class and interface (not finished)
  1117. Revision 1.45 2002/05/18 13:34:12 peter
  1118. * readded missing revisions
  1119. Revision 1.44 2002/05/16 19:46:42 carl
  1120. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1121. + try to fix temp allocation (still in ifdef)
  1122. + generic constructor calls
  1123. + start of tassembler / tmodulebase class cleanup
  1124. Revision 1.42 2002/05/12 16:53:08 peter
  1125. * moved entry and exitcode to ncgutil and cgobj
  1126. * foreach gets extra argument for passing local data to the
  1127. iterator function
  1128. * -CR checks also class typecasts at runtime by changing them
  1129. into as
  1130. * fixed compiler to cycle with the -CR option
  1131. * fixed stabs with elf writer, finally the global variables can
  1132. be watched
  1133. * removed a lot of routines from cga unit and replaced them by
  1134. calls to cgobj
  1135. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1136. u32bit then the other is typecasted also to u32bit without giving
  1137. a rangecheck warning/error.
  1138. * fixed pascal calling method with reversing also the high tree in
  1139. the parast, detected by tcalcst3 test
  1140. Revision 1.41 2002/04/21 19:02:04 peter
  1141. * removed newn and disposen nodes, the code is now directly
  1142. inlined from pexpr
  1143. * -an option that will write the secondpass nodes to the .s file, this
  1144. requires EXTDEBUG define to actually write the info
  1145. * fixed various internal errors and crashes due recent code changes
  1146. Revision 1.40 2002/04/19 15:46:02 peter
  1147. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1148. in most cases and not written to the ppu
  1149. * add mangeledname_prefix() routine to generate the prefix of
  1150. manglednames depending on the current procedure, object and module
  1151. * removed static procprefix since the mangledname is now build only
  1152. on demand from tprocdef.mangledname
  1153. Revision 1.39 2002/04/04 19:06:00 peter
  1154. * removed unused units
  1155. * use tlocation.size in cg.a_*loc*() routines
  1156. Revision 1.38 2002/01/25 17:38:19 peter
  1157. * fixed default value for properties with index values
  1158. Revision 1.37 2002/01/24 18:25:48 peter
  1159. * implicit result variable generation for assembler routines
  1160. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1161. Revision 1.36 2002/01/06 12:08:15 peter
  1162. * removed uauto from orddef, use new range_to_basetype generating
  1163. the correct ordinal type for a range
  1164. }