pdecobj.pas 50 KB

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