pdecobj.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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 : tobjectdef) : tdef;
  25. implementation
  26. uses
  27. cutils,cclasses,
  28. globals,verbose,systems,tokens,
  29. aasm,symconst,symbase,symsym,symtable,types,
  30. cgbase,
  31. node,nld,nmem,ncon,ncnv,ncal,pass_1,
  32. scanner,
  33. pbase,pexpr,pdecsub,pdecvar,ptype;
  34. function object_dec(const n : stringid;fd : tobjectdef) : tdef;
  35. { this function parses an object or class declaration }
  36. var
  37. actmembertype : tsymoptions;
  38. there_is_a_destructor : boolean;
  39. classtype : tobjectdeftype;
  40. childof : tobjectdef;
  41. aktclass : tobjectdef;
  42. procedure constructor_head;
  43. begin
  44. consume(_CONSTRUCTOR);
  45. { must be at same level as in implementation }
  46. inc(lexlevel);
  47. parse_proc_head(potype_constructor);
  48. dec(lexlevel);
  49. if (cs_constructor_name in aktglobalswitches) and (aktprocsym.name<>'INIT') then
  50. Message(parser_e_constructorname_must_be_init);
  51. include(aktclass.objectoptions,oo_has_constructor);
  52. consume(_SEMICOLON);
  53. begin
  54. if is_class(aktclass) then
  55. begin
  56. { CLASS constructors return the created instance }
  57. aktprocsym.definition.rettype.def:=aktclass;
  58. end
  59. else
  60. begin
  61. { OBJECT constructors return a boolean }
  62. aktprocsym.definition.rettype:=booltype;
  63. end;
  64. end;
  65. end;
  66. procedure property_dec;
  67. { convert a node tree to symlist and return the last
  68. symbol }
  69. function parse_symlist(pl:tsymlist):boolean;
  70. var
  71. idx : longint;
  72. sym : tsym;
  73. def : tdef;
  74. st : tsymtable;
  75. begin
  76. parse_symlist:=true;
  77. def:=nil;
  78. if token=_ID then
  79. begin
  80. sym:=search_class_member(aktclass,pattern);
  81. if assigned(sym) then
  82. begin
  83. case sym.typ of
  84. varsym :
  85. begin
  86. pl.addsym(sl_load,sym);
  87. def:=tvarsym(sym).vartype.def;
  88. end;
  89. procsym :
  90. begin
  91. pl.addsym(sl_call,sym);
  92. end;
  93. end;
  94. end
  95. else
  96. begin
  97. Message1(parser_e_illegal_field_or_method,pattern);
  98. parse_symlist:=false;
  99. end;
  100. consume(_ID);
  101. repeat
  102. case token of
  103. _ID,
  104. _SEMICOLON :
  105. begin
  106. break;
  107. end;
  108. _POINT :
  109. begin
  110. consume(_POINT);
  111. if assigned(def) then
  112. begin
  113. st:=def.getsymtable(gs_record);
  114. if assigned(st) then
  115. begin
  116. sym:=searchsymonlyin(st,pattern);
  117. if assigned(sym) then
  118. begin
  119. pl.addsym(sl_subscript,sym);
  120. case sym.typ of
  121. varsym :
  122. def:=tvarsym(sym).vartype.def;
  123. else
  124. begin
  125. Message1(sym_e_illegal_field,pattern);
  126. parse_symlist:=false;
  127. end;
  128. end;
  129. end
  130. else
  131. begin
  132. Message1(sym_e_illegal_field,pattern);
  133. parse_symlist:=false;
  134. end;
  135. end
  136. else
  137. begin
  138. Message(cg_e_invalid_qualifier);
  139. parse_symlist:=false;
  140. end;
  141. end;
  142. consume(_ID);
  143. end;
  144. _LECKKLAMMER :
  145. begin
  146. consume(_LECKKLAMMER);
  147. repeat
  148. if def.deftype=arraydef then
  149. begin
  150. idx:=get_intconst;
  151. pl.addconst(sl_vec,idx);
  152. def:=tarraydef(def).elementtype.def;
  153. end
  154. else
  155. begin
  156. Message(cg_e_invalid_qualifier);
  157. parse_symlist:=false;
  158. end;
  159. until not try_to_consume(_COMMA);
  160. consume(_RECKKLAMMER);
  161. end;
  162. else
  163. begin
  164. Message(parser_e_ill_property_access_sym);
  165. parse_symlist:=false;
  166. break;
  167. end;
  168. end;
  169. until false;
  170. end
  171. else
  172. begin
  173. Message(parser_e_ill_property_access_sym);
  174. parse_symlist:=false;
  175. end;
  176. pl.def:=def;
  177. end;
  178. var
  179. sym : tsym;
  180. propertyparas : tlinkedlist;
  181. { returns the matching procedure to access a property }
  182. function get_procdef : tprocdef;
  183. var
  184. p : tprocdef;
  185. begin
  186. p:=tprocsym(sym).definition;
  187. get_procdef:=nil;
  188. while assigned(p) do
  189. begin
  190. if equal_paras(p.para,propertyparas,cp_value_equal_const) or
  191. convertable_paras(p.para,propertyparas,cp_value_equal_const) then
  192. break;
  193. p:=p.nextoverloaded;
  194. end;
  195. get_procdef:=p;
  196. end;
  197. var
  198. hp2,datacoll : tparaitem;
  199. p : tpropertysym;
  200. overriden : tsym;
  201. hs : string;
  202. varspez : tvarspez;
  203. sc : tidstringlist;
  204. s : string;
  205. tt : ttype;
  206. declarepos : tfileposinfo;
  207. pp : tprocdef;
  208. pt : tnode;
  209. propname : stringid;
  210. begin
  211. { check for a class }
  212. aktprocsym:=nil;
  213. if not((is_class_or_interface(aktclass)) or
  214. ((m_delphi in aktmodeswitches) and (is_object(aktclass)))) then
  215. Message(parser_e_syntax_error);
  216. consume(_PROPERTY);
  217. propertyparas:=TParaLinkedList.Create;
  218. datacoll:=nil;
  219. if token=_ID then
  220. begin
  221. p:=tpropertysym.create(orgpattern);
  222. propname:=pattern;
  223. consume(_ID);
  224. { property parameters ? }
  225. if token=_LECKKLAMMER then
  226. begin
  227. if (sp_published in current_object_option) then
  228. Message(parser_e_cant_publish_that_property);
  229. { create a list of the parameters in propertyparas }
  230. consume(_LECKKLAMMER);
  231. inc(testcurobject);
  232. repeat
  233. if token=_VAR then
  234. begin
  235. consume(_VAR);
  236. varspez:=vs_var;
  237. end
  238. else if token=_CONST then
  239. begin
  240. consume(_CONST);
  241. varspez:=vs_const;
  242. end
  243. else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  244. begin
  245. consume(_OUT);
  246. varspez:=vs_out;
  247. end
  248. else varspez:=vs_value;
  249. sc:=consume_idlist;
  250. {$ifdef fixLeaksOnError}
  251. strContStack.push(sc);
  252. {$endif fixLeaksOnError}
  253. if token=_COLON then
  254. begin
  255. consume(_COLON);
  256. if token=_ARRAY then
  257. begin
  258. {
  259. if (varspez<>vs_const) and
  260. (varspez<>vs_var) then
  261. begin
  262. varspez:=vs_const;
  263. Message(parser_e_illegal_open_parameter);
  264. end;
  265. }
  266. consume(_ARRAY);
  267. consume(_OF);
  268. { define range and type of range }
  269. tt.setdef(tarraydef.create(0,-1,s32bittype));
  270. { define field type }
  271. single_type(tarraydef(tt.def).elementtype,s,false);
  272. end
  273. else
  274. single_type(tt,s,false);
  275. end
  276. else
  277. tt:=cformaltype;
  278. repeat
  279. s:=sc.get(declarepos);
  280. if s='' then
  281. break;
  282. hp2:=TParaItem.create;
  283. hp2.paratyp:=varspez;
  284. hp2.paratype:=tt;
  285. propertyparas.insert(hp2);
  286. until false;
  287. {$ifdef fixLeaksOnError}
  288. if strContStack.pop <> sc then
  289. writeln('problem with strContStack in ptype');
  290. {$endif fixLeaksOnError}
  291. sc.free;
  292. until not try_to_consume(_SEMICOLON);
  293. dec(testcurobject);
  294. consume(_RECKKLAMMER);
  295. end;
  296. { overriden property ? }
  297. { force property interface, if there is a property parameter }
  298. if (token=_COLON) or not(propertyparas.empty) then
  299. begin
  300. consume(_COLON);
  301. single_type(p.proptype,hs,false);
  302. if (idtoken=_INDEX) then
  303. begin
  304. consume(_INDEX);
  305. pt:=comp_expr(true);
  306. if is_constnode(pt) and
  307. is_ordinal(pt.resulttype.def) and
  308. (not is_64bitint(pt.resulttype.def)) then
  309. p.index:=tordconstnode(pt).value
  310. else
  311. begin
  312. Message(parser_e_invalid_property_index_value);
  313. p.index:=0;
  314. end;
  315. p.indextype.setdef(pt.resulttype.def);
  316. include(p.propoptions,ppo_indexed);
  317. { concat a longint to the para template }
  318. hp2:=TParaItem.Create;
  319. hp2.paratyp:=vs_value;
  320. hp2.paratype:=p.indextype;
  321. propertyparas.insert(hp2);
  322. pt.free;
  323. end;
  324. { the parser need to know if a property has parameters }
  325. if not(propertyparas.empty) then
  326. include(p.propoptions,ppo_hasparameters);
  327. end
  328. else
  329. begin
  330. { do an property override }
  331. overriden:=search_class_member(aktclass,propname);
  332. if assigned(overriden) and (overriden.typ=propertysym) then
  333. begin
  334. p.dooverride(tpropertysym(overriden));
  335. end
  336. else
  337. begin
  338. p.proptype:=generrortype;
  339. message(parser_e_no_property_found_to_override);
  340. end;
  341. end;
  342. if (sp_published in current_object_option) and
  343. not(p.proptype.def.is_publishable) then
  344. Message(parser_e_cant_publish_that_property);
  345. { create data defcoll to allow correct parameter checks }
  346. datacoll:=TParaItem.Create;
  347. datacoll.paratyp:=vs_value;
  348. datacoll.paratype:=p.proptype;
  349. if try_to_consume(_READ) then
  350. begin
  351. p.readaccess.clear;
  352. if parse_symlist(p.readaccess) then
  353. begin
  354. sym:=p.readaccess.firstsym^.sym;
  355. case sym.typ of
  356. procsym :
  357. begin
  358. pp:=get_procdef;
  359. if not(assigned(pp)) or
  360. not(is_equal(pp.rettype.def,p.proptype.def)) then
  361. Message(parser_e_ill_property_access_sym);
  362. p.readaccess.setdef(pp);
  363. end;
  364. varsym :
  365. begin
  366. if not(propertyparas.empty) or
  367. not(is_equal(p.readaccess.def,p.proptype.def)) then
  368. Message(parser_e_ill_property_access_sym);
  369. end;
  370. else
  371. Message(parser_e_ill_property_access_sym);
  372. end;
  373. end;
  374. end;
  375. if try_to_consume(_WRITE) then
  376. begin
  377. p.writeaccess.clear;
  378. if parse_symlist(p.writeaccess) then
  379. begin
  380. sym:=p.writeaccess.firstsym^.sym;
  381. case sym.typ of
  382. procsym :
  383. begin
  384. { insert data entry to check access method }
  385. propertyparas.insert(datacoll);
  386. pp:=get_procdef;
  387. { ... and remove it }
  388. propertyparas.remove(datacoll);
  389. if not(assigned(pp)) then
  390. Message(parser_e_ill_property_access_sym);
  391. p.writeaccess.setdef(pp);
  392. end;
  393. varsym :
  394. begin
  395. if not(propertyparas.empty) or
  396. not(is_equal(p.writeaccess.def,p.proptype.def)) then
  397. Message(parser_e_ill_property_access_sym);
  398. end;
  399. else
  400. Message(parser_e_ill_property_access_sym);
  401. end;
  402. end;
  403. end;
  404. include(p.propoptions,ppo_stored);
  405. if try_to_consume(_STORED) then
  406. begin
  407. p.storedaccess.clear;
  408. case token of
  409. _ID:
  410. begin
  411. { in the case that idtoken=_DEFAULT }
  412. { we have to do nothing except }
  413. { setting ppo_stored, it's the same }
  414. { as stored true }
  415. if idtoken<>_DEFAULT then
  416. begin
  417. if parse_symlist(p.storedaccess) then
  418. begin
  419. sym:=p.storedaccess.firstsym^.sym;
  420. case sym.typ of
  421. procsym :
  422. begin
  423. pp:=tprocsym(sym).definition;
  424. while assigned(pp) do
  425. begin
  426. { the stored function shouldn't have any parameters }
  427. if pp.Para.empty then
  428. break;
  429. pp:=pp.nextoverloaded;
  430. end;
  431. { found we a procedure and does it really return a bool? }
  432. if not(assigned(pp)) or
  433. not(is_boolean(pp.rettype.def)) then
  434. Message(parser_e_ill_property_storage_sym);
  435. p.storedaccess.setdef(pp);
  436. end;
  437. varsym :
  438. begin
  439. if not(propertyparas.empty) or
  440. not(is_boolean(p.storedaccess.def)) then
  441. Message(parser_e_stored_property_must_be_boolean);
  442. end;
  443. else
  444. Message(parser_e_ill_property_access_sym);
  445. end;
  446. end;
  447. end;
  448. end;
  449. _FALSE:
  450. begin
  451. consume(_FALSE);
  452. exclude(p.propoptions,ppo_stored);
  453. end;
  454. _TRUE:
  455. consume(_TRUE);
  456. end;
  457. end;
  458. if try_to_consume(_DEFAULT) then
  459. begin
  460. if not(is_ordinal(p.proptype.def) or
  461. is_64bitint(p.proptype.def) or
  462. ((p.proptype.def.deftype=setdef) and
  463. (tsetdef(p.proptype.def).settype=smallset))) or
  464. not(propertyparas.empty) then
  465. Message(parser_e_property_cant_have_a_default_value);
  466. { Get the result of the default, the firstpass is
  467. needed to support values like -1 }
  468. pt:=comp_expr(true);
  469. if (p.proptype.def.deftype=setdef) and
  470. (pt.nodetype=arrayconstructorn) then
  471. begin
  472. arrayconstructor_to_set(tarrayconstructornode(pt));
  473. do_resulttypepass(pt);
  474. end;
  475. inserttypeconv(pt,p.proptype);
  476. if not(is_constnode(pt)) then
  477. Message(parser_e_property_default_value_must_const);
  478. if pt.nodetype=setconstn then
  479. p.default:=plongint(tsetconstnode(pt).value_set)^
  480. else
  481. p.default:=tordconstnode(pt).value;
  482. pt.free;
  483. end
  484. else if try_to_consume(_NODEFAULT) then
  485. begin
  486. p.default:=0;
  487. end;
  488. symtablestack.insert(p);
  489. { default property ? }
  490. consume(_SEMICOLON);
  491. if (idtoken=_DEFAULT) then
  492. begin
  493. consume(_DEFAULT);
  494. { overriding a default propertyp is allowed
  495. p2:=search_default_property(aktclass);
  496. if assigned(p2) then
  497. message1(parser_e_only_one_default_property,
  498. tobjectdef(p2.owner.defowner)^.objname^)
  499. else
  500. }
  501. begin
  502. include(p.propoptions,ppo_defaultproperty);
  503. if propertyparas.empty then
  504. message(parser_e_property_need_paras);
  505. end;
  506. consume(_SEMICOLON);
  507. end;
  508. { clean up }
  509. if assigned(datacoll) then
  510. datacoll.free;
  511. end
  512. else
  513. begin
  514. consume(_ID);
  515. consume(_SEMICOLON);
  516. end;
  517. propertyparas.free;
  518. end;
  519. procedure destructor_head;
  520. begin
  521. consume(_DESTRUCTOR);
  522. inc(lexlevel);
  523. parse_proc_head(potype_destructor);
  524. dec(lexlevel);
  525. if (cs_constructor_name in aktglobalswitches) and (aktprocsym.name<>'DONE') then
  526. Message(parser_e_destructorname_must_be_done);
  527. include(aktclass.objectoptions,oo_has_destructor);
  528. consume(_SEMICOLON);
  529. if not(aktprocsym.definition.Para.empty) then
  530. if not (m_tp in aktmodeswitches) then
  531. Message(parser_e_no_paras_for_destructor);
  532. { no return value }
  533. aktprocsym.definition.rettype:=voidtype;
  534. end;
  535. var
  536. hs : string;
  537. pcrd : tclassrefdef;
  538. tt : ttype;
  539. oldprocinfo : pprocinfo;
  540. oldprocsym : tprocsym;
  541. oldparse_only : boolean;
  542. storetypecanbeforward : boolean;
  543. procedure setclassattributes;
  544. begin
  545. { publishable }
  546. if classtype in [odt_interfacecom,odt_class] then
  547. begin
  548. aktclass.objecttype:=classtype;
  549. if (cs_generate_rtti in aktlocalswitches) or
  550. (assigned(aktclass.childof) and
  551. (oo_can_have_published in aktclass.childof.objectoptions)) then
  552. begin
  553. include(aktclass.objectoptions,oo_can_have_published);
  554. { in "publishable" classes the default access type is published }
  555. actmembertype:=[sp_published];
  556. { don't know if this is necessary (FK) }
  557. current_object_option:=[sp_published];
  558. end;
  559. end;
  560. end;
  561. procedure setclassparent;
  562. begin
  563. if assigned(fd) then
  564. aktclass:=fd
  565. else
  566. aktclass:=tobjectdef.create(classtype,n,nil);
  567. { is the current class tobject? }
  568. { so you could define your own tobject }
  569. if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_class) and (upper(n)='TOBJECT') then
  570. class_tobject:=aktclass
  571. else if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  572. interface_iunknown:=aktclass
  573. else
  574. begin
  575. case classtype of
  576. odt_class:
  577. childof:=class_tobject;
  578. odt_interfacecom:
  579. childof:=interface_iunknown;
  580. end;
  581. if (oo_is_forward in childof.objectoptions) then
  582. Message1(parser_e_forward_declaration_must_be_resolved,childof.objname^);
  583. aktclass.set_parent(childof);
  584. end;
  585. end;
  586. procedure setinterfacemethodoptions;
  587. var
  588. i: longint;
  589. defs: TIndexArray;
  590. pd: tprocdef;
  591. begin
  592. include(aktclass.objectoptions,oo_has_virtual);
  593. defs:=aktclass.symtable.defindex;
  594. for i:=1 to defs.count do
  595. begin
  596. pd:=tprocdef(defs.search(i));
  597. if pd.deftype=procdef then
  598. begin
  599. pd.extnumber:=aktclass.lastvtableindex;
  600. inc(aktclass.lastvtableindex);
  601. include(pd.procoptions,po_virtualmethod);
  602. pd.forwarddef:=false;
  603. end;
  604. end;
  605. end;
  606. function readobjecttype : boolean;
  607. begin
  608. readobjecttype:=true;
  609. { distinguish classes and objects }
  610. case token of
  611. _OBJECT:
  612. begin
  613. classtype:=odt_object;
  614. consume(_OBJECT)
  615. end;
  616. _CPPCLASS:
  617. begin
  618. classtype:=odt_cppclass;
  619. consume(_CPPCLASS);
  620. end;
  621. _INTERFACE:
  622. begin
  623. if aktinterfacetype=it_interfacecom then
  624. classtype:=odt_interfacecom
  625. else {it_interfacecorba}
  626. classtype:=odt_interfacecorba;
  627. consume(_INTERFACE);
  628. { forward declaration }
  629. if not(assigned(fd)) and (token=_SEMICOLON) then
  630. begin
  631. { also anonym objects aren't allow (o : object a : longint; end;) }
  632. if n='' then
  633. Message(parser_f_no_anonym_objects);
  634. aktclass:=tobjectdef.create(classtype,n,nil);
  635. if (cs_compilesystem in aktmoduleswitches) and
  636. (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  637. interface_iunknown:=aktclass;
  638. include(aktclass.objectoptions,oo_is_forward);
  639. object_dec:=aktclass;
  640. typecanbeforward:=storetypecanbeforward;
  641. readobjecttype:=false;
  642. exit;
  643. end;
  644. end;
  645. _CLASS:
  646. begin
  647. classtype:=odt_class;
  648. consume(_CLASS);
  649. if not(assigned(fd)) and
  650. (token=_OF) and
  651. { Delphi only allows class of in type blocks.
  652. Note that when parsing the type of a variable declaration
  653. the blocktype is bt_type so the check for typecanbeforward
  654. is also necessary (PFV) }
  655. (((block_type=bt_type) and typecanbeforward) or
  656. not(m_delphi in aktmodeswitches)) then
  657. begin
  658. { a hack, but it's easy to handle }
  659. { class reference type }
  660. consume(_OF);
  661. single_type(tt,hs,typecanbeforward);
  662. { accept hp1, if is a forward def or a class }
  663. if (tt.def.deftype=forwarddef) or
  664. is_class(tt.def) then
  665. begin
  666. pcrd:=tclassrefdef.create(tt);
  667. object_dec:=pcrd;
  668. end
  669. else
  670. begin
  671. object_dec:=generrortype.def;
  672. Message1(type_e_class_type_expected,generrortype.def.typename);
  673. end;
  674. typecanbeforward:=storetypecanbeforward;
  675. readobjecttype:=false;
  676. exit;
  677. end
  678. { forward class }
  679. else if not(assigned(fd)) and (token=_SEMICOLON) then
  680. begin
  681. { also anonym objects aren't allow (o : object a : longint; end;) }
  682. if n='' then
  683. Message(parser_f_no_anonym_objects);
  684. aktclass:=tobjectdef.create(odt_class,n,nil);
  685. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  686. class_tobject:=aktclass;
  687. aktclass.objecttype:=odt_class;
  688. include(aktclass.objectoptions,oo_is_forward);
  689. { all classes must have a vmt !! at offset zero }
  690. if not(oo_has_vmt in aktclass.objectoptions) then
  691. aktclass.insertvmt;
  692. object_dec:=aktclass;
  693. typecanbeforward:=storetypecanbeforward;
  694. readobjecttype:=false;
  695. exit;
  696. end;
  697. end;
  698. else
  699. begin
  700. classtype:=odt_class; { this is error but try to recover }
  701. consume(_OBJECT);
  702. end;
  703. end;
  704. end;
  705. procedure readimplementedinterfaces;
  706. var
  707. implintf: tobjectdef;
  708. tt : ttype;
  709. begin
  710. while try_to_consume(_COMMA) do begin
  711. id_type(tt,pattern,false);
  712. implintf:=tobjectdef(tt.def);
  713. if (tt.def.deftype<>objectdef) then begin
  714. Message1(type_e_interface_type_expected,tt.def.typename);
  715. Continue; { omit }
  716. end;
  717. if not is_interface(implintf) then begin
  718. Message1(type_e_interface_type_expected,implintf.typename);
  719. Continue; { omit }
  720. end;
  721. if aktclass.implementedinterfaces.searchintf(tt.def)<>-1 then
  722. Message1(sym_e_duplicate_id,tt.def.name)
  723. else
  724. aktclass.implementedinterfaces.addintf(tt.def);
  725. end;
  726. end;
  727. procedure readinterfaceiid;
  728. var
  729. p : tnode;
  730. begin
  731. p:=comp_expr(true);
  732. if p.nodetype=stringconstn then
  733. begin
  734. aktclass.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  735. p.free;
  736. aktclass.isiidguidvalid:=string2guid(aktclass.iidstr^,aktclass.iidguid);
  737. if (classtype=odt_interfacecom) and not aktclass.isiidguidvalid then
  738. Message(parser_e_improper_guid_syntax);
  739. end
  740. else
  741. begin
  742. p.free;
  743. Message(cg_e_illegal_expression);
  744. end;
  745. end;
  746. procedure readparentclasses;
  747. begin
  748. { reads the parent class }
  749. if token=_LKLAMMER then
  750. begin
  751. consume(_LKLAMMER);
  752. id_type(tt,pattern,false);
  753. childof:=tobjectdef(tt.def);
  754. if (not assigned(childof)) or
  755. (childof.deftype<>objectdef) then
  756. begin
  757. if assigned(childof) then
  758. Message1(type_e_class_type_expected,childof.typename);
  759. childof:=nil;
  760. aktclass:=tobjectdef.create(classtype,n,nil);
  761. end
  762. else
  763. begin
  764. { a mix of class, interfaces, objects and cppclasses
  765. isn't allowed }
  766. case classtype of
  767. odt_class:
  768. if not(is_class(childof)) and
  769. not(is_interface(childof)) then
  770. Message(parser_e_mix_of_classes_and_objects);
  771. odt_interfacecorba,
  772. odt_interfacecom:
  773. if not(is_interface(childof)) then
  774. Message(parser_e_mix_of_classes_and_objects);
  775. odt_cppclass:
  776. if not(is_cppclass(childof)) then
  777. Message(parser_e_mix_of_classes_and_objects);
  778. odt_object:
  779. if not(is_object(childof)) then
  780. Message(parser_e_mix_of_classes_and_objects);
  781. end;
  782. { the forward of the child must be resolved to get
  783. correct field addresses }
  784. if assigned(fd) then
  785. begin
  786. if (oo_is_forward in childof.objectoptions) then
  787. Message1(parser_e_forward_declaration_must_be_resolved,childof.objname^);
  788. aktclass:=fd;
  789. { we must inherit several options !!
  790. this was missing !!
  791. all is now done in set_parent
  792. including symtable datasize setting PM }
  793. fd.set_parent(childof);
  794. end
  795. else
  796. aktclass:=tobjectdef.create(classtype,n,childof);
  797. if aktclass.objecttype=odt_class then
  798. readimplementedinterfaces;
  799. end;
  800. consume(_RKLAMMER);
  801. end
  802. { if no parent class, then a class get tobject as parent }
  803. else if classtype in [odt_class,odt_interfacecom] then
  804. setclassparent
  805. else
  806. aktclass:=tobjectdef.create(classtype,n,nil);
  807. { read GUID }
  808. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  809. try_to_consume(_LECKKLAMMER) then
  810. begin
  811. readinterfaceiid;
  812. consume(_RECKKLAMMER);
  813. end;
  814. end;
  815. procedure chkcpp;
  816. begin
  817. if is_cppclass(aktclass) then
  818. begin
  819. include(aktprocsym.definition.proccalloptions,pocall_cppdecl);
  820. aktprocsym.definition.setmangledname(
  821. target_info.Cprefix+aktprocsym.definition.cplusplusmangledname);
  822. end;
  823. end;
  824. var
  825. temppd : tprocdef;
  826. begin
  827. {Nowadays aktprocsym may already have a value, so we need to save
  828. it.}
  829. oldprocsym:=aktprocsym;
  830. { forward is resolved }
  831. if assigned(fd) then
  832. exclude(fd.objectoptions,oo_is_forward);
  833. there_is_a_destructor:=false;
  834. actmembertype:=[sp_public];
  835. { objects and class types can't be declared local }
  836. if not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) then
  837. Message(parser_e_no_local_objects);
  838. storetypecanbeforward:=typecanbeforward;
  839. { for tp mode don't allow forward types }
  840. if (m_tp in aktmodeswitches) and
  841. not (m_delphi in aktmodeswitches) then
  842. typecanbeforward:=false;
  843. if not(readobjecttype) then
  844. exit;
  845. { also anonym objects aren't allow (o : object a : longint; end;) }
  846. if n='' then
  847. Message(parser_f_no_anonym_objects);
  848. readparentclasses;
  849. { default access is public }
  850. actmembertype:=[sp_public];
  851. { set class flags and inherits published, if necessary? }
  852. setclassattributes;
  853. aktobjectdef:=aktclass;
  854. aktclass.symtable.next:=symtablestack;
  855. symtablestack:=aktclass.symtable;
  856. testcurobject:=1;
  857. curobjectname:=Upper(n);
  858. { new procinfo }
  859. oldprocinfo:=procinfo;
  860. new(procinfo,init);
  861. procinfo^._class:=aktclass;
  862. { short class declaration ? }
  863. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  864. begin
  865. { Parse componenten }
  866. repeat
  867. if (sp_private in actmembertype) then
  868. include(aktclass.objectoptions,oo_has_private);
  869. if (sp_protected in actmembertype) then
  870. include(aktclass.objectoptions,oo_has_protected);
  871. case token of
  872. _ID : begin
  873. case idtoken of
  874. _PRIVATE : begin
  875. if is_interface(aktclass) then
  876. Message(parser_e_no_access_specifier_in_interfaces);
  877. consume(_PRIVATE);
  878. actmembertype:=[sp_private];
  879. current_object_option:=[sp_private];
  880. end;
  881. _PROTECTED : begin
  882. if is_interface(aktclass) then
  883. Message(parser_e_no_access_specifier_in_interfaces);
  884. consume(_PROTECTED);
  885. current_object_option:=[sp_protected];
  886. actmembertype:=[sp_protected];
  887. end;
  888. _PUBLIC : begin
  889. if is_interface(aktclass) then
  890. Message(parser_e_no_access_specifier_in_interfaces);
  891. consume(_PUBLIC);
  892. current_object_option:=[sp_public];
  893. actmembertype:=[sp_public];
  894. end;
  895. _PUBLISHED : begin
  896. if is_interface(aktclass) then
  897. Message(parser_e_no_access_specifier_in_interfaces)
  898. else
  899. if not(oo_can_have_published in aktclass.objectoptions) then
  900. Message(parser_e_cant_have_published);
  901. consume(_PUBLISHED);
  902. current_object_option:=[sp_published];
  903. actmembertype:=[sp_published];
  904. end;
  905. else
  906. if is_interface(aktclass) then
  907. Message(parser_e_no_vars_in_interfaces);
  908. read_var_decs(false,true,false);
  909. end;
  910. end;
  911. _PROPERTY : begin
  912. property_dec;
  913. end;
  914. _PROCEDURE,
  915. _FUNCTION,
  916. _CLASS : begin
  917. oldparse_only:=parse_only;
  918. parse_only:=true;
  919. parse_proc_dec;
  920. { this is for error recovery as well as forward }
  921. { interface mappings, i.e. mapping to a method }
  922. { which isn't declared yet }
  923. if assigned(aktprocsym) then
  924. begin
  925. {$ifndef newcg}
  926. parse_object_proc_directives(aktprocsym);
  927. {$endif newcg}
  928. { check if there are duplicates }
  929. check_identical_proc(temppd);
  930. if (po_msgint in aktprocsym.definition.procoptions) then
  931. include(aktclass.objectoptions,oo_has_msgint);
  932. if (po_msgstr in aktprocsym.definition.procoptions) then
  933. include(aktclass.objectoptions,oo_has_msgstr);
  934. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  935. include(aktclass.objectoptions,oo_has_virtual);
  936. chkcpp;
  937. end;
  938. parse_only:=oldparse_only;
  939. end;
  940. _CONSTRUCTOR : begin
  941. if not(sp_public in actmembertype) then
  942. Message(parser_w_constructor_should_be_public);
  943. if is_interface(aktclass) then
  944. Message(parser_e_no_con_des_in_interfaces);
  945. oldparse_only:=parse_only;
  946. parse_only:=true;
  947. constructor_head;
  948. {$ifndef newcg}
  949. parse_object_proc_directives(aktprocsym);
  950. {$endif newcg}
  951. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  952. include(aktclass.objectoptions,oo_has_virtual);
  953. chkcpp;
  954. parse_only:=oldparse_only;
  955. end;
  956. _DESTRUCTOR : begin
  957. if there_is_a_destructor then
  958. Message(parser_n_only_one_destructor);
  959. if is_interface(aktclass) then
  960. Message(parser_e_no_con_des_in_interfaces);
  961. there_is_a_destructor:=true;
  962. if not(sp_public in actmembertype) then
  963. Message(parser_w_destructor_should_be_public);
  964. oldparse_only:=parse_only;
  965. parse_only:=true;
  966. destructor_head;
  967. {$ifndef newcg}
  968. parse_object_proc_directives(aktprocsym);
  969. {$endif newcg}
  970. if (po_virtualmethod in aktprocsym.definition.procoptions) then
  971. include(aktclass.objectoptions,oo_has_virtual);
  972. chkcpp;
  973. parse_only:=oldparse_only;
  974. end;
  975. _END : begin
  976. consume(_END);
  977. break;
  978. end;
  979. else
  980. consume(_ID); { Give a ident expected message, like tp7 }
  981. end;
  982. until false;
  983. current_object_option:=[sp_public];
  984. end;
  985. { generate vmt space if needed }
  986. if not(oo_has_vmt in aktclass.objectoptions) and
  987. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  988. (classtype in [odt_class])
  989. ) then
  990. aktclass.insertvmt;
  991. if is_interface(aktclass) then
  992. setinterfacemethodoptions;
  993. { reset }
  994. testcurobject:=0;
  995. curobjectname:='';
  996. typecanbeforward:=storetypecanbeforward;
  997. { restore old state }
  998. symtablestack:=symtablestack.next;
  999. aktobjectdef:=nil;
  1000. {Restore procinfo}
  1001. dispose(procinfo,done);
  1002. procinfo:=oldprocinfo;
  1003. {Restore the aktprocsym.}
  1004. aktprocsym:=oldprocsym;
  1005. object_dec:=aktclass;
  1006. end;
  1007. end.
  1008. {
  1009. $Log$
  1010. Revision 1.30 2001-10-21 12:33:06 peter
  1011. * array access for properties added
  1012. Revision 1.29 2001/08/30 20:13:53 peter
  1013. * rtti/init table updates
  1014. * rttisym for reusable global rtti/init info
  1015. * support published for interfaces
  1016. Revision 1.28 2001/08/26 13:36:44 florian
  1017. * some cg reorganisation
  1018. * some PPC updates
  1019. Revision 1.27 2001/08/22 21:16:20 florian
  1020. * some interfaces related problems regarding
  1021. mapping of interface implementions fixed
  1022. Revision 1.26 2001/06/03 21:57:36 peter
  1023. + hint directive parsing support
  1024. Revision 1.25 2001/05/04 15:52:03 florian
  1025. * some Delphi incompatibilities fixed:
  1026. - out, dispose and new can be used as idenfiers now
  1027. - const p = apointerype(nil); is supported now
  1028. + support for const p = apointertype(pointer(1234)); added
  1029. Revision 1.24 2001/04/21 15:36:00 peter
  1030. * check for type block when parsing class of
  1031. Revision 1.23 2001/04/21 13:37:16 peter
  1032. * made tclassheader using class of to implement cpu dependent code
  1033. Revision 1.22 2001/04/18 22:01:54 peter
  1034. * registration of targets and assemblers
  1035. Revision 1.21 2001/04/13 01:22:11 peter
  1036. * symtable change to classes
  1037. * range check generation and errors fixed, make cycle DEBUG=1 works
  1038. * memory leaks fixed
  1039. Revision 1.20 2001/04/04 22:43:51 peter
  1040. * remove unnecessary calls to firstpass
  1041. Revision 1.19 2001/04/04 21:30:43 florian
  1042. * applied several fixes to get the DD8 Delphi Unit compiled
  1043. e.g. "forward"-interfaces are working now
  1044. Revision 1.18 2001/04/02 21:20:31 peter
  1045. * resulttype rewrite
  1046. Revision 1.17 2001/03/16 14:56:38 marco
  1047. * Pavel's fixes commited (Peter asked). Cycled to test
  1048. Revision 1.16 2001/03/11 22:58:49 peter
  1049. * getsym redesign, removed the globals srsym,srsymtable
  1050. Revision 1.15 2000/12/25 00:07:27 peter
  1051. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1052. tlinkedlist objects)
  1053. Revision 1.14 2000/11/29 00:30:35 florian
  1054. * unused units removed from uses clause
  1055. * some changes for widestrings
  1056. Revision 1.13 2000/11/17 17:32:58 florian
  1057. * properties can now be used in interfaces
  1058. Revision 1.12 2000/11/17 08:21:07 florian
  1059. *** empty log message ***
  1060. Revision 1.11 2000/11/12 23:24:11 florian
  1061. * interfaces are basically running
  1062. Revision 1.10 2000/11/12 22:17:47 peter
  1063. * some realname updates for messages
  1064. Revision 1.9 2000/11/06 23:05:52 florian
  1065. * more fixes
  1066. Revision 1.8 2000/11/06 20:30:55 peter
  1067. * more fixes to get make cycle working
  1068. Revision 1.7 2000/11/04 18:03:57 florian
  1069. * fixed upper/lower case problem
  1070. Revision 1.6 2000/11/04 17:31:00 florian
  1071. * fixed some problems of previous commit
  1072. Revision 1.5 2000/11/04 14:25:20 florian
  1073. + merged Attila's changes for interfaces, not tested yet
  1074. Revision 1.4 2000/10/31 22:02:49 peter
  1075. * symtable splitted, no real code changes
  1076. Revision 1.3 2000/10/26 21:54:03 peter
  1077. * fixed crash with error in child definition (merged)
  1078. Revision 1.2 2000/10/21 18:16:11 florian
  1079. * a lot of changes:
  1080. - basic dyn. array support
  1081. - basic C++ support
  1082. - some work for interfaces done
  1083. ....
  1084. Revision 1.1 2000/10/14 10:14:51 peter
  1085. * moehrendorf oct 2000 rewrite
  1086. }