pdecobj.pas 50 KB

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