pdecobj.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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) then
  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)) then
  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. 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. datacoll.free;
  474. end
  475. else
  476. begin
  477. consume(_ID);
  478. consume(_SEMICOLON);
  479. end;
  480. propertyparas.free;
  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=odt_class then
  512. begin
  513. aktclass^.objecttype:=odt_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. if assigned(fd) then
  529. aktclass:=fd
  530. else
  531. aktclass:=new(pobjectdef,init(classtype,n,nil));
  532. { is the current class tobject? }
  533. { so you could define your own tobject }
  534. if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_class) and (upper(n)='TOBJECT') then
  535. class_tobject:=aktclass
  536. else if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  537. interface_iunknown:=aktclass
  538. else
  539. begin
  540. case classtype of
  541. odt_class:
  542. childof:=class_tobject;
  543. odt_interfacecom:
  544. childof:=interface_iunknown;
  545. end;
  546. if (oo_is_forward in childof^.objectoptions) then
  547. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  548. aktclass^.set_parent(childof);
  549. end;
  550. end;
  551. { generates the vmt for classes as well as for objects }
  552. procedure writevmt;
  553. var
  554. vmtlist : taasmoutput;
  555. {$ifdef WITHDMT}
  556. dmtlabel : pasmlabel;
  557. {$endif WITHDMT}
  558. interfacetable : pasmlabel;
  559. begin
  560. {$ifdef WITHDMT}
  561. dmtlabel:=gendmt(aktclass);
  562. {$endif WITHDMT}
  563. { this generates the entries }
  564. vmtlist:=TAasmoutput.Create;
  565. genvmt(vmtlist,aktclass);
  566. { write tables for classes, this must be done before the actual
  567. class is written, because we need the labels defined }
  568. if classtype=odt_class then
  569. begin
  570. methodnametable:=genpublishedmethodstable(aktclass);
  571. fieldtablelabel:=aktclass^.generate_field_table;
  572. { rtti }
  573. if (oo_can_have_published in aktclass^.objectoptions) then
  574. aktclass^.generate_rtti;
  575. { write class name }
  576. getdatalabel(classnamelabel);
  577. dataSegment.concat(Tai_label.Create(classnamelabel));
  578. dataSegment.concat(Tai_const.Create_8bit(length(aktclass^.objname^)));
  579. dataSegment.concat(Tai_string.Create(aktclass^.objname^));
  580. { generate message and dynamic tables }
  581. if (oo_has_msgstr in aktclass^.objectoptions) then
  582. strmessagetable:=genstrmsgtab(aktclass);
  583. if (oo_has_msgint in aktclass^.objectoptions) then
  584. intmessagetable:=genintmsgtab(aktclass)
  585. else
  586. dataSegment.concat(Tai_const.Create_32bit(0));
  587. if aktclass^.implementedinterfaces^.count>0 then
  588. interfacetable:=genintftable(aktclass);
  589. end;
  590. { write debug info }
  591. {$ifdef GDB}
  592. if (cs_debuginfo in aktmoduleswitches) then
  593. begin
  594. do_count_dbx:=true;
  595. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  596. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  597. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname)));
  598. end;
  599. {$endif GDB}
  600. dataSegment.concat(Tai_symbol.Createdataname_global(aktclass^.vmt_mangledname,0));
  601. { determine the size with symtable^.datasize, because }
  602. { size gives back 4 for classes }
  603. dataSegment.concat(Tai_const.Create_32bit(aktclass^.symtable^.datasize));
  604. dataSegment.concat(Tai_const.Create_32bit(-aktclass^.symtable^.datasize));
  605. {$ifdef WITHDMT}
  606. if classtype=ct_object then
  607. begin
  608. if assigned(dmtlabel) then
  609. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  610. else
  611. dataSegment.concat(Tai_const.Create_32bit(0));
  612. end;
  613. {$endif WITHDMT}
  614. { write pointer to parent VMT, this isn't implemented in TP }
  615. { but this is not used in FPC ? (PM) }
  616. { it's not used yet, but the delphi-operators as and is need it (FK) }
  617. { it is not written for parents that don't have any vmt !! }
  618. if assigned(aktclass^.childof) and
  619. (oo_has_vmt in aktclass^.childof^.objectoptions) then
  620. dataSegment.concat(Tai_const_symbol.Createname(aktclass^.childof^.vmt_mangledname))
  621. else
  622. dataSegment.concat(Tai_const.Create_32bit(0));
  623. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  624. if classtype=odt_class then
  625. begin
  626. { pointer to class name string }
  627. dataSegment.concat(Tai_const_symbol.Create(classnamelabel));
  628. { pointer to dynamic table }
  629. if (oo_has_msgint in aktclass^.objectoptions) then
  630. dataSegment.concat(Tai_const_symbol.Create(intmessagetable))
  631. else
  632. dataSegment.concat(Tai_const.Create_32bit(0));
  633. { pointer to method table }
  634. if assigned(methodnametable) then
  635. dataSegment.concat(Tai_const_symbol.Create(methodnametable))
  636. else
  637. dataSegment.concat(Tai_const.Create_32bit(0));
  638. { pointer to field table }
  639. dataSegment.concat(Tai_const_symbol.Create(fieldtablelabel));
  640. { pointer to type info of published section }
  641. if (oo_can_have_published in aktclass^.objectoptions) then
  642. dataSegment.concat(Tai_const_symbol.Createname(aktclass^.rtti_name))
  643. else
  644. dataSegment.concat(Tai_const.Create_32bit(0));
  645. { inittable for con-/destruction }
  646. {
  647. if aktclass^.needs_inittable then
  648. }
  649. { we generate the init table for classes always, because needs_inittable }
  650. { for classes is always false, it applies only for objects }
  651. dataSegment.concat(Tai_const_symbol.Create(aktclass^.get_inittable_label));
  652. {
  653. else
  654. dataSegment.concat(Tai_const.Create_32bit(0));
  655. }
  656. { auto table }
  657. dataSegment.concat(Tai_const.Create_32bit(0));
  658. { interface table }
  659. if aktclass^.implementedinterfaces^.count>0 then
  660. dataSegment.concat(Tai_const_symbol.Create(interfacetable))
  661. else
  662. dataSegment.concat(Tai_const.Create_32bit(0));
  663. { table for string messages }
  664. if (oo_has_msgstr in aktclass^.objectoptions) then
  665. dataSegment.concat(Tai_const_symbol.Create(strmessagetable))
  666. else
  667. dataSegment.concat(Tai_const.Create_32bit(0));
  668. end;
  669. dataSegment.concatlist(vmtlist);
  670. vmtlist.free;
  671. { write the size of the VMT }
  672. dataSegment.concat(Tai_symbol_end.Createname(aktclass^.vmt_mangledname));
  673. end;
  674. procedure setinterfacemethodoptions;
  675. var
  676. i: longint;
  677. defs: pindexarray;
  678. pd: pprocdef;
  679. begin
  680. include(aktclass^.objectoptions,oo_has_virtual);
  681. defs:=aktclass^.symtable^.defindex;
  682. for i:=1 to defs^.count do
  683. begin
  684. pd:=pprocdef(defs^.search(i));
  685. if pd^.deftype=procdef then
  686. begin
  687. pd^.extnumber:=aktclass^.lastvtableindex;
  688. inc(aktclass^.lastvtableindex);
  689. include(pd^.procoptions,po_virtualmethod);
  690. pd^.forwarddef:=false;
  691. end;
  692. end;
  693. end;
  694. function readobjecttype : boolean;
  695. begin
  696. readobjecttype:=true;
  697. { distinguish classes and objects }
  698. case token of
  699. _OBJECT:
  700. begin
  701. classtype:=odt_object;
  702. consume(_OBJECT)
  703. end;
  704. _CPPCLASS:
  705. begin
  706. classtype:=odt_cppclass;
  707. consume(_CPPCLASS);
  708. end;
  709. _INTERFACE:
  710. begin
  711. if aktinterfacetype=it_interfacecom then
  712. classtype:=odt_interfacecom
  713. else {it_interfacecorba}
  714. classtype:=odt_interfacecorba;
  715. consume(_INTERFACE);
  716. { forward declaration }
  717. if not(assigned(fd)) and (token=_SEMICOLON) then
  718. begin
  719. { also anonym objects aren't allow (o : object a : longint; end;) }
  720. if n='' then
  721. Message(parser_f_no_anonym_objects);
  722. aktclass:=new(pobjectdef,init(classtype,n,nil));
  723. if (cs_compilesystem in aktmoduleswitches) and
  724. (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  725. interface_iunknown:=aktclass;
  726. aktclass^.objectoptions:=aktclass^.objectoptions+[oo_is_forward];
  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.def));
  744. object_dec:=pcrd;
  745. end
  746. else
  747. begin
  748. object_dec:=generrordef;
  749. Message1(type_e_class_type_expected,generrordef^.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. do_firstpass(p);
  810. if p.nodetype=stringconstn then
  811. begin
  812. aktclass^.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  813. p.free;
  814. aktclass^.isiidguidvalid:=string2guid(aktclass^.iidstr^,aktclass^.iidguid);
  815. if (classtype=odt_interfacecom) and not aktclass^.isiidguidvalid then
  816. Message(parser_e_improper_guid_syntax);
  817. end
  818. else
  819. begin
  820. p.free;
  821. Message(cg_e_illegal_expression);
  822. end;
  823. end;
  824. procedure readparentclasses;
  825. begin
  826. { reads the parent class }
  827. if token=_LKLAMMER then
  828. begin
  829. consume(_LKLAMMER);
  830. id_type(tt,pattern,false);
  831. childof:=pobjectdef(tt.def);
  832. if (not assigned(childof)) or
  833. (childof^.deftype<>objectdef) then
  834. begin
  835. if assigned(childof) then
  836. Message1(type_e_class_type_expected,childof^.typename);
  837. childof:=nil;
  838. aktclass:=new(pobjectdef,init(classtype,n,nil));
  839. end
  840. else
  841. begin
  842. { a mix of class, interfaces, objects and cppclasses
  843. isn't allowed }
  844. case classtype of
  845. odt_class:
  846. if not(is_class(childof)) and
  847. not(is_interface(childof)) then
  848. Message(parser_e_mix_of_classes_and_objects);
  849. odt_interfacecorba,
  850. odt_interfacecom:
  851. if not(is_interface(childof)) then
  852. Message(parser_e_mix_of_classes_and_objects);
  853. odt_cppclass:
  854. if not(is_cppclass(childof)) then
  855. Message(parser_e_mix_of_classes_and_objects);
  856. odt_object:
  857. if not(is_object(childof)) then
  858. Message(parser_e_mix_of_classes_and_objects);
  859. end;
  860. { the forward of the child must be resolved to get
  861. correct field addresses }
  862. if assigned(fd) then
  863. begin
  864. if (oo_is_forward in childof^.objectoptions) then
  865. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  866. aktclass:=fd;
  867. { we must inherit several options !!
  868. this was missing !!
  869. all is now done in set_parent
  870. including symtable datasize setting PM }
  871. fd^.set_parent(childof);
  872. end
  873. else
  874. aktclass:=new(pobjectdef,init(classtype,n,childof));
  875. if aktclass^.objecttype=odt_class then
  876. readimplementedinterfaces;
  877. end;
  878. consume(_RKLAMMER);
  879. end
  880. { if no parent class, then a class get tobject as parent }
  881. else if classtype in [odt_class,odt_interfacecom] then
  882. setclassparent
  883. else
  884. aktclass:=new(pobjectdef,init(classtype,n,nil));
  885. { read GUID }
  886. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  887. try_to_consume(_LECKKLAMMER) then
  888. begin
  889. readinterfaceiid;
  890. consume(_RECKKLAMMER);
  891. end;
  892. end;
  893. procedure chkcpp;
  894. begin
  895. if is_cppclass(aktclass) then
  896. begin
  897. include(aktprocsym^.definition^.proccalloptions,pocall_cppdecl);
  898. aktprocsym^.definition^.setmangledname(
  899. target_os.Cprefix+aktprocsym^.definition^.cplusplusmangledname);
  900. end;
  901. end;
  902. var
  903. temppd : pprocdef;
  904. begin
  905. {Nowadays aktprocsym may already have a value, so we need to save
  906. it.}
  907. oldprocsym:=aktprocsym;
  908. { forward is resolved }
  909. if assigned(fd) then
  910. exclude(fd^.objectoptions,oo_is_forward);
  911. there_is_a_destructor:=false;
  912. actmembertype:=[sp_public];
  913. { objects and class types can't be declared local }
  914. if (symtablestack^.symtabletype<>globalsymtable) and
  915. (symtablestack^.symtabletype<>staticsymtable) then
  916. Message(parser_e_no_local_objects);
  917. storetypecanbeforward:=typecanbeforward;
  918. { for tp mode don't allow forward types }
  919. if (m_tp in aktmodeswitches) and
  920. not (m_delphi in aktmodeswitches) then
  921. typecanbeforward:=false;
  922. if not(readobjecttype) then
  923. exit;
  924. { also anonym objects aren't allow (o : object a : longint; end;) }
  925. if n='' then
  926. Message(parser_f_no_anonym_objects);
  927. readparentclasses;
  928. { default access is public }
  929. actmembertype:=[sp_public];
  930. { set class flags and inherits published, if necessary? }
  931. setclassattributes;
  932. aktobjectdef:=aktclass;
  933. aktclass^.symtable^.next:=symtablestack;
  934. symtablestack:=aktclass^.symtable;
  935. testcurobject:=1;
  936. curobjectname:=Upper(n);
  937. { new procinfo }
  938. oldprocinfo:=procinfo;
  939. new(procinfo,init);
  940. procinfo^._class:=aktclass;
  941. { short class declaration ? }
  942. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  943. begin
  944. { Parse componenten }
  945. repeat
  946. if (sp_private in actmembertype) then
  947. include(aktclass^.objectoptions,oo_has_private);
  948. if (sp_protected in actmembertype) then
  949. include(aktclass^.objectoptions,oo_has_protected);
  950. case token of
  951. _ID : begin
  952. case idtoken of
  953. _PRIVATE : begin
  954. if is_interface(aktclass) then
  955. Message(parser_e_no_access_specifier_in_interfaces);
  956. consume(_PRIVATE);
  957. actmembertype:=[sp_private];
  958. current_object_option:=[sp_private];
  959. end;
  960. _PROTECTED : begin
  961. if is_interface(aktclass) then
  962. Message(parser_e_no_access_specifier_in_interfaces);
  963. consume(_PROTECTED);
  964. current_object_option:=[sp_protected];
  965. actmembertype:=[sp_protected];
  966. end;
  967. _PUBLIC : begin
  968. if is_interface(aktclass) then
  969. Message(parser_e_no_access_specifier_in_interfaces);
  970. consume(_PUBLIC);
  971. current_object_option:=[sp_public];
  972. actmembertype:=[sp_public];
  973. end;
  974. _PUBLISHED : begin
  975. if is_interface(aktclass) then
  976. Message(parser_e_no_access_specifier_in_interfaces)
  977. else
  978. if not(oo_can_have_published in aktclass^.objectoptions) then
  979. Message(parser_e_cant_have_published);
  980. consume(_PUBLISHED);
  981. current_object_option:=[sp_published];
  982. actmembertype:=[sp_published];
  983. end;
  984. else
  985. if is_interface(aktclass) then
  986. Message(parser_e_no_vars_in_interfaces);
  987. read_var_decs(false,true,false);
  988. end;
  989. end;
  990. _PROPERTY : property_dec;
  991. _PROCEDURE,
  992. _FUNCTION,
  993. _CLASS : begin
  994. oldparse_only:=parse_only;
  995. parse_only:=true;
  996. parse_proc_dec;
  997. {$ifndef newcg}
  998. parse_object_proc_directives(aktprocsym);
  999. {$endif newcg}
  1000. { check if there are duplicates }
  1001. check_identical_proc(temppd);
  1002. if (po_msgint in aktprocsym^.definition^.procoptions) then
  1003. include(aktclass^.objectoptions,oo_has_msgint);
  1004. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  1005. include(aktclass^.objectoptions,oo_has_msgstr);
  1006. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1007. include(aktclass^.objectoptions,oo_has_virtual);
  1008. chkcpp;
  1009. parse_only:=oldparse_only;
  1010. end;
  1011. _CONSTRUCTOR : begin
  1012. if not(sp_public in actmembertype) then
  1013. Message(parser_w_constructor_should_be_public);
  1014. if is_interface(aktclass) then
  1015. Message(parser_e_no_con_des_in_interfaces);
  1016. oldparse_only:=parse_only;
  1017. parse_only:=true;
  1018. constructor_head;
  1019. {$ifndef newcg}
  1020. parse_object_proc_directives(aktprocsym);
  1021. {$endif newcg}
  1022. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1023. include(aktclass^.objectoptions,oo_has_virtual);
  1024. chkcpp;
  1025. parse_only:=oldparse_only;
  1026. end;
  1027. _DESTRUCTOR : begin
  1028. if there_is_a_destructor then
  1029. Message(parser_n_only_one_destructor);
  1030. if is_interface(aktclass) then
  1031. Message(parser_e_no_con_des_in_interfaces);
  1032. there_is_a_destructor:=true;
  1033. if not(sp_public in actmembertype) then
  1034. Message(parser_w_destructor_should_be_public);
  1035. oldparse_only:=parse_only;
  1036. parse_only:=true;
  1037. destructor_head;
  1038. {$ifndef newcg}
  1039. parse_object_proc_directives(aktprocsym);
  1040. {$endif newcg}
  1041. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1042. include(aktclass^.objectoptions,oo_has_virtual);
  1043. chkcpp;
  1044. parse_only:=oldparse_only;
  1045. end;
  1046. _END : begin
  1047. consume(_END);
  1048. break;
  1049. end;
  1050. else
  1051. consume(_ID); { Give a ident expected message, like tp7 }
  1052. end;
  1053. until false;
  1054. current_object_option:=[sp_public];
  1055. end;
  1056. testcurobject:=0;
  1057. curobjectname:='';
  1058. typecanbeforward:=storetypecanbeforward;
  1059. { generate vmt space if needed }
  1060. if not(oo_has_vmt in aktclass^.objectoptions) and
  1061. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass^.objectoptions<>[]) or
  1062. (classtype in [odt_class])
  1063. ) then
  1064. aktclass^.insertvmt;
  1065. if (cs_create_smart in aktmoduleswitches) then
  1066. dataSegment.concat(Tai_cut.Create);
  1067. if is_interface(aktclass) then
  1068. writeinterfaceids(aktclass);
  1069. if (oo_has_vmt in aktclass^.objectoptions) then
  1070. writevmt;
  1071. if is_interface(aktclass) then
  1072. setinterfacemethodoptions;
  1073. { restore old state }
  1074. symtablestack:=symtablestack^.next;
  1075. aktobjectdef:=nil;
  1076. {Restore procinfo}
  1077. dispose(procinfo,done);
  1078. procinfo:=oldprocinfo;
  1079. {Restore the aktprocsym.}
  1080. aktprocsym:=oldprocsym;
  1081. object_dec:=aktclass;
  1082. end;
  1083. end.
  1084. {
  1085. $Log$
  1086. Revision 1.15 2000-12-25 00:07:27 peter
  1087. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1088. tlinkedlist objects)
  1089. Revision 1.14 2000/11/29 00:30:35 florian
  1090. * unused units removed from uses clause
  1091. * some changes for widestrings
  1092. Revision 1.13 2000/11/17 17:32:58 florian
  1093. * properties can now be used in interfaces
  1094. Revision 1.12 2000/11/17 08:21:07 florian
  1095. *** empty log message ***
  1096. Revision 1.11 2000/11/12 23:24:11 florian
  1097. * interfaces are basically running
  1098. Revision 1.10 2000/11/12 22:17:47 peter
  1099. * some realname updates for messages
  1100. Revision 1.9 2000/11/06 23:05:52 florian
  1101. * more fixes
  1102. Revision 1.8 2000/11/06 20:30:55 peter
  1103. * more fixes to get make cycle working
  1104. Revision 1.7 2000/11/04 18:03:57 florian
  1105. * fixed upper/lower case problem
  1106. Revision 1.6 2000/11/04 17:31:00 florian
  1107. * fixed some problems of previous commit
  1108. Revision 1.5 2000/11/04 14:25:20 florian
  1109. + merged Attila's changes for interfaces, not tested yet
  1110. Revision 1.4 2000/10/31 22:02:49 peter
  1111. * symtable splitted, no real code changes
  1112. Revision 1.3 2000/10/26 21:54:03 peter
  1113. * fixed crash with error in child definition (merged)
  1114. Revision 1.2 2000/10/21 18:16:11 florian
  1115. * a lot of changes:
  1116. - basic dyn. array support
  1117. - basic C++ support
  1118. - some work for interfaces done
  1119. ....
  1120. Revision 1.1 2000/10/14 10:14:51 peter
  1121. * moehrendorf oct 2000 rewrite
  1122. }