pdecobj.pas 49 KB

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