pdecobj.pas 50 KB

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