pdecobj.pas 50 KB

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