pdecobj.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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,cclasses,
  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,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 : tobjectdeftype;
  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 is_class(aktclass) 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:=booltype;
  66. end;
  67. end;
  68. end;
  69. procedure property_dec;
  70. var
  71. sym : psym;
  72. propertyparas : tlinkedlist;
  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) or convertable_paras(p^.para,propertyparas,cp_value_equal_const) then {MvdV: Ozerski 14.03.01}
  83. break;
  84. p:=p^.nextoverloaded;
  85. end;
  86. get_procdef:=p;
  87. end;
  88. var
  89. hp2,datacoll : tparaitem;
  90. p : ppropertysym;
  91. overriden : psym;
  92. hs : string;
  93. varspez : tvarspez;
  94. sc : tidstringlist;
  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((is_class_or_interface(aktclass)) or
  104. ((m_delphi in aktmodeswitches) and (is_object(aktclass)))) then
  105. Message(parser_e_syntax_error);
  106. consume(_PROPERTY);
  107. propertyparas:=TParaLinkedList.Create;
  108. datacoll:=nil;
  109. if token=_ID then
  110. begin
  111. p:=new(ppropertysym,init(orgpattern));
  112. propname:=pattern;
  113. consume(_ID);
  114. { property parameters ? }
  115. if token=_LECKKLAMMER then
  116. begin
  117. if (sp_published in current_object_option) then
  118. Message(parser_e_cant_publish_that_property);
  119. { create a list of the parameters in propertyparas }
  120. consume(_LECKKLAMMER);
  121. inc(testcurobject);
  122. repeat
  123. if token=_VAR then
  124. begin
  125. consume(_VAR);
  126. varspez:=vs_var;
  127. end
  128. else if token=_CONST then
  129. begin
  130. consume(_CONST);
  131. varspez:=vs_const;
  132. end
  133. else if token=_OUT then
  134. begin
  135. consume(_OUT);
  136. varspez:=vs_out;
  137. end
  138. else varspez:=vs_value;
  139. sc:=idlist;
  140. {$ifdef fixLeaksOnError}
  141. strContStack.push(sc);
  142. {$endif fixLeaksOnError}
  143. if token=_COLON then
  144. begin
  145. consume(_COLON);
  146. if token=_ARRAY then
  147. begin
  148. {
  149. if (varspez<>vs_const) and
  150. (varspez<>vs_var) then
  151. begin
  152. varspez:=vs_const;
  153. Message(parser_e_illegal_open_parameter);
  154. end;
  155. }
  156. consume(_ARRAY);
  157. consume(_OF);
  158. { define range and type of range }
  159. tt.setdef(new(parraydef,init(0,-1,s32bittype)));
  160. { define field type }
  161. single_type(parraydef(tt.def)^.elementtype,s,false);
  162. end
  163. else
  164. single_type(tt,s,false);
  165. end
  166. else
  167. tt:=cformaltype;
  168. repeat
  169. s:=sc.get(declarepos);
  170. if s='' then
  171. break;
  172. hp2:=TParaItem.create;
  173. hp2.paratyp:=varspez;
  174. hp2.paratype:=tt;
  175. propertyparas.insert(hp2);
  176. until false;
  177. {$ifdef fixLeaksOnError}
  178. if strContStack.pop <> sc then
  179. writeln('problem with strContStack in ptype');
  180. {$endif fixLeaksOnError}
  181. sc.free;
  182. until not try_to_consume(_SEMICOLON);
  183. dec(testcurobject);
  184. consume(_RECKKLAMMER);
  185. end;
  186. { overriden property ? }
  187. { force property interface, if there is a property parameter }
  188. if (token=_COLON) or not(propertyparas.empty) then
  189. begin
  190. consume(_COLON);
  191. single_type(p^.proptype,hs,false);
  192. if (idtoken=_INDEX) then
  193. begin
  194. consume(_INDEX);
  195. pt:=comp_expr(true);
  196. if is_constnode(pt) and
  197. is_ordinal(pt.resulttype.def) and
  198. (not is_64bitint(pt.resulttype.def)) then
  199. p^.index:=tordconstnode(pt).value
  200. else
  201. begin
  202. Message(parser_e_invalid_property_index_value);
  203. p^.index:=0;
  204. end;
  205. p^.indextype.setdef(pt.resulttype.def);
  206. include(p^.propoptions,ppo_indexed);
  207. { concat a longint to the para template }
  208. hp2:=TParaItem.Create;
  209. hp2.paratyp:=vs_value;
  210. hp2.paratype:=p^.indextype;
  211. propertyparas.insert(hp2);
  212. pt.free;
  213. end;
  214. { the parser need to know if a property has parameters }
  215. if not(propertyparas.empty) then
  216. include(p^.propoptions,ppo_hasparameters);
  217. end
  218. else
  219. begin
  220. { do an property override }
  221. overriden:=search_class_member(aktclass,propname);
  222. if assigned(overriden) and (overriden^.typ=propertysym) then
  223. begin
  224. p^.dooverride(ppropertysym(overriden));
  225. end
  226. else
  227. begin
  228. p^.proptype:=generrortype;
  229. message(parser_e_no_property_found_to_override);
  230. end;
  231. end;
  232. if (sp_published in current_object_option) and
  233. not(p^.proptype.def^.is_publishable) then
  234. Message(parser_e_cant_publish_that_property);
  235. { create data defcoll to allow correct parameter checks }
  236. datacoll:=TParaItem.Create;
  237. datacoll.paratyp:=vs_value;
  238. datacoll.paratype:=p^.proptype;
  239. if (idtoken=_READ) then
  240. begin
  241. p^.readaccess^.clear;
  242. consume(_READ);
  243. sym:=search_class_member(aktclass,pattern);
  244. if not(assigned(sym)) then
  245. begin
  246. Message1(sym_e_unknown_id,pattern);
  247. consume(_ID);
  248. end
  249. else
  250. begin
  251. consume(_ID);
  252. while (token=_POINT) and
  253. ((sym^.typ=varsym) and
  254. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  255. begin
  256. p^.readaccess^.addsym(sym);
  257. consume(_POINT);
  258. sym:=searchsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  259. if not assigned(sym) then
  260. Message1(sym_e_illegal_field,pattern);
  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. sym:=searchsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  308. if not assigned(sym) then
  309. Message1(sym_e_illegal_field,pattern);
  310. consume(_ID);
  311. end;
  312. end;
  313. if assigned(sym) then
  314. begin
  315. { search the matching definition }
  316. case sym^.typ of
  317. procsym :
  318. begin
  319. { insert data entry to check access method }
  320. propertyparas.insert(datacoll);
  321. pp:=get_procdef;
  322. { ... and remove it }
  323. propertyparas.remove(datacoll);
  324. if not(assigned(pp)) then
  325. Message(parser_e_ill_property_access_sym);
  326. p^.writeaccess^.setdef(pp);
  327. end;
  328. varsym :
  329. begin
  330. if not(propertyparas.empty) or
  331. not(is_equal(pvarsym(sym)^.vartype.def,p^.proptype.def)) then
  332. Message(parser_e_ill_property_access_sym);
  333. end
  334. else
  335. Message(parser_e_ill_property_access_sym);
  336. end;
  337. p^.writeaccess^.addsym(sym);
  338. end;
  339. end;
  340. include(p^.propoptions,ppo_stored);
  341. if (idtoken=_STORED) then
  342. begin
  343. consume(_STORED);
  344. p^.storedaccess^.clear;
  345. case token of
  346. _ID:
  347. { in the case that idtoken=_DEFAULT }
  348. { we have to do nothing except }
  349. { setting ppo_stored, it's the same }
  350. { as stored true }
  351. if idtoken<>_DEFAULT then
  352. begin
  353. sym:=search_class_member(aktclass,pattern);
  354. if not(assigned(sym)) then
  355. begin
  356. Message1(sym_e_unknown_id,pattern);
  357. consume(_ID);
  358. end
  359. else
  360. begin
  361. consume(_ID);
  362. while (token=_POINT) and
  363. ((sym^.typ=varsym) and
  364. (pvarsym(sym)^.vartype.def^.deftype=recorddef)) do
  365. begin
  366. p^.storedaccess^.addsym(sym);
  367. consume(_POINT);
  368. sym:=searchsymonlyin(precorddef(pvarsym(sym)^.vartype.def)^.symtable,pattern);
  369. if not assigned(sym) then
  370. Message1(sym_e_illegal_field,pattern);
  371. consume(_ID);
  372. end;
  373. end;
  374. if assigned(sym) then
  375. begin
  376. { only non array properties can be stored }
  377. case sym^.typ of
  378. procsym :
  379. begin
  380. pp:=pprocsym(sym)^.definition;
  381. while assigned(pp) do
  382. begin
  383. { the stored function shouldn't have any parameters }
  384. if pp^.Para.empty then
  385. break;
  386. pp:=pp^.nextoverloaded;
  387. end;
  388. { found we a procedure and does it really return a bool? }
  389. if not(assigned(pp)) or
  390. not(is_boolean(pp^.rettype.def)) then
  391. Message(parser_e_ill_property_storage_sym);
  392. p^.storedaccess^.setdef(pp);
  393. end;
  394. varsym :
  395. begin
  396. if not(propertyparas.empty) or
  397. not(is_boolean(pvarsym(sym)^.vartype.def)) then
  398. Message(parser_e_stored_property_must_be_boolean);
  399. end;
  400. else
  401. Message(parser_e_ill_property_storage_sym);
  402. end;
  403. p^.storedaccess^.addsym(sym);
  404. end;
  405. end;
  406. _FALSE:
  407. begin
  408. consume(_FALSE);
  409. exclude(p^.propoptions,ppo_stored);
  410. end;
  411. _TRUE:
  412. consume(_TRUE);
  413. end;
  414. end;
  415. if (idtoken=_DEFAULT) then
  416. begin
  417. consume(_DEFAULT);
  418. if not(is_ordinal(p^.proptype.def) or
  419. is_64bitint(p^.proptype.def) or
  420. ((p^.proptype.def^.deftype=setdef) and
  421. (psetdef(p^.proptype.def)^.settype=smallset))) or
  422. not(propertyparas.empty) then
  423. Message(parser_e_property_cant_have_a_default_value);
  424. { Get the result of the default, the firstpass is
  425. needed to support values like -1 }
  426. pt:=comp_expr(true);
  427. if (p^.proptype.def^.deftype=setdef) and
  428. (pt.nodetype=arrayconstructorn) then
  429. begin
  430. arrayconstructor_to_set(tarrayconstructornode(pt));
  431. do_resulttypepass(pt);
  432. end;
  433. inserttypeconv(pt,p^.proptype);
  434. if not(is_constnode(pt)) then
  435. Message(parser_e_property_default_value_must_const);
  436. if pt.nodetype=setconstn then
  437. p^.default:=plongint(tsetconstnode(pt).value_set)^
  438. else
  439. p^.default:=tordconstnode(pt).value;
  440. pt.free;
  441. end
  442. else if (idtoken=_NODEFAULT) then
  443. begin
  444. consume(_NODEFAULT);
  445. p^.default:=0;
  446. end;
  447. symtablestack^.insert(p);
  448. { default property ? }
  449. consume(_SEMICOLON);
  450. if (idtoken=_DEFAULT) then
  451. begin
  452. consume(_DEFAULT);
  453. { overriding a default propertyp is allowed
  454. p2:=search_default_property(aktclass);
  455. if assigned(p2) then
  456. message1(parser_e_only_one_default_property,
  457. pobjectdef(p2^.owner^.defowner)^.objname^)
  458. else
  459. }
  460. begin
  461. include(p^.propoptions,ppo_defaultproperty);
  462. if propertyparas.empty then
  463. message(parser_e_property_need_paras);
  464. end;
  465. consume(_SEMICOLON);
  466. end;
  467. { clean up }
  468. if assigned(datacoll) then
  469. datacoll.free;
  470. end
  471. else
  472. begin
  473. consume(_ID);
  474. consume(_SEMICOLON);
  475. end;
  476. propertyparas.free;
  477. end;
  478. procedure destructor_head;
  479. begin
  480. consume(_DESTRUCTOR);
  481. inc(lexlevel);
  482. parse_proc_head(potype_destructor);
  483. dec(lexlevel);
  484. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'DONE') then
  485. Message(parser_e_destructorname_must_be_done);
  486. include(aktclass^.objectoptions,oo_has_destructor);
  487. consume(_SEMICOLON);
  488. if not(aktprocsym^.definition^.Para.empty) then
  489. if not (m_tp in aktmodeswitches) then
  490. Message(parser_e_no_paras_for_destructor);
  491. { no return value }
  492. aktprocsym^.definition^.rettype:=voidtype;
  493. end;
  494. var
  495. hs : string;
  496. pcrd : pclassrefdef;
  497. tt : ttype;
  498. oldprocinfo : pprocinfo;
  499. oldprocsym : pprocsym;
  500. oldparse_only : boolean;
  501. methodnametable,intmessagetable,
  502. strmessagetable,classnamelabel,
  503. fieldtablelabel : pasmlabel;
  504. storetypecanbeforward : boolean;
  505. procedure setclassattributes;
  506. begin
  507. if classtype=odt_class then
  508. begin
  509. aktclass^.objecttype:=odt_class;
  510. if (cs_generate_rtti in aktlocalswitches) or
  511. (assigned(aktclass^.childof) and
  512. (oo_can_have_published in aktclass^.childof^.objectoptions)) then
  513. begin
  514. include(aktclass^.objectoptions,oo_can_have_published);
  515. { in "publishable" classes the default access type is published }
  516. actmembertype:=[sp_published];
  517. { don't know if this is necessary (FK) }
  518. current_object_option:=[sp_published];
  519. end;
  520. end;
  521. end;
  522. procedure setclassparent;
  523. begin
  524. if assigned(fd) then
  525. aktclass:=fd
  526. else
  527. aktclass:=new(pobjectdef,init(classtype,n,nil));
  528. { is the current class tobject? }
  529. { so you could define your own tobject }
  530. if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_class) and (upper(n)='TOBJECT') then
  531. class_tobject:=aktclass
  532. else if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  533. interface_iunknown:=aktclass
  534. else
  535. begin
  536. case classtype of
  537. odt_class:
  538. childof:=class_tobject;
  539. odt_interfacecom:
  540. childof:=interface_iunknown;
  541. end;
  542. if (oo_is_forward in childof^.objectoptions) then
  543. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  544. aktclass^.set_parent(childof);
  545. end;
  546. end;
  547. { generates the vmt for classes as well as for objects }
  548. procedure writevmt;
  549. var
  550. vmtlist : taasmoutput;
  551. {$ifdef WITHDMT}
  552. dmtlabel : pasmlabel;
  553. {$endif WITHDMT}
  554. interfacetable : pasmlabel;
  555. begin
  556. {$ifdef WITHDMT}
  557. dmtlabel:=gendmt(aktclass);
  558. {$endif WITHDMT}
  559. { this generates the entries }
  560. vmtlist:=TAasmoutput.Create;
  561. genvmt(vmtlist,aktclass);
  562. { write tables for classes, this must be done before the actual
  563. class is written, because we need the labels defined }
  564. if classtype=odt_class then
  565. begin
  566. methodnametable:=genpublishedmethodstable(aktclass);
  567. fieldtablelabel:=aktclass^.generate_field_table;
  568. { rtti }
  569. if (oo_can_have_published in aktclass^.objectoptions) then
  570. aktclass^.generate_rtti;
  571. { write class name }
  572. getdatalabel(classnamelabel);
  573. dataSegment.concat(Tai_label.Create(classnamelabel));
  574. dataSegment.concat(Tai_const.Create_8bit(length(aktclass^.objname^)));
  575. dataSegment.concat(Tai_string.Create(aktclass^.objname^));
  576. { generate message and dynamic tables }
  577. if (oo_has_msgstr in aktclass^.objectoptions) then
  578. strmessagetable:=genstrmsgtab(aktclass);
  579. if (oo_has_msgint in aktclass^.objectoptions) then
  580. intmessagetable:=genintmsgtab(aktclass)
  581. else
  582. dataSegment.concat(Tai_const.Create_32bit(0));
  583. if aktclass^.implementedinterfaces^.count>0 then
  584. interfacetable:=genintftable(aktclass);
  585. end;
  586. { write debug info }
  587. {$ifdef GDB}
  588. if (cs_debuginfo in aktmoduleswitches) then
  589. begin
  590. do_count_dbx:=true;
  591. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  592. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  593. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname)));
  594. end;
  595. {$endif GDB}
  596. dataSegment.concat(Tai_symbol.Createdataname_global(aktclass^.vmt_mangledname,0));
  597. { determine the size with symtable^.datasize, because }
  598. { size gives back 4 for classes }
  599. dataSegment.concat(Tai_const.Create_32bit(aktclass^.symtable^.datasize));
  600. dataSegment.concat(Tai_const.Create_32bit(-aktclass^.symtable^.datasize));
  601. {$ifdef WITHDMT}
  602. if classtype=ct_object then
  603. begin
  604. if assigned(dmtlabel) then
  605. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  606. else
  607. dataSegment.concat(Tai_const.Create_32bit(0));
  608. end;
  609. {$endif WITHDMT}
  610. { write pointer to parent VMT, this isn't implemented in TP }
  611. { but this is not used in FPC ? (PM) }
  612. { it's not used yet, but the delphi-operators as and is need it (FK) }
  613. { it is not written for parents that don't have any vmt !! }
  614. if assigned(aktclass^.childof) and
  615. (oo_has_vmt in aktclass^.childof^.objectoptions) then
  616. dataSegment.concat(Tai_const_symbol.Createname(aktclass^.childof^.vmt_mangledname))
  617. else
  618. dataSegment.concat(Tai_const.Create_32bit(0));
  619. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  620. if classtype=odt_class then
  621. begin
  622. { pointer to class name string }
  623. dataSegment.concat(Tai_const_symbol.Create(classnamelabel));
  624. { pointer to dynamic table }
  625. if (oo_has_msgint in aktclass^.objectoptions) then
  626. dataSegment.concat(Tai_const_symbol.Create(intmessagetable))
  627. else
  628. dataSegment.concat(Tai_const.Create_32bit(0));
  629. { pointer to method table }
  630. if assigned(methodnametable) then
  631. dataSegment.concat(Tai_const_symbol.Create(methodnametable))
  632. else
  633. dataSegment.concat(Tai_const.Create_32bit(0));
  634. { pointer to field table }
  635. dataSegment.concat(Tai_const_symbol.Create(fieldtablelabel));
  636. { pointer to type info of published section }
  637. if (oo_can_have_published in aktclass^.objectoptions) then
  638. dataSegment.concat(Tai_const_symbol.Createname(aktclass^.rtti_name))
  639. else
  640. dataSegment.concat(Tai_const.Create_32bit(0));
  641. { inittable for con-/destruction }
  642. {
  643. if aktclass^.needs_inittable then
  644. }
  645. { we generate the init table for classes always, because needs_inittable }
  646. { for classes is always false, it applies only for objects }
  647. dataSegment.concat(Tai_const_symbol.Create(aktclass^.get_inittable_label));
  648. {
  649. else
  650. dataSegment.concat(Tai_const.Create_32bit(0));
  651. }
  652. { auto table }
  653. dataSegment.concat(Tai_const.Create_32bit(0));
  654. { interface table }
  655. if aktclass^.implementedinterfaces^.count>0 then
  656. dataSegment.concat(Tai_const_symbol.Create(interfacetable))
  657. else
  658. dataSegment.concat(Tai_const.Create_32bit(0));
  659. { table for string messages }
  660. if (oo_has_msgstr in aktclass^.objectoptions) then
  661. dataSegment.concat(Tai_const_symbol.Create(strmessagetable))
  662. else
  663. dataSegment.concat(Tai_const.Create_32bit(0));
  664. end;
  665. dataSegment.concatlist(vmtlist);
  666. vmtlist.free;
  667. { write the size of the VMT }
  668. dataSegment.concat(Tai_symbol_end.Createname(aktclass^.vmt_mangledname));
  669. end;
  670. procedure setinterfacemethodoptions;
  671. var
  672. i: longint;
  673. defs: pindexarray;
  674. pd: pprocdef;
  675. begin
  676. include(aktclass^.objectoptions,oo_has_virtual);
  677. defs:=aktclass^.symtable^.defindex;
  678. for i:=1 to defs^.count do
  679. begin
  680. pd:=pprocdef(defs^.search(i));
  681. if pd^.deftype=procdef then
  682. begin
  683. pd^.extnumber:=aktclass^.lastvtableindex;
  684. inc(aktclass^.lastvtableindex);
  685. include(pd^.procoptions,po_virtualmethod);
  686. pd^.forwarddef:=false;
  687. end;
  688. end;
  689. end;
  690. function readobjecttype : boolean;
  691. begin
  692. readobjecttype:=true;
  693. { distinguish classes and objects }
  694. case token of
  695. _OBJECT:
  696. begin
  697. classtype:=odt_object;
  698. consume(_OBJECT)
  699. end;
  700. _CPPCLASS:
  701. begin
  702. classtype:=odt_cppclass;
  703. consume(_CPPCLASS);
  704. end;
  705. _INTERFACE:
  706. begin
  707. if aktinterfacetype=it_interfacecom then
  708. classtype:=odt_interfacecom
  709. else {it_interfacecorba}
  710. classtype:=odt_interfacecorba;
  711. consume(_INTERFACE);
  712. { forward declaration }
  713. if not(assigned(fd)) and (token=_SEMICOLON) then
  714. begin
  715. { also anonym objects aren't allow (o : object a : longint; end;) }
  716. if n='' then
  717. Message(parser_f_no_anonym_objects);
  718. aktclass:=new(pobjectdef,init(classtype,n,nil));
  719. if (cs_compilesystem in aktmoduleswitches) and
  720. (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  721. interface_iunknown:=aktclass;
  722. include(aktclass^.objectoptions,oo_is_forward);
  723. object_dec:=aktclass;
  724. typecanbeforward:=storetypecanbeforward;
  725. readobjecttype:=false;
  726. exit;
  727. end;
  728. end;
  729. _CLASS:
  730. begin
  731. classtype:=odt_class;
  732. consume(_CLASS);
  733. if not(assigned(fd)) and (token=_OF) then
  734. begin
  735. { a hack, but it's easy to handle }
  736. { class reference type }
  737. consume(_OF);
  738. single_type(tt,hs,typecanbeforward);
  739. { accept hp1, if is a forward def or a class }
  740. if (tt.def^.deftype=forwarddef) or
  741. is_class(tt.def) then
  742. begin
  743. pcrd:=new(pclassrefdef,init(tt));
  744. object_dec:=pcrd;
  745. end
  746. else
  747. begin
  748. object_dec:=generrortype.def;
  749. Message1(type_e_class_type_expected,generrortype.def^.typename);
  750. end;
  751. typecanbeforward:=storetypecanbeforward;
  752. readobjecttype:=false;
  753. exit;
  754. end
  755. { forward class }
  756. else if not(assigned(fd)) and (token=_SEMICOLON) then
  757. begin
  758. { also anonym objects aren't allow (o : object a : longint; end;) }
  759. if n='' then
  760. Message(parser_f_no_anonym_objects);
  761. aktclass:=new(pobjectdef,init(odt_class,n,nil));
  762. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  763. class_tobject:=aktclass;
  764. aktclass^.objecttype:=odt_class;
  765. include(aktclass^.objectoptions,oo_is_forward);
  766. { all classes must have a vmt !! at offset zero }
  767. if not(oo_has_vmt in aktclass^.objectoptions) then
  768. aktclass^.insertvmt;
  769. object_dec:=aktclass;
  770. typecanbeforward:=storetypecanbeforward;
  771. readobjecttype:=false;
  772. exit;
  773. end;
  774. end;
  775. else
  776. begin
  777. classtype:=odt_class; { this is error but try to recover }
  778. consume(_OBJECT);
  779. end;
  780. end;
  781. end;
  782. procedure readimplementedinterfaces;
  783. var
  784. implintf: pobjectdef;
  785. tt : ttype;
  786. begin
  787. while try_to_consume(_COMMA) do begin
  788. id_type(tt,pattern,false);
  789. implintf:=pobjectdef(tt.def);
  790. if (tt.def^.deftype<>objectdef) then begin
  791. Message1(type_e_interface_type_expected,tt.def^.typename);
  792. Continue; { omit }
  793. end;
  794. if not is_interface(implintf) then begin
  795. Message1(type_e_interface_type_expected,implintf^.typename);
  796. Continue; { omit }
  797. end;
  798. if aktclass^.implementedinterfaces^.searchintf(tt.def)<>-1 then
  799. Message1(sym_e_duplicate_id,tt.def^.name)
  800. else
  801. aktclass^.implementedinterfaces^.addintf(tt.def);
  802. end;
  803. end;
  804. procedure readinterfaceiid;
  805. var
  806. p : tnode;
  807. begin
  808. p:=comp_expr(true);
  809. if p.nodetype=stringconstn then
  810. begin
  811. aktclass^.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  812. p.free;
  813. aktclass^.isiidguidvalid:=string2guid(aktclass^.iidstr^,aktclass^.iidguid);
  814. if (classtype=odt_interfacecom) and not aktclass^.isiidguidvalid then
  815. Message(parser_e_improper_guid_syntax);
  816. end
  817. else
  818. begin
  819. p.free;
  820. Message(cg_e_illegal_expression);
  821. end;
  822. end;
  823. procedure readparentclasses;
  824. begin
  825. { reads the parent class }
  826. if token=_LKLAMMER then
  827. begin
  828. consume(_LKLAMMER);
  829. id_type(tt,pattern,false);
  830. childof:=pobjectdef(tt.def);
  831. if (not assigned(childof)) or
  832. (childof^.deftype<>objectdef) then
  833. begin
  834. if assigned(childof) then
  835. Message1(type_e_class_type_expected,childof^.typename);
  836. childof:=nil;
  837. aktclass:=new(pobjectdef,init(classtype,n,nil));
  838. end
  839. else
  840. begin
  841. { a mix of class, interfaces, objects and cppclasses
  842. isn't allowed }
  843. case classtype of
  844. odt_class:
  845. if not(is_class(childof)) and
  846. not(is_interface(childof)) then
  847. Message(parser_e_mix_of_classes_and_objects);
  848. odt_interfacecorba,
  849. odt_interfacecom:
  850. if not(is_interface(childof)) then
  851. Message(parser_e_mix_of_classes_and_objects);
  852. odt_cppclass:
  853. if not(is_cppclass(childof)) then
  854. Message(parser_e_mix_of_classes_and_objects);
  855. odt_object:
  856. if not(is_object(childof)) then
  857. Message(parser_e_mix_of_classes_and_objects);
  858. end;
  859. { the forward of the child must be resolved to get
  860. correct field addresses }
  861. if assigned(fd) then
  862. begin
  863. if (oo_is_forward in childof^.objectoptions) then
  864. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  865. aktclass:=fd;
  866. { we must inherit several options !!
  867. this was missing !!
  868. all is now done in set_parent
  869. including symtable datasize setting PM }
  870. fd^.set_parent(childof);
  871. end
  872. else
  873. aktclass:=new(pobjectdef,init(classtype,n,childof));
  874. if aktclass^.objecttype=odt_class then
  875. readimplementedinterfaces;
  876. end;
  877. consume(_RKLAMMER);
  878. end
  879. { if no parent class, then a class get tobject as parent }
  880. else if classtype in [odt_class,odt_interfacecom] then
  881. setclassparent
  882. else
  883. aktclass:=new(pobjectdef,init(classtype,n,nil));
  884. { read GUID }
  885. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  886. try_to_consume(_LECKKLAMMER) then
  887. begin
  888. readinterfaceiid;
  889. consume(_RECKKLAMMER);
  890. end;
  891. end;
  892. procedure chkcpp;
  893. begin
  894. if is_cppclass(aktclass) then
  895. begin
  896. include(aktprocsym^.definition^.proccalloptions,pocall_cppdecl);
  897. aktprocsym^.definition^.setmangledname(
  898. target_os.Cprefix+aktprocsym^.definition^.cplusplusmangledname);
  899. end;
  900. end;
  901. var
  902. temppd : pprocdef;
  903. begin
  904. {Nowadays aktprocsym may already have a value, so we need to save
  905. it.}
  906. oldprocsym:=aktprocsym;
  907. { forward is resolved }
  908. if assigned(fd) then
  909. exclude(fd^.objectoptions,oo_is_forward);
  910. there_is_a_destructor:=false;
  911. actmembertype:=[sp_public];
  912. { objects and class types can't be declared local }
  913. if (symtablestack^.symtabletype<>globalsymtable) and
  914. (symtablestack^.symtabletype<>staticsymtable) then
  915. Message(parser_e_no_local_objects);
  916. storetypecanbeforward:=typecanbeforward;
  917. { for tp mode don't allow forward types }
  918. if (m_tp in aktmodeswitches) and
  919. not (m_delphi in aktmodeswitches) then
  920. typecanbeforward:=false;
  921. if not(readobjecttype) then
  922. exit;
  923. { also anonym objects aren't allow (o : object a : longint; end;) }
  924. if n='' then
  925. Message(parser_f_no_anonym_objects);
  926. readparentclasses;
  927. { default access is public }
  928. actmembertype:=[sp_public];
  929. { set class flags and inherits published, if necessary? }
  930. setclassattributes;
  931. aktobjectdef:=aktclass;
  932. aktclass^.symtable^.next:=symtablestack;
  933. symtablestack:=aktclass^.symtable;
  934. testcurobject:=1;
  935. curobjectname:=Upper(n);
  936. { new procinfo }
  937. oldprocinfo:=procinfo;
  938. new(procinfo,init);
  939. procinfo^._class:=aktclass;
  940. { short class declaration ? }
  941. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  942. begin
  943. { Parse componenten }
  944. repeat
  945. if (sp_private in actmembertype) then
  946. include(aktclass^.objectoptions,oo_has_private);
  947. if (sp_protected in actmembertype) then
  948. include(aktclass^.objectoptions,oo_has_protected);
  949. case token of
  950. _ID : begin
  951. case idtoken of
  952. _PRIVATE : begin
  953. if is_interface(aktclass) then
  954. Message(parser_e_no_access_specifier_in_interfaces);
  955. consume(_PRIVATE);
  956. actmembertype:=[sp_private];
  957. current_object_option:=[sp_private];
  958. end;
  959. _PROTECTED : begin
  960. if is_interface(aktclass) then
  961. Message(parser_e_no_access_specifier_in_interfaces);
  962. consume(_PROTECTED);
  963. current_object_option:=[sp_protected];
  964. actmembertype:=[sp_protected];
  965. end;
  966. _PUBLIC : begin
  967. if is_interface(aktclass) then
  968. Message(parser_e_no_access_specifier_in_interfaces);
  969. consume(_PUBLIC);
  970. current_object_option:=[sp_public];
  971. actmembertype:=[sp_public];
  972. end;
  973. _PUBLISHED : begin
  974. if is_interface(aktclass) then
  975. Message(parser_e_no_access_specifier_in_interfaces)
  976. else
  977. if not(oo_can_have_published in aktclass^.objectoptions) then
  978. Message(parser_e_cant_have_published);
  979. consume(_PUBLISHED);
  980. current_object_option:=[sp_published];
  981. actmembertype:=[sp_published];
  982. end;
  983. else
  984. if is_interface(aktclass) then
  985. Message(parser_e_no_vars_in_interfaces);
  986. read_var_decs(false,true,false);
  987. end;
  988. end;
  989. _PROPERTY : property_dec;
  990. _PROCEDURE,
  991. _FUNCTION,
  992. _CLASS : begin
  993. oldparse_only:=parse_only;
  994. parse_only:=true;
  995. parse_proc_dec;
  996. {$ifndef newcg}
  997. parse_object_proc_directives(aktprocsym);
  998. {$endif newcg}
  999. { check if there are duplicates }
  1000. check_identical_proc(temppd);
  1001. if (po_msgint in aktprocsym^.definition^.procoptions) then
  1002. include(aktclass^.objectoptions,oo_has_msgint);
  1003. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  1004. include(aktclass^.objectoptions,oo_has_msgstr);
  1005. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1006. include(aktclass^.objectoptions,oo_has_virtual);
  1007. chkcpp;
  1008. parse_only:=oldparse_only;
  1009. end;
  1010. _CONSTRUCTOR : begin
  1011. if not(sp_public in actmembertype) then
  1012. Message(parser_w_constructor_should_be_public);
  1013. if is_interface(aktclass) then
  1014. Message(parser_e_no_con_des_in_interfaces);
  1015. oldparse_only:=parse_only;
  1016. parse_only:=true;
  1017. constructor_head;
  1018. {$ifndef newcg}
  1019. parse_object_proc_directives(aktprocsym);
  1020. {$endif newcg}
  1021. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1022. include(aktclass^.objectoptions,oo_has_virtual);
  1023. chkcpp;
  1024. parse_only:=oldparse_only;
  1025. end;
  1026. _DESTRUCTOR : begin
  1027. if there_is_a_destructor then
  1028. Message(parser_n_only_one_destructor);
  1029. if is_interface(aktclass) then
  1030. Message(parser_e_no_con_des_in_interfaces);
  1031. there_is_a_destructor:=true;
  1032. if not(sp_public in actmembertype) then
  1033. Message(parser_w_destructor_should_be_public);
  1034. oldparse_only:=parse_only;
  1035. parse_only:=true;
  1036. destructor_head;
  1037. {$ifndef newcg}
  1038. parse_object_proc_directives(aktprocsym);
  1039. {$endif newcg}
  1040. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1041. include(aktclass^.objectoptions,oo_has_virtual);
  1042. chkcpp;
  1043. parse_only:=oldparse_only;
  1044. end;
  1045. _END : begin
  1046. consume(_END);
  1047. break;
  1048. end;
  1049. else
  1050. consume(_ID); { Give a ident expected message, like tp7 }
  1051. end;
  1052. until false;
  1053. current_object_option:=[sp_public];
  1054. end;
  1055. testcurobject:=0;
  1056. curobjectname:='';
  1057. typecanbeforward:=storetypecanbeforward;
  1058. { generate vmt space if needed }
  1059. if not(oo_has_vmt in aktclass^.objectoptions) and
  1060. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass^.objectoptions<>[]) or
  1061. (classtype in [odt_class])
  1062. ) then
  1063. aktclass^.insertvmt;
  1064. if (cs_create_smart in aktmoduleswitches) then
  1065. dataSegment.concat(Tai_cut.Create);
  1066. if is_interface(aktclass) then
  1067. writeinterfaceids(aktclass);
  1068. if (oo_has_vmt in aktclass^.objectoptions) then
  1069. writevmt;
  1070. if is_interface(aktclass) then
  1071. setinterfacemethodoptions;
  1072. { restore old state }
  1073. symtablestack:=symtablestack^.next;
  1074. aktobjectdef:=nil;
  1075. {Restore procinfo}
  1076. dispose(procinfo,done);
  1077. procinfo:=oldprocinfo;
  1078. {Restore the aktprocsym.}
  1079. aktprocsym:=oldprocsym;
  1080. object_dec:=aktclass;
  1081. end;
  1082. end.
  1083. {
  1084. $Log$
  1085. Revision 1.20 2001-04-04 22:43:51 peter
  1086. * remove unnecessary calls to firstpass
  1087. Revision 1.19 2001/04/04 21:30:43 florian
  1088. * applied several fixes to get the DD8 Delphi Unit compiled
  1089. e.g. "forward"-interfaces are working now
  1090. Revision 1.18 2001/04/02 21:20:31 peter
  1091. * resulttype rewrite
  1092. Revision 1.17 2001/03/16 14:56:38 marco
  1093. * Pavel's fixes commited (Peter asked). Cycled to test
  1094. Revision 1.16 2001/03/11 22:58:49 peter
  1095. * getsym redesign, removed the globals srsym,srsymtable
  1096. Revision 1.15 2000/12/25 00:07:27 peter
  1097. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1098. tlinkedlist objects)
  1099. Revision 1.14 2000/11/29 00:30:35 florian
  1100. * unused units removed from uses clause
  1101. * some changes for widestrings
  1102. Revision 1.13 2000/11/17 17:32:58 florian
  1103. * properties can now be used in interfaces
  1104. Revision 1.12 2000/11/17 08:21:07 florian
  1105. *** empty log message ***
  1106. Revision 1.11 2000/11/12 23:24:11 florian
  1107. * interfaces are basically running
  1108. Revision 1.10 2000/11/12 22:17:47 peter
  1109. * some realname updates for messages
  1110. Revision 1.9 2000/11/06 23:05:52 florian
  1111. * more fixes
  1112. Revision 1.8 2000/11/06 20:30:55 peter
  1113. * more fixes to get make cycle working
  1114. Revision 1.7 2000/11/04 18:03:57 florian
  1115. * fixed upper/lower case problem
  1116. Revision 1.6 2000/11/04 17:31:00 florian
  1117. * fixed some problems of previous commit
  1118. Revision 1.5 2000/11/04 14:25:20 florian
  1119. + merged Attila's changes for interfaces, not tested yet
  1120. Revision 1.4 2000/10/31 22:02:49 peter
  1121. * symtable splitted, no real code changes
  1122. Revision 1.3 2000/10/26 21:54:03 peter
  1123. * fixed crash with error in child definition (merged)
  1124. Revision 1.2 2000/10/21 18:16:11 florian
  1125. * a lot of changes:
  1126. - basic dyn. array support
  1127. - basic C++ support
  1128. - some work for interfaces done
  1129. ....
  1130. Revision 1.1 2000/10/14 10:14:51 peter
  1131. * moehrendorf oct 2000 rewrite
  1132. }