pdecobj.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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 ((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,s32bittype)));
  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:=cformaltype;
  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.def) and
  197. (not is_64bitint(pt.resulttype.def)) 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.def);
  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:=generrortype;
  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_boolean(pp^.rettype.def)) 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_boolean(pvarsym(sym)^.vartype.def)) 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:=ctypeconvnode.create(pt,p^.proptype);
  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:=voidtype;
  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. include(aktclass^.objectoptions,oo_is_forward);
  724. object_dec:=aktclass;
  725. typecanbeforward:=storetypecanbeforward;
  726. readobjecttype:=false;
  727. exit;
  728. end;
  729. end;
  730. _CLASS:
  731. begin
  732. classtype:=odt_class;
  733. consume(_CLASS);
  734. if not(assigned(fd)) and (token=_OF) then
  735. begin
  736. { a hack, but it's easy to handle }
  737. { class reference type }
  738. consume(_OF);
  739. single_type(tt,hs,typecanbeforward);
  740. { accept hp1, if is a forward def or a class }
  741. if (tt.def^.deftype=forwarddef) or
  742. is_class(tt.def) then
  743. begin
  744. pcrd:=new(pclassrefdef,init(tt));
  745. object_dec:=pcrd;
  746. end
  747. else
  748. begin
  749. object_dec:=generrortype.def;
  750. Message1(type_e_class_type_expected,generrortype.def^.typename);
  751. end;
  752. typecanbeforward:=storetypecanbeforward;
  753. readobjecttype:=false;
  754. exit;
  755. end
  756. { forward class }
  757. else if not(assigned(fd)) and (token=_SEMICOLON) then
  758. begin
  759. { also anonym objects aren't allow (o : object a : longint; end;) }
  760. if n='' then
  761. Message(parser_f_no_anonym_objects);
  762. aktclass:=new(pobjectdef,init(odt_class,n,nil));
  763. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  764. class_tobject:=aktclass;
  765. aktclass^.objecttype:=odt_class;
  766. include(aktclass^.objectoptions,oo_is_forward);
  767. { all classes must have a vmt !! at offset zero }
  768. if not(oo_has_vmt in aktclass^.objectoptions) then
  769. aktclass^.insertvmt;
  770. object_dec:=aktclass;
  771. typecanbeforward:=storetypecanbeforward;
  772. readobjecttype:=false;
  773. exit;
  774. end;
  775. end;
  776. else
  777. begin
  778. classtype:=odt_class; { this is error but try to recover }
  779. consume(_OBJECT);
  780. end;
  781. end;
  782. end;
  783. procedure readimplementedinterfaces;
  784. var
  785. implintf: pobjectdef;
  786. tt : ttype;
  787. begin
  788. while try_to_consume(_COMMA) do begin
  789. id_type(tt,pattern,false);
  790. implintf:=pobjectdef(tt.def);
  791. if (tt.def^.deftype<>objectdef) then begin
  792. Message1(type_e_interface_type_expected,tt.def^.typename);
  793. Continue; { omit }
  794. end;
  795. if not is_interface(implintf) then begin
  796. Message1(type_e_interface_type_expected,implintf^.typename);
  797. Continue; { omit }
  798. end;
  799. if aktclass^.implementedinterfaces^.searchintf(tt.def)<>-1 then
  800. Message1(sym_e_duplicate_id,tt.def^.name)
  801. else
  802. aktclass^.implementedinterfaces^.addintf(tt.def);
  803. end;
  804. end;
  805. procedure readinterfaceiid;
  806. var
  807. p : tnode;
  808. begin
  809. p:=comp_expr(true);
  810. do_firstpass(p);
  811. if p.nodetype=stringconstn then
  812. begin
  813. aktclass^.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  814. p.free;
  815. aktclass^.isiidguidvalid:=string2guid(aktclass^.iidstr^,aktclass^.iidguid);
  816. if (classtype=odt_interfacecom) and not aktclass^.isiidguidvalid then
  817. Message(parser_e_improper_guid_syntax);
  818. end
  819. else
  820. begin
  821. p.free;
  822. Message(cg_e_illegal_expression);
  823. end;
  824. end;
  825. procedure readparentclasses;
  826. begin
  827. { reads the parent class }
  828. if token=_LKLAMMER then
  829. begin
  830. consume(_LKLAMMER);
  831. id_type(tt,pattern,false);
  832. childof:=pobjectdef(tt.def);
  833. if (not assigned(childof)) or
  834. (childof^.deftype<>objectdef) then
  835. begin
  836. if assigned(childof) then
  837. Message1(type_e_class_type_expected,childof^.typename);
  838. childof:=nil;
  839. aktclass:=new(pobjectdef,init(classtype,n,nil));
  840. end
  841. else
  842. begin
  843. { a mix of class, interfaces, objects and cppclasses
  844. isn't allowed }
  845. case classtype of
  846. odt_class:
  847. if not(is_class(childof)) and
  848. not(is_interface(childof)) then
  849. Message(parser_e_mix_of_classes_and_objects);
  850. odt_interfacecorba,
  851. odt_interfacecom:
  852. if not(is_interface(childof)) then
  853. Message(parser_e_mix_of_classes_and_objects);
  854. odt_cppclass:
  855. if not(is_cppclass(childof)) then
  856. Message(parser_e_mix_of_classes_and_objects);
  857. odt_object:
  858. if not(is_object(childof)) then
  859. Message(parser_e_mix_of_classes_and_objects);
  860. end;
  861. { the forward of the child must be resolved to get
  862. correct field addresses }
  863. if assigned(fd) then
  864. begin
  865. if (oo_is_forward in childof^.objectoptions) then
  866. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  867. aktclass:=fd;
  868. { we must inherit several options !!
  869. this was missing !!
  870. all is now done in set_parent
  871. including symtable datasize setting PM }
  872. fd^.set_parent(childof);
  873. end
  874. else
  875. aktclass:=new(pobjectdef,init(classtype,n,childof));
  876. if aktclass^.objecttype=odt_class then
  877. readimplementedinterfaces;
  878. end;
  879. consume(_RKLAMMER);
  880. end
  881. { if no parent class, then a class get tobject as parent }
  882. else if classtype in [odt_class,odt_interfacecom] then
  883. setclassparent
  884. else
  885. aktclass:=new(pobjectdef,init(classtype,n,nil));
  886. { read GUID }
  887. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  888. try_to_consume(_LECKKLAMMER) then
  889. begin
  890. readinterfaceiid;
  891. consume(_RECKKLAMMER);
  892. end;
  893. end;
  894. procedure chkcpp;
  895. begin
  896. if is_cppclass(aktclass) then
  897. begin
  898. include(aktprocsym^.definition^.proccalloptions,pocall_cppdecl);
  899. aktprocsym^.definition^.setmangledname(
  900. target_os.Cprefix+aktprocsym^.definition^.cplusplusmangledname);
  901. end;
  902. end;
  903. var
  904. temppd : pprocdef;
  905. begin
  906. {Nowadays aktprocsym may already have a value, so we need to save
  907. it.}
  908. oldprocsym:=aktprocsym;
  909. { forward is resolved }
  910. if assigned(fd) then
  911. exclude(fd^.objectoptions,oo_is_forward);
  912. there_is_a_destructor:=false;
  913. actmembertype:=[sp_public];
  914. { objects and class types can't be declared local }
  915. if (symtablestack^.symtabletype<>globalsymtable) and
  916. (symtablestack^.symtabletype<>staticsymtable) then
  917. Message(parser_e_no_local_objects);
  918. storetypecanbeforward:=typecanbeforward;
  919. { for tp mode don't allow forward types }
  920. if (m_tp in aktmodeswitches) and
  921. not (m_delphi in aktmodeswitches) then
  922. typecanbeforward:=false;
  923. if not(readobjecttype) then
  924. exit;
  925. { also anonym objects aren't allow (o : object a : longint; end;) }
  926. if n='' then
  927. Message(parser_f_no_anonym_objects);
  928. readparentclasses;
  929. { default access is public }
  930. actmembertype:=[sp_public];
  931. { set class flags and inherits published, if necessary? }
  932. setclassattributes;
  933. aktobjectdef:=aktclass;
  934. aktclass^.symtable^.next:=symtablestack;
  935. symtablestack:=aktclass^.symtable;
  936. testcurobject:=1;
  937. curobjectname:=Upper(n);
  938. { new procinfo }
  939. oldprocinfo:=procinfo;
  940. new(procinfo,init);
  941. procinfo^._class:=aktclass;
  942. { short class declaration ? }
  943. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  944. begin
  945. { Parse componenten }
  946. repeat
  947. if (sp_private in actmembertype) then
  948. include(aktclass^.objectoptions,oo_has_private);
  949. if (sp_protected in actmembertype) then
  950. include(aktclass^.objectoptions,oo_has_protected);
  951. case token of
  952. _ID : begin
  953. case idtoken of
  954. _PRIVATE : begin
  955. if is_interface(aktclass) then
  956. Message(parser_e_no_access_specifier_in_interfaces);
  957. consume(_PRIVATE);
  958. actmembertype:=[sp_private];
  959. current_object_option:=[sp_private];
  960. end;
  961. _PROTECTED : begin
  962. if is_interface(aktclass) then
  963. Message(parser_e_no_access_specifier_in_interfaces);
  964. consume(_PROTECTED);
  965. current_object_option:=[sp_protected];
  966. actmembertype:=[sp_protected];
  967. end;
  968. _PUBLIC : begin
  969. if is_interface(aktclass) then
  970. Message(parser_e_no_access_specifier_in_interfaces);
  971. consume(_PUBLIC);
  972. current_object_option:=[sp_public];
  973. actmembertype:=[sp_public];
  974. end;
  975. _PUBLISHED : begin
  976. if is_interface(aktclass) then
  977. Message(parser_e_no_access_specifier_in_interfaces)
  978. else
  979. if not(oo_can_have_published in aktclass^.objectoptions) then
  980. Message(parser_e_cant_have_published);
  981. consume(_PUBLISHED);
  982. current_object_option:=[sp_published];
  983. actmembertype:=[sp_published];
  984. end;
  985. else
  986. if is_interface(aktclass) then
  987. Message(parser_e_no_vars_in_interfaces);
  988. read_var_decs(false,true,false);
  989. end;
  990. end;
  991. _PROPERTY : property_dec;
  992. _PROCEDURE,
  993. _FUNCTION,
  994. _CLASS : begin
  995. oldparse_only:=parse_only;
  996. parse_only:=true;
  997. parse_proc_dec;
  998. {$ifndef newcg}
  999. parse_object_proc_directives(aktprocsym);
  1000. {$endif newcg}
  1001. { check if there are duplicates }
  1002. check_identical_proc(temppd);
  1003. if (po_msgint in aktprocsym^.definition^.procoptions) then
  1004. include(aktclass^.objectoptions,oo_has_msgint);
  1005. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  1006. include(aktclass^.objectoptions,oo_has_msgstr);
  1007. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1008. include(aktclass^.objectoptions,oo_has_virtual);
  1009. chkcpp;
  1010. parse_only:=oldparse_only;
  1011. end;
  1012. _CONSTRUCTOR : begin
  1013. if not(sp_public in actmembertype) then
  1014. Message(parser_w_constructor_should_be_public);
  1015. if is_interface(aktclass) then
  1016. Message(parser_e_no_con_des_in_interfaces);
  1017. oldparse_only:=parse_only;
  1018. parse_only:=true;
  1019. constructor_head;
  1020. {$ifndef newcg}
  1021. parse_object_proc_directives(aktprocsym);
  1022. {$endif newcg}
  1023. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1024. include(aktclass^.objectoptions,oo_has_virtual);
  1025. chkcpp;
  1026. parse_only:=oldparse_only;
  1027. end;
  1028. _DESTRUCTOR : begin
  1029. if there_is_a_destructor then
  1030. Message(parser_n_only_one_destructor);
  1031. if is_interface(aktclass) then
  1032. Message(parser_e_no_con_des_in_interfaces);
  1033. there_is_a_destructor:=true;
  1034. if not(sp_public in actmembertype) then
  1035. Message(parser_w_destructor_should_be_public);
  1036. oldparse_only:=parse_only;
  1037. parse_only:=true;
  1038. destructor_head;
  1039. {$ifndef newcg}
  1040. parse_object_proc_directives(aktprocsym);
  1041. {$endif newcg}
  1042. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1043. include(aktclass^.objectoptions,oo_has_virtual);
  1044. chkcpp;
  1045. parse_only:=oldparse_only;
  1046. end;
  1047. _END : begin
  1048. consume(_END);
  1049. break;
  1050. end;
  1051. else
  1052. consume(_ID); { Give a ident expected message, like tp7 }
  1053. end;
  1054. until false;
  1055. current_object_option:=[sp_public];
  1056. end;
  1057. testcurobject:=0;
  1058. curobjectname:='';
  1059. typecanbeforward:=storetypecanbeforward;
  1060. { generate vmt space if needed }
  1061. if not(oo_has_vmt in aktclass^.objectoptions) and
  1062. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass^.objectoptions<>[]) or
  1063. (classtype in [odt_class])
  1064. ) then
  1065. aktclass^.insertvmt;
  1066. if (cs_create_smart in aktmoduleswitches) then
  1067. dataSegment.concat(Tai_cut.Create);
  1068. if is_interface(aktclass) then
  1069. writeinterfaceids(aktclass);
  1070. if (oo_has_vmt in aktclass^.objectoptions) then
  1071. writevmt;
  1072. if is_interface(aktclass) then
  1073. setinterfacemethodoptions;
  1074. { restore old state }
  1075. symtablestack:=symtablestack^.next;
  1076. aktobjectdef:=nil;
  1077. {Restore procinfo}
  1078. dispose(procinfo,done);
  1079. procinfo:=oldprocinfo;
  1080. {Restore the aktprocsym.}
  1081. aktprocsym:=oldprocsym;
  1082. object_dec:=aktclass;
  1083. end;
  1084. end.
  1085. {
  1086. $Log$
  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. }