pdecobj.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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,
  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,pdecl,psub,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 : plinkedlist;
  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 : pparaitem;
  90. p : ppropertysym;
  91. overriden : psym;
  92. hs : string;
  93. varspez : tvarspez;
  94. sc : pstringcontainer;
  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(aktclass)) then
  104. Message(parser_e_syntax_error);
  105. consume(_PROPERTY);
  106. new(propertyparas,init);
  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_with_tokeninfo(declarepos);
  169. if s='' then
  170. break;
  171. new(hp2,init);
  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. dispose(sc,done);
  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. new(hp2,init);
  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. new(datacoll,init);
  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. dispose(datacoll,done);
  474. end
  475. else
  476. begin
  477. consume(_ID);
  478. consume(_SEMICOLON);
  479. end;
  480. dispose(propertyparas,done);
  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.init;
  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(new(pai_label,init(classnamelabel)));
  578. datasegment^.concat(new(pai_const,init_8bit(length(aktclass^.objname^))));
  579. datasegment^.concat(new(pai_string,init(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(new(pai_const,init_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(new(pai_stabs,init(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(new(pai_symbol,initdataname_global(aktclass^.vmt_mangledname,0)));
  601. { determine the size with symtable^.datasize, because }
  602. { size gives back 4 for classes }
  603. datasegment^.concat(new(pai_const,init_32bit(aktclass^.symtable^.datasize)));
  604. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.symtable^.datasize)));
  605. {$ifdef WITHDMT}
  606. if classtype=ct_object then
  607. begin
  608. if assigned(dmtlabel) then
  609. datasegment^.concat(new(pai_const_symbol,init(dmtlabel)))
  610. else
  611. datasegment^.concat(new(pai_const,init_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(new(pai_const_symbol,initname(aktclass^.childof^.vmt_mangledname)))
  621. else
  622. datasegment^.concat(new(pai_const,init_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(new(pai_const_symbol,init(classnamelabel)));
  628. { pointer to dynamic table }
  629. if (oo_has_msgint in aktclass^.objectoptions) then
  630. datasegment^.concat(new(pai_const_symbol,init(intmessagetable)))
  631. else
  632. datasegment^.concat(new(pai_const,init_32bit(0)));
  633. { pointer to method table }
  634. if assigned(methodnametable) then
  635. datasegment^.concat(new(pai_const_symbol,init(methodnametable)))
  636. else
  637. datasegment^.concat(new(pai_const,init_32bit(0)));
  638. { pointer to field table }
  639. datasegment^.concat(new(pai_const_symbol,init(fieldtablelabel)));
  640. { pointer to type info of published section }
  641. if (oo_can_have_published in aktclass^.objectoptions) then
  642. datasegment^.concat(new(pai_const_symbol,initname(aktclass^.rtti_name)))
  643. else
  644. datasegment^.concat(new(pai_const,init_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(new(pai_const_symbol,init(aktclass^.get_inittable_label)));
  652. {
  653. else
  654. datasegment^.concat(new(pai_const,init_32bit(0)));
  655. }
  656. { auto table }
  657. datasegment^.concat(new(pai_const,init_32bit(0)));
  658. { interface table }
  659. if aktclass^.implementedinterfaces^.count>0 then
  660. datasegment^.concat(new(pai_const_symbol,init(interfacetable)))
  661. else
  662. datasegment^.concat(new(pai_const,init_32bit(0)));
  663. { table for string messages }
  664. if (oo_has_msgstr in aktclass^.objectoptions) then
  665. datasegment^.concat(new(pai_const_symbol,init(strmessagetable)))
  666. else
  667. datasegment^.concat(new(pai_const,init_32bit(0)));
  668. end;
  669. datasegment^.concatlist(@vmtlist);
  670. vmtlist.done;
  671. { write the size of the VMT }
  672. datasegment^.concat(new(pai_symbol_end,initname(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. aktclass^.lastvtableindex:=aktclass^.lastvtableindex+1;
  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. tt: ttype;
  807. p : tnode;
  808. isiidguidvalid: boolean;
  809. begin
  810. p:=comp_expr(true);
  811. do_firstpass(p);
  812. if p.nodetype=stringconstn then
  813. begin
  814. aktclass^.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  815. p.free;
  816. aktclass^.isiidguidvalid:=string2guid(aktclass^.iidstr^,aktclass^.iidguid);
  817. if (classtype=odt_interfacecom) and not aktclass^.isiidguidvalid then
  818. Message(parser_e_improper_guid_syntax);
  819. end
  820. else
  821. begin
  822. p.free;
  823. Message(cg_e_illegal_expression);
  824. end;
  825. end;
  826. procedure readparentclasses;
  827. begin
  828. { reads the parent class }
  829. if token=_LKLAMMER then
  830. begin
  831. consume(_LKLAMMER);
  832. id_type(tt,pattern,false);
  833. childof:=pobjectdef(tt.def);
  834. if (not assigned(childof)) or
  835. (childof^.deftype<>objectdef) then
  836. begin
  837. if assigned(childof) then
  838. Message1(type_e_class_type_expected,childof^.typename);
  839. childof:=nil;
  840. aktclass:=new(pobjectdef,init(classtype,n,nil));
  841. end
  842. else
  843. begin
  844. { a mix of class, interfaces, objects and cppclasses
  845. isn't allowed }
  846. case classtype of
  847. odt_class:
  848. if not(is_class(childof)) and
  849. not(is_interface(childof)) then
  850. Message(parser_e_mix_of_classes_and_objects);
  851. odt_interfacecorba,
  852. odt_interfacecom:
  853. if not(is_interface(childof)) then
  854. Message(parser_e_mix_of_classes_and_objects);
  855. odt_cppclass:
  856. if not(is_cppclass(childof)) then
  857. Message(parser_e_mix_of_classes_and_objects);
  858. odt_object:
  859. if not(is_object(childof)) then
  860. Message(parser_e_mix_of_classes_and_objects);
  861. end;
  862. { the forward of the child must be resolved to get
  863. correct field addresses }
  864. if assigned(fd) then
  865. begin
  866. if (oo_is_forward in childof^.objectoptions) then
  867. Message1(parser_e_forward_declaration_must_be_resolved,childof^.objname^);
  868. aktclass:=fd;
  869. { we must inherit several options !!
  870. this was missing !!
  871. all is now done in set_parent
  872. including symtable datasize setting PM }
  873. fd^.set_parent(childof);
  874. end
  875. else
  876. aktclass:=new(pobjectdef,init(classtype,n,childof));
  877. if aktclass^.objecttype=odt_class then
  878. readimplementedinterfaces;
  879. end;
  880. consume(_RKLAMMER);
  881. end
  882. { if no parent class, then a class get tobject as parent }
  883. else if classtype in [odt_class,odt_interfacecom] then
  884. setclassparent
  885. else
  886. aktclass:=new(pobjectdef,init(classtype,n,nil));
  887. { read GUID }
  888. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  889. try_to_consume(_LECKKLAMMER) then
  890. begin
  891. readinterfaceiid;
  892. consume(_RECKKLAMMER);
  893. end;
  894. end;
  895. procedure chkcpp;
  896. begin
  897. if is_cppclass(aktclass) then
  898. begin
  899. include(aktprocsym^.definition^.proccalloptions,pocall_cppdecl);
  900. aktprocsym^.definition^.setmangledname(
  901. target_os.Cprefix+aktprocsym^.definition^.cplusplusmangledname);
  902. end;
  903. end;
  904. var
  905. temppd : pprocdef;
  906. begin
  907. {Nowadays aktprocsym may already have a value, so we need to save
  908. it.}
  909. oldprocsym:=aktprocsym;
  910. { forward is resolved }
  911. if assigned(fd) then
  912. exclude(fd^.objectoptions,oo_is_forward);
  913. there_is_a_destructor:=false;
  914. actmembertype:=[sp_public];
  915. { objects and class types can't be declared local }
  916. if (symtablestack^.symtabletype<>globalsymtable) and
  917. (symtablestack^.symtabletype<>staticsymtable) then
  918. Message(parser_e_no_local_objects);
  919. storetypecanbeforward:=typecanbeforward;
  920. { for tp mode don't allow forward types }
  921. if (m_tp in aktmodeswitches) and
  922. not (m_delphi in aktmodeswitches) then
  923. typecanbeforward:=false;
  924. if not(readobjecttype) then
  925. exit;
  926. { also anonym objects aren't allow (o : object a : longint; end;) }
  927. if n='' then
  928. Message(parser_f_no_anonym_objects);
  929. readparentclasses;
  930. { default access is public }
  931. actmembertype:=[sp_public];
  932. { set class flags and inherits published, if necessary? }
  933. setclassattributes;
  934. aktobjectdef:=aktclass;
  935. aktclass^.symtable^.next:=symtablestack;
  936. symtablestack:=aktclass^.symtable;
  937. testcurobject:=1;
  938. curobjectname:=Upper(n);
  939. { new procinfo }
  940. oldprocinfo:=procinfo;
  941. new(procinfo,init);
  942. procinfo^._class:=aktclass;
  943. { short class declaration ? }
  944. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  945. begin
  946. { Parse componenten }
  947. repeat
  948. if (sp_private in actmembertype) then
  949. include(aktclass^.objectoptions,oo_has_private);
  950. if (sp_protected in actmembertype) then
  951. include(aktclass^.objectoptions,oo_has_protected);
  952. case token of
  953. _ID : begin
  954. case idtoken of
  955. _PRIVATE : begin
  956. consume(_PRIVATE);
  957. actmembertype:=[sp_private];
  958. current_object_option:=[sp_private];
  959. end;
  960. _PROTECTED : begin
  961. consume(_PROTECTED);
  962. current_object_option:=[sp_protected];
  963. actmembertype:=[sp_protected];
  964. end;
  965. _PUBLIC : begin
  966. consume(_PUBLIC);
  967. current_object_option:=[sp_public];
  968. actmembertype:=[sp_public];
  969. end;
  970. _PUBLISHED : begin
  971. if not(oo_can_have_published in aktclass^.objectoptions) then
  972. Message(parser_e_cant_have_published);
  973. consume(_PUBLISHED);
  974. current_object_option:=[sp_published];
  975. actmembertype:=[sp_published];
  976. end;
  977. else
  978. read_var_decs(false,true,false);
  979. end;
  980. end;
  981. _PROPERTY : property_dec;
  982. _PROCEDURE,
  983. _FUNCTION,
  984. _CLASS : begin
  985. oldparse_only:=parse_only;
  986. parse_only:=true;
  987. parse_proc_dec;
  988. {$ifndef newcg}
  989. parse_object_proc_directives(aktprocsym);
  990. {$endif newcg}
  991. { check if there are duplicates }
  992. check_identical_proc(temppd);
  993. if (po_msgint in aktprocsym^.definition^.procoptions) then
  994. include(aktclass^.objectoptions,oo_has_msgint);
  995. if (po_msgstr in aktprocsym^.definition^.procoptions) then
  996. include(aktclass^.objectoptions,oo_has_msgstr);
  997. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  998. include(aktclass^.objectoptions,oo_has_virtual);
  999. chkcpp;
  1000. parse_only:=oldparse_only;
  1001. end;
  1002. _CONSTRUCTOR : begin
  1003. if not(sp_public in actmembertype) then
  1004. Message(parser_w_constructor_should_be_public);
  1005. oldparse_only:=parse_only;
  1006. parse_only:=true;
  1007. constructor_head;
  1008. {$ifndef newcg}
  1009. parse_object_proc_directives(aktprocsym);
  1010. {$endif newcg}
  1011. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1012. include(aktclass^.objectoptions,oo_has_virtual);
  1013. chkcpp;
  1014. parse_only:=oldparse_only;
  1015. end;
  1016. _DESTRUCTOR : begin
  1017. if there_is_a_destructor then
  1018. Message(parser_n_only_one_destructor);
  1019. there_is_a_destructor:=true;
  1020. if not(sp_public in actmembertype) then
  1021. Message(parser_w_destructor_should_be_public);
  1022. oldparse_only:=parse_only;
  1023. parse_only:=true;
  1024. destructor_head;
  1025. {$ifndef newcg}
  1026. parse_object_proc_directives(aktprocsym);
  1027. {$endif newcg}
  1028. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  1029. include(aktclass^.objectoptions,oo_has_virtual);
  1030. chkcpp;
  1031. parse_only:=oldparse_only;
  1032. end;
  1033. _END : begin
  1034. consume(_END);
  1035. break;
  1036. end;
  1037. else
  1038. consume(_ID); { Give a ident expected message, like tp7 }
  1039. end;
  1040. until false;
  1041. current_object_option:=[sp_public];
  1042. end;
  1043. testcurobject:=0;
  1044. curobjectname:='';
  1045. typecanbeforward:=storetypecanbeforward;
  1046. { generate vmt space if needed }
  1047. if not(oo_has_vmt in aktclass^.objectoptions) and
  1048. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass^.objectoptions<>[]) or
  1049. (classtype in [odt_class])
  1050. ) then
  1051. aktclass^.insertvmt;
  1052. if (cs_create_smart in aktmoduleswitches) then
  1053. datasegment^.concat(new(pai_cut,init));
  1054. if is_interface(aktclass) then
  1055. writeinterfaceids(aktclass);
  1056. if (oo_has_vmt in aktclass^.objectoptions) then
  1057. writevmt;
  1058. if is_interface(aktclass) then
  1059. setinterfacemethodoptions;
  1060. { restore old state }
  1061. symtablestack:=symtablestack^.next;
  1062. aktobjectdef:=nil;
  1063. {Restore procinfo}
  1064. dispose(procinfo,done);
  1065. procinfo:=oldprocinfo;
  1066. {Restore the aktprocsym.}
  1067. aktprocsym:=oldprocsym;
  1068. object_dec:=aktclass;
  1069. end;
  1070. end.
  1071. {
  1072. $Log$
  1073. Revision 1.9 2000-11-06 23:05:52 florian
  1074. * more fixes
  1075. Revision 1.8 2000/11/06 20:30:55 peter
  1076. * more fixes to get make cycle working
  1077. Revision 1.7 2000/11/04 18:03:57 florian
  1078. * fixed upper/lower case problem
  1079. Revision 1.6 2000/11/04 17:31:00 florian
  1080. * fixed some problems of previous commit
  1081. Revision 1.5 2000/11/04 14:25:20 florian
  1082. + merged Attila's changes for interfaces, not tested yet
  1083. Revision 1.4 2000/10/31 22:02:49 peter
  1084. * symtable splitted, no real code changes
  1085. Revision 1.3 2000/10/26 21:54:03 peter
  1086. * fixed crash with error in child definition (merged)
  1087. Revision 1.2 2000/10/21 18:16:11 florian
  1088. * a lot of changes:
  1089. - basic dyn. array support
  1090. - basic C++ support
  1091. - some work for interfaces done
  1092. ....
  1093. Revision 1.1 2000/10/14 10:14:51 peter
  1094. * moehrendorf oct 2000 rewrite
  1095. }