pdecobj.pas 51 KB

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