pdecobj.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. interface
  21. uses
  22. globtype,symtype,symdef;
  23. { parses a object declaration }
  24. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  25. implementation
  26. uses
  27. cutils,cobjects,
  28. globals,verbose,systems,tokens,
  29. aasm,symconst,symbase,symsym,symtable,types,
  30. {$ifdef GDB}
  31. gdb,
  32. {$endif}
  33. hcodegen,hcgdata,
  34. node,nld,ncon,ncnv,pass_1,
  35. scanner,
  36. pbase,pexpr,pdecl,psub,pdecsub,pdecvar,ptype;
  37. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  38. { this function parses an object or class declaration }
  39. var
  40. actmembertype : tsymoptions;
  41. there_is_a_destructor : boolean;
  42. classtype : (ct_object,ct_class,ct_interfacecom,ct_interfaceraw,ct_cppclass);
  43. childof : pobjectdef;
  44. aktclass : pobjectdef;
  45. procedure constructor_head;
  46. begin
  47. consume(_CONSTRUCTOR);
  48. { must be at same level as in implementation }
  49. inc(lexlevel);
  50. parse_proc_head(potype_constructor);
  51. dec(lexlevel);
  52. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'INIT') then
  53. Message(parser_e_constructorname_must_be_init);
  54. include(aktclass^.objectoptions,oo_has_constructor);
  55. consume(_SEMICOLON);
  56. begin
  57. if (aktclass^.is_class) then
  58. begin
  59. { CLASS constructors return the created instance }
  60. aktprocsym^.definition^.rettype.def:=aktclass;
  61. end
  62. else
  63. begin
  64. { OBJECT constructors return a boolean }
  65. aktprocsym^.definition^.rettype.setdef(booldef);
  66. end;
  67. end;
  68. end;
  69. procedure property_dec;
  70. var
  71. sym : psym;
  72. propertyparas : plinkedlist;
  73. { returns the matching procedure to access a property }
  74. function get_procdef : pprocdef;
  75. var
  76. p : pprocdef;
  77. begin
  78. p:=pprocsym(sym)^.definition;
  79. get_procdef:=nil;
  80. while assigned(p) do
  81. begin
  82. if equal_paras(p^.para,propertyparas,cp_value_equal_const) then
  83. break;
  84. p:=p^.nextoverloaded;
  85. end;
  86. get_procdef:=p;
  87. end;
  88. var
  89. hp2,datacoll : pparaitem;
  90. p : ppropertysym;
  91. overriden : psym;
  92. hs : string;
  93. varspez : tvarspez;
  94. sc : pstringcontainer;
  95. s : string;
  96. tt : ttype;
  97. declarepos : tfileposinfo;
  98. pp : pprocdef;
  99. pt : tnode;
  100. propname : stringid;
  101. begin
  102. { check for a class }
  103. if not(aktclass^.is_class) then
  104. Message(parser_e_syntax_error);
  105. consume(_PROPERTY);
  106. new(propertyparas,init);
  107. datacoll:=nil;
  108. if token=_ID then
  109. begin
  110. p:=new(ppropertysym,init(orgpattern));
  111. propname:=pattern;
  112. consume(_ID);
  113. { property parameters ? }
  114. if token=_LECKKLAMMER then
  115. begin
  116. if (sp_published in current_object_option) then
  117. Message(parser_e_cant_publish_that_property);
  118. { create a list of the parameters in propertyparas }
  119. consume(_LECKKLAMMER);
  120. inc(testcurobject);
  121. repeat
  122. if token=_VAR then
  123. begin
  124. consume(_VAR);
  125. varspez:=vs_var;
  126. end
  127. else if token=_CONST then
  128. begin
  129. consume(_CONST);
  130. varspez:=vs_const;
  131. end
  132. else if token=_OUT then
  133. begin
  134. consume(_OUT);
  135. varspez:=vs_out;
  136. end
  137. else varspez:=vs_value;
  138. sc:=idlist;
  139. {$ifdef fixLeaksOnError}
  140. strContStack.push(sc);
  141. {$endif fixLeaksOnError}
  142. if token=_COLON then
  143. begin
  144. consume(_COLON);
  145. if token=_ARRAY then
  146. begin
  147. {
  148. if (varspez<>vs_const) and
  149. (varspez<>vs_var) then
  150. begin
  151. varspez:=vs_const;
  152. Message(parser_e_illegal_open_parameter);
  153. end;
  154. }
  155. consume(_ARRAY);
  156. consume(_OF);
  157. { define range and type of range }
  158. tt.setdef(new(parraydef,init(0,-1,s32bitdef)));
  159. { define field type }
  160. single_type(parraydef(tt.def)^.elementtype,s,false);
  161. end
  162. else
  163. single_type(tt,s,false);
  164. end
  165. else
  166. tt.setdef(cformaldef);
  167. repeat
  168. s:=sc^.get_with_tokeninfo(declarepos);
  169. if s='' then
  170. break;
  171. new(hp2,init);
  172. hp2^.paratyp:=varspez;
  173. hp2^.paratype:=tt;
  174. propertyparas^.insert(hp2);
  175. until false;
  176. {$ifdef fixLeaksOnError}
  177. if strContStack.pop <> sc then
  178. writeln('problem with strContStack in ptype');
  179. {$endif fixLeaksOnError}
  180. dispose(sc,done);
  181. until not try_to_consume(_SEMICOLON);
  182. dec(testcurobject);
  183. consume(_RECKKLAMMER);
  184. end;
  185. { overriden property ? }
  186. { force property interface, if there is a property parameter }
  187. if (token=_COLON) or not(propertyparas^.empty) then
  188. begin
  189. consume(_COLON);
  190. single_type(p^.proptype,hs,false);
  191. if (idtoken=_INDEX) then
  192. begin
  193. consume(_INDEX);
  194. pt:=comp_expr(true);
  195. do_firstpass(pt);
  196. if is_ordinal(pt.resulttype) and
  197. (not is_64bitint(pt.resulttype)) then
  198. p^.index:=tordconstnode(pt).value
  199. else
  200. begin
  201. Message(parser_e_invalid_property_index_value);
  202. p^.index:=0;
  203. end;
  204. p^.indextype.setdef(pt.resulttype);
  205. include(p^.propoptions,ppo_indexed);
  206. { concat a longint to the para template }
  207. new(hp2,init);
  208. hp2^.paratyp:=vs_value;
  209. hp2^.paratype:=p^.indextype;
  210. propertyparas^.insert(hp2);
  211. pt.free;
  212. end;
  213. { the parser need to know if a property has parameters }
  214. if not(propertyparas^.empty) then
  215. include(p^.propoptions,ppo_hasparameters);
  216. end
  217. else
  218. begin
  219. { do an property override }
  220. overriden:=search_class_member(aktclass,propname);
  221. if assigned(overriden) and (overriden^.typ=propertysym) then
  222. begin
  223. p^.dooverride(ppropertysym(overriden));
  224. end
  225. else
  226. begin
  227. p^.proptype.setdef(generrordef);
  228. message(parser_e_no_property_found_to_override);
  229. end;
  230. end;
  231. if (sp_published in current_object_option) and
  232. not(p^.proptype.def^.is_publishable) then
  233. Message(parser_e_cant_publish_that_property);
  234. { create data defcoll to allow correct parameter checks }
  235. new(datacoll,init);
  236. datacoll^.paratyp:=vs_value;
  237. datacoll^.paratype:=p^.proptype;
  238. if (idtoken=_READ) then
  239. begin
  240. p^.readaccess^.clear;
  241. consume(_READ);
  242. sym:=search_class_member(aktclass,pattern);
  243. if not(assigned(sym)) then
  244. begin
  245. Message1(sym_e_unknown_id,pattern);
  246. consume(_ID);
  247. end
  248. else
  249. begin
  250. consume(_ID);
  251. while (token=_POINT) and
  252. ((sym^.typ=varsym) and
  253. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  254. begin
  255. p^.readaccess^.addsym(sym);
  256. consume(_POINT);
  257. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  258. if not assigned(srsym) then
  259. Message1(sym_e_illegal_field,pattern);
  260. sym:=srsym;
  261. consume(_ID);
  262. end;
  263. end;
  264. if assigned(sym) then
  265. begin
  266. { search the matching definition }
  267. case sym^.typ of
  268. procsym :
  269. begin
  270. pp:=get_procdef;
  271. if not(assigned(pp)) or
  272. not(is_equal(pp^.rettype.def,p^.proptype.def)) then
  273. Message(parser_e_ill_property_access_sym);
  274. p^.readaccess^.setdef(pp);
  275. end;
  276. varsym :
  277. begin
  278. if not(propertyparas^.empty) or
  279. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  280. Message(parser_e_ill_property_access_sym);
  281. end;
  282. else
  283. Message(parser_e_ill_property_access_sym);
  284. end;
  285. p^.readaccess^.addsym(sym);
  286. end;
  287. end;
  288. if (idtoken=_WRITE) then
  289. begin
  290. p^.writeaccess^.clear;
  291. consume(_WRITE);
  292. sym:=search_class_member(aktclass,pattern);
  293. if not(assigned(sym)) then
  294. begin
  295. Message1(sym_e_unknown_id,pattern);
  296. consume(_ID);
  297. end
  298. else
  299. begin
  300. consume(_ID);
  301. while (token=_POINT) and
  302. ((sym^.typ=varsym) and
  303. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  304. begin
  305. p^.writeaccess^.addsym(sym);
  306. consume(_POINT);
  307. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  308. if not assigned(srsym) then
  309. Message1(sym_e_illegal_field,pattern);
  310. sym:=srsym;
  311. consume(_ID);
  312. end;
  313. end;
  314. if assigned(sym) then
  315. begin
  316. { search the matching definition }
  317. case sym^.typ of
  318. procsym :
  319. begin
  320. { insert data entry to check access method }
  321. propertyparas^.insert(datacoll);
  322. pp:=get_procdef;
  323. { ... and remove it }
  324. propertyparas^.remove(datacoll);
  325. if not(assigned(pp)) then
  326. Message(parser_e_ill_property_access_sym);
  327. p^.writeaccess^.setdef(pp);
  328. end;
  329. varsym :
  330. begin
  331. if not(propertyparas^.empty) or
  332. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  333. Message(parser_e_ill_property_access_sym);
  334. end
  335. else
  336. Message(parser_e_ill_property_access_sym);
  337. end;
  338. p^.writeaccess^.addsym(sym);
  339. end;
  340. end;
  341. include(p^.propoptions,ppo_stored);
  342. if (idtoken=_STORED) then
  343. begin
  344. consume(_STORED);
  345. p^.storedaccess^.clear;
  346. case token of
  347. _ID:
  348. { in the case that idtoken=_DEFAULT }
  349. { we have to do nothing except }
  350. { setting ppo_stored, it's the same }
  351. { as stored true }
  352. if idtoken<>_DEFAULT then
  353. begin
  354. sym:=search_class_member(aktclass,pattern);
  355. if not(assigned(sym)) then
  356. begin
  357. Message1(sym_e_unknown_id,pattern);
  358. consume(_ID);
  359. end
  360. else
  361. begin
  362. consume(_ID);
  363. while (token=_POINT) and
  364. ((sym^.typ=varsym) and
  365. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  366. begin
  367. p^.storedaccess^.addsym(sym);
  368. consume(_POINT);
  369. getsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  370. if not assigned(srsym) then
  371. Message1(sym_e_illegal_field,pattern);
  372. sym:=srsym;
  373. consume(_ID);
  374. end;
  375. end;
  376. if assigned(sym) then
  377. begin
  378. { only non array properties can be stored }
  379. case sym^.typ of
  380. procsym :
  381. begin
  382. pp:=pprocsym(sym)^.definition;
  383. while assigned(pp) do
  384. begin
  385. { the stored function shouldn't have any parameters }
  386. if pp^.para^.empty then
  387. break;
  388. pp:=pp^.nextoverloaded;
  389. end;
  390. { found we a procedure and does it really return a bool? }
  391. if not(assigned(pp)) or
  392. not(is_equal(pp^.rettype.def,booldef)) then
  393. Message(parser_e_ill_property_storage_sym);
  394. p^.storedaccess^.setdef(pp);
  395. end;
  396. varsym :
  397. begin
  398. if not(propertyparas^.empty) or
  399. not(is_equal(pvarsym(sym)^.vartype.def,booldef)) then
  400. Message(parser_e_stored_property_must_be_boolean);
  401. end;
  402. else
  403. Message(parser_e_ill_property_storage_sym);
  404. end;
  405. p^.storedaccess^.addsym(sym);
  406. end;
  407. end;
  408. _FALSE:
  409. begin
  410. consume(_FALSE);
  411. exclude(p^.propoptions,ppo_stored);
  412. end;
  413. _TRUE:
  414. consume(_TRUE);
  415. end;
  416. end;
  417. if (idtoken=_DEFAULT) then
  418. begin
  419. consume(_DEFAULT);
  420. if not(is_ordinal(p^.proptype.def) or
  421. is_64bitint(p^.proptype.def) or
  422. ((p^.proptype.def^.deftype=setdef) and
  423. (psetdef(p^.proptype.def)^.settype=smallset))) or
  424. not(propertyparas^.empty) then
  425. Message(parser_e_property_cant_have_a_default_value);
  426. { Get the result of the default, the firstpass is
  427. needed to support values like -1 }
  428. pt:=comp_expr(true);
  429. do_firstpass(pt);
  430. if (p^.proptype.def^.deftype=setdef) and
  431. (pt.nodetype=arrayconstructorn) then
  432. begin
  433. arrayconstructor_to_set(tarrayconstructornode(pt));
  434. do_firstpass(pt);
  435. end;
  436. pt:=gentypeconvnode(pt,p^.proptype.def);
  437. do_firstpass(pt);
  438. if not(is_constnode(pt)) then
  439. Message(parser_e_property_default_value_must_const);
  440. if pt.nodetype=setconstn then
  441. p^.default:=plongint(tsetconstnode(pt).value_set)^
  442. else
  443. p^.default:=tordconstnode(pt).value;
  444. pt.free;
  445. end
  446. else if (idtoken=_NODEFAULT) then
  447. begin
  448. consume(_NODEFAULT);
  449. p^.default:=0;
  450. end;
  451. symtablestack^.insert(p);
  452. { default property ? }
  453. consume(_SEMICOLON);
  454. if (idtoken=_DEFAULT) then
  455. begin
  456. consume(_DEFAULT);
  457. { overriding a default propertyp is allowed
  458. p2:=search_default_property(aktclass);
  459. if assigned(p2) then
  460. message1(parser_e_only_one_default_property,
  461. pobjectdef(p2^.owner^.defowner)^.objname^)
  462. else
  463. }
  464. begin
  465. include(p^.propoptions,ppo_defaultproperty);
  466. if propertyparas^.empty then
  467. message(parser_e_property_need_paras);
  468. end;
  469. consume(_SEMICOLON);
  470. end;
  471. { clean up }
  472. if assigned(datacoll) then
  473. dispose(datacoll,done);
  474. end
  475. else
  476. begin
  477. consume(_ID);
  478. consume(_SEMICOLON);
  479. end;
  480. dispose(propertyparas,done);
  481. end;
  482. procedure destructor_head;
  483. begin
  484. consume(_DESTRUCTOR);
  485. inc(lexlevel);
  486. parse_proc_head(potype_destructor);
  487. dec(lexlevel);
  488. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'DONE') then
  489. Message(parser_e_destructorname_must_be_done);
  490. include(aktclass^.objectoptions,oo_has_destructor);
  491. consume(_SEMICOLON);
  492. if not(aktprocsym^.definition^.para^.empty) then
  493. if not (m_tp in aktmodeswitches) then
  494. Message(parser_e_no_paras_for_destructor);
  495. { no return value }
  496. aktprocsym^.definition^.rettype.def:=voiddef;
  497. end;
  498. var
  499. hs : string;
  500. pcrd : pclassrefdef;
  501. tt : ttype;
  502. oldprocinfo : pprocinfo;
  503. oldprocsym : pprocsym;
  504. oldparse_only : boolean;
  505. methodnametable,intmessagetable,
  506. strmessagetable,classnamelabel,
  507. fieldtablelabel : pasmlabel;
  508. storetypecanbeforward : boolean;
  509. procedure setclassattributes;
  510. begin
  511. if classtype=ct_class then
  512. begin
  513. include(aktclass^.objectoptions,oo_is_class);
  514. if (cs_generate_rtti in aktlocalswitches) or
  515. (assigned(aktclass^.childof) and
  516. (oo_can_have_published in aktclass^.childof^.objectoptions)) then
  517. begin
  518. include(aktclass^.objectoptions,oo_can_have_published);
  519. { in "publishable" classes the default access type is published }
  520. actmembertype:=[sp_published];
  521. { don't know if this is necessary (FK) }
  522. current_object_option:=[sp_published];
  523. end;
  524. end;
  525. end;
  526. procedure setclassparent;
  527. begin
  528. { is the current class tobject? }
  529. { so you could define your own tobject }
  530. if (cs_compilesystem in aktmoduleswitches) and
  531. (upper(n)='TOBJECT') then
  532. begin
  533. if assigned(fd) then
  534. aktclass:=fd
  535. else
  536. aktclass:=new(pobjectdef,init(n,nil));
  537. class_tobject:=aktclass;
  538. end
  539. else
  540. begin
  541. childof:=class_tobject;
  542. if assigned(fd) then
  543. begin
  544. { the forward of the child must be resolved to get
  545. correct field addresses
  546. }
  547. if (oo_is_forward in childof^.objectoptions) then
  548. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  549. aktclass:=fd;
  550. aktclass^.set_parent(childof);
  551. end
  552. else
  553. begin
  554. aktclass:=new(pobjectdef,init(n,childof));
  555. aktclass^.set_parent(childof);
  556. end;
  557. end;
  558. end;
  559. { generates the vmt for classes as well as for objects }
  560. procedure writevmt;
  561. var
  562. vmtlist : taasmoutput;
  563. {$ifdef WITHDMT}
  564. dmtlabel : pasmlabel;
  565. {$endif WITHDMT}
  566. begin
  567. {$ifdef WITHDMT}
  568. dmtlabel:=gendmt(aktclass);
  569. {$endif WITHDMT}
  570. { this generates the entries }
  571. vmtlist.init;
  572. genvmt(@vmtlist,aktclass);
  573. { write tables for classes, this must be done before the actual
  574. class is written, because we need the labels defined }
  575. if classtype=ct_class then
  576. begin
  577. methodnametable:=genpublishedmethodstable(aktclass);
  578. fieldtablelabel:=aktclass^.generate_field_table;
  579. { rtti }
  580. if (oo_can_have_published in aktclass^.objectoptions) then
  581. aktclass^.generate_rtti;
  582. { write class name }
  583. getdatalabel(classnamelabel);
  584. datasegment^.concat(new(pai_label,init(classnamelabel)));
  585. datasegment^.concat(new(pai_const,init_8bit(length(aktclass^.objname^))));
  586. datasegment^.concat(new(pai_string,init(aktclass^.objname^)));
  587. { generate message and dynamic tables }
  588. if (oo_has_msgstr in aktclass^.objectoptions) then
  589. strmessagetable:=genstrmsgtab(aktclass);
  590. if (oo_has_msgint in aktclass^.objectoptions) then
  591. intmessagetable:=genintmsgtab(aktclass)
  592. else
  593. datasegment^.concat(new(pai_const,init_32bit(0)));
  594. end;
  595. { write debug info }
  596. {$ifdef GDB}
  597. if (cs_debuginfo in aktmoduleswitches) then
  598. begin
  599. do_count_dbx:=true;
  600. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  601. datasegment^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  602. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  603. end;
  604. {$endif GDB}
  605. datasegment^.concat(new(pai_symbol,initdataname_global(aktclass^.vmt_mangledname,0)));
  606. { determine the size with symtable^.datasize, because }
  607. { size gives back 4 for classes }
  608. datasegment^.concat(new(pai_const,init_32bit(aktclass^.symtable^.datasize)));
  609. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.symtable^.datasize)));
  610. {$ifdef WITHDMT}
  611. if classtype=ct_object then
  612. begin
  613. if assigned(dmtlabel) then
  614. datasegment^.concat(new(pai_const_symbol,init(dmtlabel)))
  615. else
  616. datasegment^.concat(new(pai_const,init_32bit(0)));
  617. end;
  618. {$endif WITHDMT}
  619. { write pointer to parent VMT, this isn't implemented in TP }
  620. { but this is not used in FPC ? (PM) }
  621. { it's not used yet, but the delphi-operators as and is need it (FK) }
  622. { it is not written for parents that don't have any vmt !! }
  623. if assigned(aktclass^.childof) and
  624. (oo_has_vmt in aktclass^.childof^.objectoptions) then
  625. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.childof^.vmt_mangledname)))
  626. else
  627. datasegment^.concat(new(pai_const,init_32bit(0)));
  628. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  629. if classtype=ct_class then
  630. begin
  631. { pointer to class name string }
  632. datasegment^.concat(new(pai_const_symbol,init(classnamelabel)));
  633. { pointer to dynamic table }
  634. if (oo_has_msgint in aktclass^.objectoptions) then
  635. datasegment^.concat(new(pai_const_symbol,init(intmessagetable)))
  636. else
  637. datasegment^.concat(new(pai_const,init_32bit(0)));
  638. { pointer to method table }
  639. if assigned(methodnametable) then
  640. datasegment^.concat(new(pai_const_symbol,init(methodnametable)))
  641. else
  642. datasegment^.concat(new(pai_const,init_32bit(0)));
  643. { pointer to field table }
  644. datasegment^.concat(new(pai_const_symbol,init(fieldtablelabel)));
  645. { pointer to type info of published section }
  646. if (oo_can_have_published in aktclass^.objectoptions) then
  647. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.rtti_name)))
  648. else
  649. datasegment^.concat(new(pai_const,init_32bit(0)));
  650. { inittable for con-/destruction }
  651. {
  652. if aktclass^.needs_inittable then
  653. }
  654. { we generate the init table for classes always, because needs_inittable }
  655. { for classes is always false, it applies only for objects }
  656. datasegment^.concat(new(pai_const_symbol,init(aktclass^.get_inittable_label)));
  657. {
  658. else
  659. datasegment^.concat(new(pai_const,init_32bit(0)));
  660. }
  661. { auto table }
  662. datasegment^.concat(new(pai_const,init_32bit(0)));
  663. { interface table }
  664. datasegment^.concat(new(pai_const,init_32bit(0)));
  665. { table for string messages }
  666. if (oo_has_msgstr in aktclass^.objectoptions) then
  667. datasegment^.concat(new(pai_const_symbol,init(strmessagetable)))
  668. else
  669. datasegment^.concat(new(pai_const,init_32bit(0)));
  670. end;
  671. datasegment^.concatlist(@vmtlist);
  672. vmtlist.done;
  673. { write the size of the VMT }
  674. datasegment^.concat(new(pai_symbol_end,initname(aktclass^.vmt_mangledname)));
  675. end;
  676. function readobjecttype : boolean;
  677. begin
  678. readobjecttype:=true;
  679. { distinguish classes and objects }
  680. case token of
  681. _OBJECT:
  682. begin
  683. classtype:=ct_object;
  684. consume(_OBJECT)
  685. end;
  686. _CPPCLASS:
  687. begin
  688. classtype:=ct_cppclass;
  689. consume(_CPPCLASS);
  690. end;
  691. {$ifdef SUPPORT_INTERFACE}
  692. _INTERFACE:
  693. begin
  694. if aktinterfacetype=it_interfacecom then
  695. objecttype:=ct_interfacecom
  696. else {it_interfaceraw}
  697. objecttype:=ct_interfaceraw;
  698. consume(_INTERFACE);
  699. { forward declaration }
  700. if not(assigned(fd)) and (token=_SEMICOLON) then
  701. begin
  702. { also anonym objects aren't allow (o : object a : longint; end;) }
  703. if n='' then
  704. Message(parser_f_no_anonym_objects);
  705. aktclass:=new(pobjectdef,init(objecttype,n,nil));
  706. if (cs_compilesystem in aktmoduleswitches) and
  707. (objecttype=odt_interfacecom) and (n='IUNKNOWN') then
  708. interface_iunknown:=aktclass;
  709. include(aktclass^.objectoptions,[oo_is_forward]);
  710. end;
  711. end;
  712. {$endif SUPPORT_INTERFACE}
  713. _CLASS:
  714. begin
  715. classtype:=ct_class;
  716. consume(_CLASS);
  717. if not(assigned(fd)) and (token=_OF) then
  718. begin
  719. { a hack, but it's easy to handle }
  720. { class reference type }
  721. consume(_OF);
  722. single_type(tt,hs,typecanbeforward);
  723. { accept hp1, if is a forward def or a class }
  724. if (tt.def^.deftype=forwarddef) or
  725. ((tt.def^.deftype=objectdef) and pobjectdef(tt.def)^.is_class) then
  726. begin
  727. pcrd:=new(pclassrefdef,init(tt.def));
  728. object_dec:=pcrd;
  729. end
  730. else
  731. begin
  732. object_dec:=generrordef;
  733. Message1(type_e_class_type_expected,generrordef^.typename);
  734. end;
  735. typecanbeforward:=storetypecanbeforward;
  736. readobjecttype:=false;
  737. exit;
  738. end
  739. { forward class }
  740. else if not(assigned(fd)) and (token=_SEMICOLON) then
  741. begin
  742. { also anonym objects aren't allow (o : object a : longint; end;) }
  743. if n='' then
  744. Message(parser_f_no_anonym_objects);
  745. aktclass:=new(pobjectdef,init(n,nil));
  746. if (cs_compilesystem in aktmoduleswitches) and (n='TOBJECT') then
  747. class_tobject:=aktclass;
  748. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_class,oo_is_forward];
  749. { all classes must have a vmt !! at offset zero }
  750. if not(oo_has_vmt in aktclass^.objectoptions) then
  751. aktclass^.insertvmt;
  752. object_dec:=aktclass;
  753. typecanbeforward:=storetypecanbeforward;
  754. readobjecttype:=false;
  755. exit;
  756. end;
  757. end;
  758. else
  759. begin
  760. classtype:=ct_class; { this is error but try to recover }
  761. consume(_OBJECT);
  762. end;
  763. end;
  764. end;
  765. procedure readparentclasses;
  766. begin
  767. { reads the parent class }
  768. if token=_LKLAMMER then
  769. begin
  770. consume(_LKLAMMER);
  771. id_type(tt,pattern,false);
  772. childof:=pobjectdef(tt.def);
  773. if (not assigned(childof)) or
  774. (childof^.deftype<>objectdef) then
  775. begin
  776. if assigned(childof) then
  777. Message1(type_e_class_type_expected,childof^.typename);
  778. childof:=nil;
  779. aktclass:=new(pobjectdef,init(n,nil));
  780. end
  781. else
  782. begin
  783. { a mix of class, interfaces, objects and cppclasses
  784. isn't allowed }
  785. case classtype of
  786. ct_class:
  787. if not(childof^.is_class) and
  788. not(childof^.is_interface) then
  789. Message(parser_e_mix_of_classes_and_objects);
  790. ct_interfaceraw,
  791. ct_interfacecom:
  792. if not(childof^.is_interface) then
  793. Message(parser_e_mix_of_classes_and_objects);
  794. ct_cppclass:
  795. if not(childof^.is_cppclass) then
  796. Message(parser_e_mix_of_classes_and_objects);
  797. ct_object:
  798. if not(childof^.is_object) then
  799. Message(parser_e_mix_of_classes_and_objects);
  800. end;
  801. { the forward of the child must be resolved to get
  802. correct field addresses }
  803. if assigned(fd) then
  804. begin
  805. if (oo_is_forward in childof^.objectoptions) then
  806. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  807. aktclass:=fd;
  808. { we must inherit several options !!
  809. this was missing !!
  810. all is now done in set_parent
  811. including symtable datasize setting PM }
  812. fd^.set_parent(childof);
  813. end
  814. else
  815. aktclass:=new(pobjectdef,init(n,childof));
  816. end;
  817. consume(_RKLAMMER);
  818. end
  819. { if no parent class, then a class get tobject as parent }
  820. else if classtype=ct_class then
  821. setclassparent
  822. else
  823. aktclass:=new(pobjectdef,init(n,nil));
  824. end;
  825. procedure chkcpp;
  826. begin
  827. if aktclass^.is_cppclass then
  828. begin
  829. include(aktprocsym^.definition^.proccalloptions,pocall_cppdecl);
  830. aktprocsym^.definition^.setmangledname(
  831. target_os.Cprefix+aktprocsym^.definition^.cplusplusmangledname);
  832. end;
  833. end;
  834. var
  835. temppd : pprocdef;
  836. begin
  837. {Nowadays aktprocsym may already have a value, so we need to save
  838. it.}
  839. oldprocsym:=aktprocsym;
  840. { forward is resolved }
  841. if assigned(fd) then
  842. exclude(fd^.objectoptions,oo_is_forward);
  843. there_is_a_destructor:=false;
  844. actmembertype:=[sp_public];
  845. { objects and class types can't be declared local }
  846. if (symtablestack^.symtabletype<>globalsymtable) and
  847. (symtablestack^.symtabletype<>staticsymtable) then
  848. Message(parser_e_no_local_objects);
  849. storetypecanbeforward:=typecanbeforward;
  850. { for tp mode don't allow forward types }
  851. if (m_tp in aktmodeswitches) and
  852. not (m_delphi in aktmodeswitches) then
  853. typecanbeforward:=false;
  854. if not(readobjecttype) then
  855. exit;
  856. { also anonym objects aren't allow (o : object a : longint; end;) }
  857. if n='' then
  858. Message(parser_f_no_anonym_objects);
  859. readparentclasses;
  860. { default access is public }
  861. actmembertype:=[sp_public];
  862. { set class flags and inherits published, if necessary? }
  863. setclassattributes;
  864. aktobjectdef:=aktclass;
  865. aktclass^.symtable^.next:=symtablestack;
  866. symtablestack:=aktclass^.symtable;
  867. testcurobject:=1;
  868. curobjectname:=Upper(n);
  869. { new procinfo }
  870. oldprocinfo:=procinfo;
  871. new(procinfo,init);
  872. procinfo^._class:=aktclass;
  873. { short class declaration ? }
  874. if (classtype<>ct_class) or (token<>_SEMICOLON) then
  875. begin
  876. { Parse componenten }
  877. repeat
  878. if (sp_private in actmembertype) then
  879. include(aktclass^.objectoptions,oo_has_private);
  880. if (sp_protected in actmembertype) then
  881. include(aktclass^.objectoptions,oo_has_protected);
  882. case token of
  883. _ID : begin
  884. case idtoken of
  885. _PRIVATE : begin
  886. consume(_PRIVATE);
  887. actmembertype:=[sp_private];
  888. current_object_option:=[sp_private];
  889. end;
  890. _PROTECTED : begin
  891. consume(_PROTECTED);
  892. current_object_option:=[sp_protected];
  893. actmembertype:=[sp_protected];
  894. end;
  895. _PUBLIC : begin
  896. consume(_PUBLIC);
  897. current_object_option:=[sp_public];
  898. actmembertype:=[sp_public];
  899. end;
  900. _PUBLISHED : begin
  901. if not(oo_can_have_published in aktclass^.objectoptions) then
  902. Message(parser_e_cant_have_published);
  903. consume(_PUBLISHED);
  904. current_object_option:=[sp_published];
  905. actmembertype:=[sp_published];
  906. end;
  907. else
  908. read_var_decs(false,true,false);
  909. end;
  910. end;
  911. _PROPERTY : property_dec;
  912. _PROCEDURE,
  913. _FUNCTION,
  914. _CLASS : begin
  915. oldparse_only:=parse_only;
  916. parse_only:=true;
  917. parse_proc_dec;
  918. {$ifndef newcg}
  919. parse_object_proc_directives(aktprocsym);
  920. {$endif newcg}
  921. { check if there are duplicates }
  922. check_identical_proc(temppd);
  923. if (po_msgint in aktprocsym^.definition^.procoptions) then
  924. include(aktclass^.objectoptions,oo_has_msgint);
  925. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  926. include(aktclass^.objectoptions,oo_has_msgstr);
  927. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  928. include(aktclass^.objectoptions,oo_has_virtual);
  929. chkcpp;
  930. parse_only:=oldparse_only;
  931. end;
  932. _CONSTRUCTOR : begin
  933. if not(sp_public in actmembertype) then
  934. Message(parser_w_constructor_should_be_public);
  935. oldparse_only:=parse_only;
  936. parse_only:=true;
  937. constructor_head;
  938. {$ifndef newcg}
  939. parse_object_proc_directives(aktprocsym);
  940. {$endif newcg}
  941. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  942. include(aktclass^.objectoptions,oo_has_virtual);
  943. chkcpp;
  944. parse_only:=oldparse_only;
  945. end;
  946. _DESTRUCTOR : begin
  947. if there_is_a_destructor then
  948. Message(parser_n_only_one_destructor);
  949. there_is_a_destructor:=true;
  950. if not(sp_public in actmembertype) then
  951. Message(parser_w_destructor_should_be_public);
  952. oldparse_only:=parse_only;
  953. parse_only:=true;
  954. destructor_head;
  955. {$ifndef newcg}
  956. parse_object_proc_directives(aktprocsym);
  957. {$endif newcg}
  958. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  959. include(aktclass^.objectoptions,oo_has_virtual);
  960. chkcpp;
  961. parse_only:=oldparse_only;
  962. end;
  963. _END : begin
  964. consume(_END);
  965. break;
  966. end;
  967. else
  968. consume(_ID); { Give a ident expected message, like tp7 }
  969. end;
  970. until false;
  971. current_object_option:=[sp_public];
  972. end;
  973. testcurobject:=0;
  974. curobjectname:='';
  975. typecanbeforward:=storetypecanbeforward;
  976. { generate vmt space if needed }
  977. if not(oo_has_vmt in aktclass^.objectoptions) and
  978. ([oo_has_virtual,oo_has_constructor,oo_has_destructor,oo_is_class]*aktclass^.objectoptions<>[]) then
  979. aktclass^.insertvmt;
  980. if (cs_create_smart in aktmoduleswitches) then
  981. datasegment^.concat(new(pai_cut,init));
  982. if (oo_has_vmt in aktclass^.objectoptions) then
  983. writevmt;
  984. { restore old state }
  985. symtablestack:=symtablestack^.next;
  986. aktobjectdef:=nil;
  987. {Restore procinfo}
  988. dispose(procinfo,done);
  989. procinfo:=oldprocinfo;
  990. {Restore the aktprocsym.}
  991. aktprocsym:=oldprocsym;
  992. object_dec:=aktclass;
  993. end;
  994. end.
  995. {
  996. $Log$
  997. Revision 1.4 2000-10-31 22:02:49 peter
  998. * symtable splitted, no real code changes
  999. Revision 1.3 2000/10/26 21:54:03 peter
  1000. * fixed crash with error in child definition (merged)
  1001. Revision 1.2 2000/10/21 18:16:11 florian
  1002. * a lot of changes:
  1003. - basic dyn. array support
  1004. - basic C++ support
  1005. - some work for interfaces done
  1006. ....
  1007. Revision 1.1 2000/10/14 10:14:51 peter
  1008. * moehrendorf oct 2000 rewrite
  1009. }