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