pdecobj.pas 44 KB

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