pdecobj.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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. symconst,symbase,symsym,symtable,defutil,defcmp,
  30. cgbase,
  31. node,nld,nmem,ncon,ncnv,ncal,pass_1,
  32. scanner,
  33. pbase,pexpr,pdecsub,pdecvar,ptype
  34. {$ifdef delphi}
  35. ,dmisc
  36. ,sysutils
  37. {$endif}
  38. ;
  39. const
  40. { Please leave this here, this module should NOT use
  41. these variables.
  42. Declaring it as string here results in an error when compiling (PFV) }
  43. current_procdef = 'error';
  44. function object_dec(const n : stringid;fd : tobjectdef) : tdef;
  45. { this function parses an object or class declaration }
  46. var
  47. there_is_a_destructor : boolean;
  48. classtype : tobjectdeftype;
  49. childof : tobjectdef;
  50. aktclass : tobjectdef;
  51. function constructor_head:tprocdef;
  52. var
  53. pd : tprocdef;
  54. begin
  55. consume(_CONSTRUCTOR);
  56. { must be at same level as in implementation }
  57. pd:=parse_proc_head(aktclass,potype_constructor);
  58. if (cs_constructor_name in aktglobalswitches) and
  59. (pd.procsym.name<>'INIT') then
  60. Message(parser_e_constructorname_must_be_init);
  61. consume(_SEMICOLON);
  62. include(aktclass.objectoptions,oo_has_constructor);
  63. { Set return type, class constructors return the
  64. created instance, object constructors return boolean }
  65. if is_class(pd._class) then
  66. pd.rettype.setdef(pd._class)
  67. else
  68. pd.rettype:=booltype;
  69. constructor_head:=pd;
  70. end;
  71. procedure property_dec;
  72. { convert a node tree to symlist and return the last
  73. symbol }
  74. function parse_symlist(pl:tsymlist):boolean;
  75. var
  76. idx : longint;
  77. sym : tsym;
  78. def : tdef;
  79. st : tsymtable;
  80. begin
  81. parse_symlist:=true;
  82. def:=nil;
  83. if token=_ID then
  84. begin
  85. sym:=search_class_member(aktclass,pattern);
  86. if assigned(sym) then
  87. begin
  88. case sym.typ of
  89. varsym :
  90. begin
  91. pl.addsym(sl_load,sym);
  92. def:=tvarsym(sym).vartype.def;
  93. end;
  94. procsym :
  95. begin
  96. pl.addsym(sl_call,sym);
  97. end;
  98. end;
  99. end
  100. else
  101. begin
  102. Message1(parser_e_illegal_field_or_method,pattern);
  103. parse_symlist:=false;
  104. end;
  105. consume(_ID);
  106. repeat
  107. case token of
  108. _ID,
  109. _SEMICOLON :
  110. begin
  111. break;
  112. end;
  113. _POINT :
  114. begin
  115. consume(_POINT);
  116. if assigned(def) then
  117. begin
  118. st:=def.getsymtable(gs_record);
  119. if assigned(st) then
  120. begin
  121. sym:=searchsymonlyin(st,pattern);
  122. if assigned(sym) then
  123. begin
  124. pl.addsym(sl_subscript,sym);
  125. case sym.typ of
  126. varsym :
  127. def:=tvarsym(sym).vartype.def;
  128. else
  129. begin
  130. Message1(sym_e_illegal_field,pattern);
  131. parse_symlist:=false;
  132. end;
  133. end;
  134. end
  135. else
  136. begin
  137. Message1(sym_e_illegal_field,pattern);
  138. parse_symlist:=false;
  139. end;
  140. end
  141. else
  142. begin
  143. Message(cg_e_invalid_qualifier);
  144. parse_symlist:=false;
  145. end;
  146. end;
  147. consume(_ID);
  148. end;
  149. _LECKKLAMMER :
  150. begin
  151. consume(_LECKKLAMMER);
  152. repeat
  153. if def.deftype=arraydef then
  154. begin
  155. idx:=get_intconst;
  156. pl.addconst(sl_vec,idx);
  157. def:=tarraydef(def).elementtype.def;
  158. end
  159. else
  160. begin
  161. Message(cg_e_invalid_qualifier);
  162. parse_symlist:=false;
  163. end;
  164. until not try_to_consume(_COMMA);
  165. consume(_RECKKLAMMER);
  166. end;
  167. else
  168. begin
  169. Message(parser_e_ill_property_access_sym);
  170. parse_symlist:=false;
  171. break;
  172. end;
  173. end;
  174. until false;
  175. end
  176. else
  177. begin
  178. Message(parser_e_ill_property_access_sym);
  179. parse_symlist:=false;
  180. end;
  181. pl.def:=def;
  182. end;
  183. var
  184. sym : tsym;
  185. p : tpropertysym;
  186. overriden : tsym;
  187. hs : string;
  188. varspez : tvarspez;
  189. s : string;
  190. tt : ttype;
  191. arraytype : ttype;
  192. pp : Tprocdef;
  193. pd : tprocdef;
  194. pt : tnode;
  195. propname : stringid;
  196. sc : tsinglelist;
  197. oldregisterdef : boolean;
  198. readvs,
  199. hvs : tvarsym;
  200. readprocdef,
  201. writeprocdef : tprocvardef;
  202. begin
  203. { check for a class }
  204. if not((is_class_or_interface(aktclass)) or
  205. ((m_delphi in aktmodeswitches) and (is_object(aktclass)))) then
  206. Message(parser_e_syntax_error);
  207. consume(_PROPERTY);
  208. { Generate temp procvardefs to search for matching read/write
  209. procedures. the readprocdef will store all definitions }
  210. oldregisterdef:=registerdef;
  211. registerdef:=false;
  212. readprocdef:=tprocvardef.create(normal_function_level);
  213. include(readprocdef.procoptions,po_methodpointer);
  214. writeprocdef:=tprocvardef.create(normal_function_level);
  215. include(writeprocdef.procoptions,po_methodpointer);
  216. registerdef:=oldregisterdef;
  217. if token<>_ID then
  218. begin
  219. consume(_ID);
  220. consume(_SEMICOLON);
  221. exit;
  222. end;
  223. { Generate propertysym and insert in symtablestack }
  224. p:=tpropertysym.create(orgpattern);
  225. symtablestack.insert(p);
  226. propname:=pattern;
  227. consume(_ID);
  228. { Set the symtablestack to the parast of readprop so
  229. temp defs will be destroyed after declaration }
  230. readprocdef.parast.next:=symtablestack;
  231. symtablestack:=readprocdef.parast;
  232. { property parameters ? }
  233. if token=_LECKKLAMMER then
  234. begin
  235. if (sp_published in current_object_option) then
  236. Message(parser_e_cant_publish_that_property);
  237. { create a list of the parameters }
  238. sc:=tsinglelist.create;
  239. consume(_LECKKLAMMER);
  240. inc(testcurobject);
  241. repeat
  242. if token=_VAR then
  243. begin
  244. consume(_VAR);
  245. varspez:=vs_var;
  246. end
  247. else if token=_CONST then
  248. begin
  249. consume(_CONST);
  250. varspez:=vs_const;
  251. end
  252. else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  253. begin
  254. consume(_OUT);
  255. varspez:=vs_out;
  256. end
  257. else
  258. varspez:=vs_value;
  259. sc.reset;
  260. repeat
  261. readvs:=tvarsym.create(orgpattern,varspez,generrortype);
  262. readprocdef.parast.insert(readvs);
  263. sc.insert(readvs);
  264. consume(_ID);
  265. until not try_to_consume(_COMMA);
  266. if token=_COLON then
  267. begin
  268. consume(_COLON);
  269. if token=_ARRAY then
  270. begin
  271. consume(_ARRAY);
  272. consume(_OF);
  273. { define range and type of range }
  274. tt.setdef(tarraydef.create(0,-1,s32bittype));
  275. { define field type }
  276. single_type(arraytype,s,false);
  277. tarraydef(tt.def).setelementtype(arraytype);
  278. end
  279. else
  280. single_type(tt,s,false);
  281. end
  282. else
  283. tt:=cformaltype;
  284. readvs:=tvarsym(sc.first);
  285. while assigned(readvs) do
  286. begin
  287. readprocdef.concatpara(nil,tt,readvs,nil,false);
  288. { also update the writeprocdef }
  289. hvs:=tvarsym.create(readvs.realname,vs_value,generrortype);
  290. writeprocdef.parast.insert(hvs);
  291. writeprocdef.concatpara(nil,tt,hvs,nil,false);
  292. readvs:=tvarsym(readvs.listnext);
  293. end;
  294. until not try_to_consume(_SEMICOLON);
  295. sc.free;
  296. dec(testcurobject);
  297. consume(_RECKKLAMMER);
  298. { the parser need to know if a property has parameters, the
  299. index parameter doesn't count (PFV) }
  300. if readprocdef.minparacount>0 then
  301. include(p.propoptions,ppo_hasparameters);
  302. end;
  303. { overriden property ? }
  304. { force property interface, if there is a property parameter }
  305. if (token=_COLON) or (readprocdef.minparacount>0) then
  306. begin
  307. consume(_COLON);
  308. single_type(p.proptype,hs,false);
  309. if (idtoken=_INDEX) then
  310. begin
  311. consume(_INDEX);
  312. pt:=comp_expr(true);
  313. if is_constnode(pt) and
  314. is_ordinal(pt.resulttype.def) and
  315. (not is_64bitint(pt.resulttype.def)) then
  316. p.index:=tordconstnode(pt).value
  317. else
  318. begin
  319. Message(parser_e_invalid_property_index_value);
  320. p.index:=0;
  321. end;
  322. p.indextype.setdef(pt.resulttype.def);
  323. include(p.propoptions,ppo_indexed);
  324. { concat a longint to the para templates }
  325. hvs:=tvarsym.create('$index',vs_value,p.indextype);
  326. readprocdef.parast.insert(hvs);
  327. readprocdef.concatpara(nil,p.indextype,hvs,nil,false);
  328. hvs:=tvarsym.create('$index',vs_value,p.indextype);
  329. writeprocdef.parast.insert(hvs);
  330. writeprocdef.concatpara(nil,p.indextype,hvs,nil,false);
  331. pt.free;
  332. end;
  333. end
  334. else
  335. begin
  336. { do an property override }
  337. overriden:=search_class_member(aktclass.childof,propname);
  338. if assigned(overriden) and (overriden.typ=propertysym) then
  339. begin
  340. p.dooverride(tpropertysym(overriden));
  341. end
  342. else
  343. begin
  344. p.proptype:=generrortype;
  345. message(parser_e_no_property_found_to_override);
  346. end;
  347. end;
  348. if (sp_published in current_object_option) and
  349. not(p.proptype.def.is_publishable) then
  350. Message(parser_e_cant_publish_that_property);
  351. if try_to_consume(_READ) then
  352. begin
  353. p.readaccess.clear;
  354. if parse_symlist(p.readaccess) then
  355. begin
  356. sym:=p.readaccess.firstsym^.sym;
  357. case sym.typ of
  358. procsym :
  359. begin
  360. { read is function returning the type of the property }
  361. readprocdef.rettype:=p.proptype;
  362. { Insert hidden parameters }
  363. calc_parast(readprocdef);
  364. { search procdefs matching readprocdef }
  365. pd:=Tprocsym(sym).search_procdef_bypara(readprocdef.para,p.proptype.def,true,false);
  366. if not(assigned(pd)) then
  367. Message(parser_e_ill_property_access_sym);
  368. p.readaccess.setdef(pd);
  369. end;
  370. varsym :
  371. begin
  372. if compare_defs(p.readaccess.def,p.proptype.def,nothingn)>=te_equal then
  373. begin
  374. { property parameters are allowed if this is
  375. an indexed property, because the index is then
  376. the parameter.
  377. Note: In the help of Kylix it is written
  378. that it isn't allowed, but the compiler accepts it (PFV) }
  379. if (ppo_hasparameters in p.propoptions) then
  380. Message(parser_e_ill_property_access_sym);
  381. end
  382. else
  383. CGMessage2(type_e_incompatible_types,p.readaccess.def.typename,p.proptype.def.typename);
  384. end;
  385. else
  386. Message(parser_e_ill_property_access_sym);
  387. end;
  388. end;
  389. end;
  390. if try_to_consume(_WRITE) then
  391. begin
  392. p.writeaccess.clear;
  393. if parse_symlist(p.writeaccess) then
  394. begin
  395. sym:=p.writeaccess.firstsym^.sym;
  396. case sym.typ of
  397. procsym :
  398. begin
  399. { write is a procedure with an extra value parameter
  400. of the of the property }
  401. writeprocdef.rettype:=voidtype;
  402. hvs:=tvarsym.create('$value',vs_value,p.proptype);
  403. writeprocdef.parast.insert(hvs);
  404. writeprocdef.concatpara(nil,p.proptype,hvs,nil,false);
  405. { Insert hidden parameters }
  406. calc_parast(writeprocdef);
  407. { search procdefs matching writeprocdef }
  408. pd:=Tprocsym(sym).search_procdef_bypara(writeprocdef.para,writeprocdef.rettype.def,true,false);
  409. if not(assigned(pd)) then
  410. Message(parser_e_ill_property_access_sym);
  411. p.writeaccess.setdef(pd);
  412. end;
  413. varsym :
  414. begin
  415. if compare_defs(p.writeaccess.def,p.proptype.def,nothingn)>=te_equal then
  416. begin
  417. { property parameters are allowed if this is
  418. an indexed property, because the index is then
  419. the parameter.
  420. Note: In the help of Kylix it is written
  421. that it isn't allowed, but the compiler accepts it (PFV) }
  422. if (ppo_hasparameters in p.propoptions) then
  423. Message(parser_e_ill_property_access_sym);
  424. end
  425. else
  426. CGMessage2(type_e_incompatible_types,p.readaccess.def.typename,p.proptype.def.typename);
  427. end;
  428. else
  429. Message(parser_e_ill_property_access_sym);
  430. end;
  431. end;
  432. end;
  433. include(p.propoptions,ppo_stored);
  434. if try_to_consume(_STORED) then
  435. begin
  436. p.storedaccess.clear;
  437. case token of
  438. _ID:
  439. begin
  440. { in the case that idtoken=_DEFAULT }
  441. { we have to do nothing except }
  442. { setting ppo_stored, it's the same }
  443. { as stored true }
  444. if idtoken<>_DEFAULT then
  445. begin
  446. if parse_symlist(p.storedaccess) then
  447. begin
  448. sym:=p.storedaccess.firstsym^.sym;
  449. case sym.typ of
  450. procsym :
  451. begin
  452. pp:=Tprocsym(sym).search_procdef_nopara_boolret;
  453. if assigned(pp) then
  454. p.storedaccess.setdef(pp)
  455. else
  456. message(parser_e_ill_property_storage_sym);
  457. end;
  458. varsym :
  459. begin
  460. if (ppo_hasparameters in p.propoptions) or
  461. not(is_boolean(p.storedaccess.def)) then
  462. Message(parser_e_stored_property_must_be_boolean);
  463. end;
  464. else
  465. Message(parser_e_ill_property_access_sym);
  466. end;
  467. end;
  468. end;
  469. end;
  470. _FALSE:
  471. begin
  472. consume(_FALSE);
  473. exclude(p.propoptions,ppo_stored);
  474. end;
  475. _TRUE:
  476. consume(_TRUE);
  477. end;
  478. end;
  479. if try_to_consume(_DEFAULT) then
  480. begin
  481. if not(is_ordinal(p.proptype.def) or
  482. is_64bitint(p.proptype.def) or
  483. ((p.proptype.def.deftype=setdef) and
  484. (tsetdef(p.proptype.def).settype=smallset))) or
  485. ((p.proptype.def.deftype=arraydef) and
  486. (ppo_indexed in p.propoptions)) or
  487. (ppo_hasparameters in p.propoptions) then
  488. Message(parser_e_property_cant_have_a_default_value);
  489. { Get the result of the default, the firstpass is
  490. needed to support values like -1 }
  491. pt:=comp_expr(true);
  492. if (p.proptype.def.deftype=setdef) and
  493. (pt.nodetype=arrayconstructorn) then
  494. begin
  495. arrayconstructor_to_set(pt);
  496. do_resulttypepass(pt);
  497. end;
  498. inserttypeconv(pt,p.proptype);
  499. if not(is_constnode(pt)) then
  500. Message(parser_e_property_default_value_must_const);
  501. if pt.nodetype=setconstn then
  502. p.default:=plongint(tsetconstnode(pt).value_set)^
  503. else
  504. p.default:=tordconstnode(pt).value;
  505. pt.free;
  506. end
  507. else if try_to_consume(_NODEFAULT) then
  508. begin
  509. p.default:=0;
  510. end;
  511. consume(_SEMICOLON);
  512. { default property ? }
  513. if try_to_consume(_DEFAULT) then
  514. begin
  515. include(p.propoptions,ppo_defaultproperty);
  516. if readprocdef.maxparacount=0 then
  517. message(parser_e_property_need_paras);
  518. consume(_SEMICOLON);
  519. end;
  520. { remove temporary procvardefs }
  521. symtablestack:=symtablestack.next;
  522. readprocdef.free;
  523. writeprocdef.free;
  524. end;
  525. function destructor_head:tprocdef;
  526. var
  527. pd : tprocdef;
  528. begin
  529. consume(_DESTRUCTOR);
  530. pd:=parse_proc_head(aktclass,potype_destructor);
  531. if (cs_constructor_name in aktglobalswitches) and
  532. (pd.procsym.name<>'DONE') then
  533. Message(parser_e_destructorname_must_be_done);
  534. if not(pd.maxparacount=0) and
  535. (m_fpc in aktmodeswitches) then
  536. Message(parser_e_no_paras_for_destructor);
  537. consume(_SEMICOLON);
  538. include(aktclass.objectoptions,oo_has_destructor);
  539. { no return value }
  540. pd.rettype:=voidtype;
  541. destructor_head:=pd;
  542. end;
  543. var
  544. hs : string;
  545. pcrd : tclassrefdef;
  546. tt : ttype;
  547. old_object_option : tsymoptions;
  548. old_current_procinfo : tprocinfo;
  549. oldparse_only : boolean;
  550. storetypecanbeforward : boolean;
  551. procedure setclassattributes;
  552. begin
  553. { publishable }
  554. if classtype in [odt_interfacecom,odt_class] then
  555. begin
  556. aktclass.objecttype:=classtype;
  557. if (cs_generate_rtti in aktlocalswitches) or
  558. (assigned(aktclass.childof) and
  559. (oo_can_have_published in aktclass.childof.objectoptions)) then
  560. begin
  561. include(aktclass.objectoptions,oo_can_have_published);
  562. { in "publishable" classes the default access type is published }
  563. current_object_option:=[sp_published];
  564. end;
  565. end;
  566. end;
  567. procedure setclassparent;
  568. begin
  569. if assigned(fd) then
  570. aktclass:=fd
  571. else
  572. aktclass:=tobjectdef.create(classtype,n,nil);
  573. { is the current class tobject? }
  574. { so you could define your own tobject }
  575. if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_class) and (upper(n)='TOBJECT') then
  576. class_tobject:=aktclass
  577. else if (cs_compilesystem in aktmoduleswitches) and (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  578. interface_iunknown:=aktclass
  579. else
  580. begin
  581. case classtype of
  582. odt_class:
  583. childof:=class_tobject;
  584. odt_interfacecom:
  585. childof:=interface_iunknown;
  586. end;
  587. if (oo_is_forward in childof.objectoptions) then
  588. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  589. aktclass.set_parent(childof);
  590. end;
  591. end;
  592. procedure setinterfacemethodoptions;
  593. var
  594. i: longint;
  595. defs: TIndexArray;
  596. pd: tprocdef;
  597. begin
  598. include(aktclass.objectoptions,oo_has_virtual);
  599. defs:=aktclass.symtable.defindex;
  600. for i:=1 to defs.count do
  601. begin
  602. pd:=tprocdef(defs.search(i));
  603. if pd.deftype=procdef then
  604. begin
  605. pd.extnumber:=aktclass.lastvtableindex;
  606. inc(aktclass.lastvtableindex);
  607. include(pd.procoptions,po_virtualmethod);
  608. pd.forwarddef:=false;
  609. end;
  610. end;
  611. end;
  612. function readobjecttype : boolean;
  613. begin
  614. readobjecttype:=true;
  615. { distinguish classes and objects }
  616. case token of
  617. _OBJECT:
  618. begin
  619. classtype:=odt_object;
  620. consume(_OBJECT)
  621. end;
  622. _CPPCLASS:
  623. begin
  624. classtype:=odt_cppclass;
  625. consume(_CPPCLASS);
  626. end;
  627. _INTERFACE:
  628. begin
  629. { need extra check here since interface is a keyword
  630. in all pascal modes }
  631. if not(m_class in aktmodeswitches) then
  632. Message(parser_f_need_objfpc_or_delphi_mode);
  633. if aktinterfacetype=it_interfacecom then
  634. classtype:=odt_interfacecom
  635. else {it_interfacecorba}
  636. classtype:=odt_interfacecorba;
  637. consume(_INTERFACE);
  638. { forward declaration }
  639. if not(assigned(fd)) and (token=_SEMICOLON) then
  640. begin
  641. { also anonym objects aren't allow (o : object a : longint; end;) }
  642. if n='' then
  643. Message(parser_f_no_anonym_objects);
  644. aktclass:=tobjectdef.create(classtype,n,nil);
  645. if (cs_compilesystem in aktmoduleswitches) and
  646. (classtype=odt_interfacecom) and (upper(n)='IUNKNOWN') then
  647. interface_iunknown:=aktclass;
  648. include(aktclass.objectoptions,oo_is_forward);
  649. object_dec:=aktclass;
  650. typecanbeforward:=storetypecanbeforward;
  651. readobjecttype:=false;
  652. exit;
  653. end;
  654. end;
  655. _CLASS:
  656. begin
  657. classtype:=odt_class;
  658. consume(_CLASS);
  659. if not(assigned(fd)) and
  660. (token=_OF) and
  661. { Delphi only allows class of in type blocks.
  662. Note that when parsing the type of a variable declaration
  663. the blocktype is bt_type so the check for typecanbeforward
  664. is also necessary (PFV) }
  665. (((block_type=bt_type) and typecanbeforward) or
  666. not(m_delphi in aktmodeswitches)) then
  667. begin
  668. { a hack, but it's easy to handle }
  669. { class reference type }
  670. consume(_OF);
  671. single_type(tt,hs,typecanbeforward);
  672. { accept hp1, if is a forward def or a class }
  673. if (tt.def.deftype=forwarddef) or
  674. is_class(tt.def) then
  675. begin
  676. pcrd:=tclassrefdef.create(tt);
  677. object_dec:=pcrd;
  678. end
  679. else
  680. begin
  681. object_dec:=generrortype.def;
  682. Message1(type_e_class_type_expected,generrortype.def.typename);
  683. end;
  684. typecanbeforward:=storetypecanbeforward;
  685. readobjecttype:=false;
  686. exit;
  687. end
  688. { forward class }
  689. else if not(assigned(fd)) and (token=_SEMICOLON) then
  690. begin
  691. { also anonym objects aren't allow (o : object a : longint; end;) }
  692. if n='' then
  693. Message(parser_f_no_anonym_objects);
  694. aktclass:=tobjectdef.create(odt_class,n,nil);
  695. if (cs_compilesystem in aktmoduleswitches) and (upper(n)='TOBJECT') then
  696. class_tobject:=aktclass;
  697. aktclass.objecttype:=odt_class;
  698. include(aktclass.objectoptions,oo_is_forward);
  699. { all classes must have a vmt !! at offset zero }
  700. if not(oo_has_vmt in aktclass.objectoptions) then
  701. aktclass.insertvmt;
  702. object_dec:=aktclass;
  703. typecanbeforward:=storetypecanbeforward;
  704. readobjecttype:=false;
  705. exit;
  706. end;
  707. end;
  708. else
  709. begin
  710. classtype:=odt_class; { this is error but try to recover }
  711. consume(_OBJECT);
  712. end;
  713. end;
  714. end;
  715. procedure handleimplementedinterface(implintf : tobjectdef);
  716. begin
  717. if not is_interface(implintf) then
  718. begin
  719. Message1(type_e_interface_type_expected,implintf.typename);
  720. exit;
  721. end;
  722. if aktclass.implementedinterfaces.searchintf(implintf)<>-1 then
  723. Message1(sym_e_duplicate_id,implintf.name)
  724. else
  725. begin
  726. { allocate and prepare the GUID only if the class
  727. implements some interfaces.
  728. }
  729. if aktclass.implementedinterfaces.count = 0 then
  730. aktclass.prepareguid;
  731. aktclass.implementedinterfaces.addintf(implintf);
  732. end;
  733. end;
  734. procedure readimplementedinterfaces;
  735. var
  736. tt : ttype;
  737. begin
  738. while try_to_consume(_COMMA) do
  739. begin
  740. id_type(tt,pattern,false);
  741. if (tt.def.deftype<>objectdef) then
  742. begin
  743. Message1(type_e_interface_type_expected,tt.def.typename);
  744. continue;
  745. end;
  746. handleimplementedinterface(tobjectdef(tt.def));
  747. end;
  748. end;
  749. procedure readinterfaceiid;
  750. var
  751. p : tnode;
  752. valid : boolean;
  753. begin
  754. p:=comp_expr(true);
  755. if p.nodetype=stringconstn then
  756. begin
  757. stringdispose(aktclass.iidstr);
  758. aktclass.iidstr:=stringdup(strpas(tstringconstnode(p).value_str)); { or upper? }
  759. p.free;
  760. valid:=string2guid(aktclass.iidstr^,aktclass.iidguid^);
  761. if (classtype=odt_interfacecom) and not assigned(aktclass.iidguid) and not valid then
  762. Message(parser_e_improper_guid_syntax);
  763. end
  764. else
  765. begin
  766. p.free;
  767. Message(cg_e_illegal_expression);
  768. end;
  769. end;
  770. procedure readparentclasses;
  771. var
  772. hp : tobjectdef;
  773. begin
  774. hp:=nil;
  775. { reads the parent class }
  776. if token=_LKLAMMER then
  777. begin
  778. consume(_LKLAMMER);
  779. id_type(tt,pattern,false);
  780. childof:=tobjectdef(tt.def);
  781. if (not assigned(childof)) or
  782. (childof.deftype<>objectdef) then
  783. begin
  784. if assigned(childof) then
  785. Message1(type_e_class_type_expected,childof.typename);
  786. childof:=nil;
  787. aktclass:=tobjectdef.create(classtype,n,nil);
  788. end
  789. else
  790. begin
  791. { a mix of class, interfaces, objects and cppclasses
  792. isn't allowed }
  793. case classtype of
  794. odt_class:
  795. if not(is_class(childof)) then
  796. begin
  797. if is_interface(childof) then
  798. begin
  799. { we insert the interface after the child
  800. is set, see below
  801. }
  802. hp:=childof;
  803. childof:=class_tobject;
  804. end
  805. else
  806. Message(parser_e_mix_of_classes_and_objects);
  807. end;
  808. odt_interfacecorba,
  809. odt_interfacecom:
  810. if not(is_interface(childof)) then
  811. Message(parser_e_mix_of_classes_and_objects);
  812. odt_cppclass:
  813. if not(is_cppclass(childof)) then
  814. Message(parser_e_mix_of_classes_and_objects);
  815. odt_object:
  816. if not(is_object(childof)) then
  817. Message(parser_e_mix_of_classes_and_objects);
  818. end;
  819. { the forward of the child must be resolved to get
  820. correct field addresses }
  821. if assigned(fd) then
  822. begin
  823. if (oo_is_forward in childof.objectoptions) then
  824. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  825. aktclass:=fd;
  826. { we must inherit several options !!
  827. this was missing !!
  828. all is now done in set_parent
  829. including symtable datasize setting PM }
  830. fd.set_parent(childof);
  831. end
  832. else
  833. aktclass:=tobjectdef.create(classtype,n,childof);
  834. if aktclass.objecttype=odt_class then
  835. begin
  836. if assigned(hp) then
  837. handleimplementedinterface(hp);
  838. readimplementedinterfaces;
  839. end;
  840. end;
  841. consume(_RKLAMMER);
  842. end
  843. { if no parent class, then a class get tobject as parent }
  844. else if classtype in [odt_class,odt_interfacecom] then
  845. setclassparent
  846. else
  847. aktclass:=tobjectdef.create(classtype,n,nil);
  848. { read GUID }
  849. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  850. try_to_consume(_LECKKLAMMER) then
  851. begin
  852. readinterfaceiid;
  853. consume(_RECKKLAMMER);
  854. end;
  855. end;
  856. procedure chkcpp(pd:tprocdef);
  857. begin
  858. if is_cppclass(pd._class) then
  859. begin
  860. pd.proccalloption:=pocall_cppdecl;
  861. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  862. end;
  863. end;
  864. var
  865. pd : tprocdef;
  866. begin
  867. old_object_option:=current_object_option;
  868. { forward is resolved }
  869. if assigned(fd) then
  870. exclude(fd.objectoptions,oo_is_forward);
  871. { objects and class types can't be declared local }
  872. if not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) then
  873. Message(parser_e_no_local_objects);
  874. storetypecanbeforward:=typecanbeforward;
  875. { for tp7 don't allow forward types }
  876. if (m_tp7 in aktmodeswitches) then
  877. typecanbeforward:=false;
  878. if not(readobjecttype) then
  879. exit;
  880. { also anonym objects aren't allow (o : object a : longint; end;) }
  881. if n='' then
  882. Message(parser_f_no_anonym_objects);
  883. { read list of parent classes }
  884. readparentclasses;
  885. { default access is public }
  886. there_is_a_destructor:=false;
  887. current_object_option:=[sp_public];
  888. { set class flags and inherits published }
  889. setclassattributes;
  890. aktobjectdef:=aktclass;
  891. aktclass.symtable.next:=symtablestack;
  892. symtablestack:=aktclass.symtable;
  893. testcurobject:=1;
  894. curobjectname:=Upper(n);
  895. { temp procinfo }
  896. old_current_procinfo:=current_procinfo;
  897. current_procinfo:=cprocinfo.create(nil);
  898. { short class declaration ? }
  899. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  900. begin
  901. { Parse componenten }
  902. repeat
  903. case token of
  904. _ID :
  905. begin
  906. case idtoken of
  907. _PRIVATE :
  908. begin
  909. if is_interface(aktclass) then
  910. Message(parser_e_no_access_specifier_in_interfaces);
  911. consume(_PRIVATE);
  912. current_object_option:=[sp_private];
  913. include(aktclass.objectoptions,oo_has_private);
  914. end;
  915. _PROTECTED :
  916. begin
  917. if is_interface(aktclass) then
  918. Message(parser_e_no_access_specifier_in_interfaces);
  919. consume(_PROTECTED);
  920. current_object_option:=[sp_protected];
  921. include(aktclass.objectoptions,oo_has_protected);
  922. end;
  923. _PUBLIC :
  924. begin
  925. if is_interface(aktclass) then
  926. Message(parser_e_no_access_specifier_in_interfaces);
  927. consume(_PUBLIC);
  928. current_object_option:=[sp_public];
  929. end;
  930. _PUBLISHED :
  931. begin
  932. { we've to check for a pushlished section in non- }
  933. { publishable classes later, if a real declaration }
  934. { this is the way, delphi does it }
  935. if is_interface(aktclass) then
  936. Message(parser_e_no_access_specifier_in_interfaces);
  937. consume(_PUBLISHED);
  938. current_object_option:=[sp_published];
  939. end;
  940. else
  941. begin
  942. if is_interface(aktclass) then
  943. Message(parser_e_no_vars_in_interfaces);
  944. if (sp_published in current_object_option) and
  945. not(oo_can_have_published in aktclass.objectoptions) then
  946. Message(parser_e_cant_have_published);
  947. read_var_decs(false,true,false);
  948. end;
  949. end;
  950. end;
  951. _PROPERTY :
  952. begin
  953. property_dec;
  954. end;
  955. _PROCEDURE,
  956. _FUNCTION,
  957. _CLASS :
  958. begin
  959. if (sp_published in current_object_option) and
  960. not(oo_can_have_published in aktclass.objectoptions) then
  961. Message(parser_e_cant_have_published);
  962. oldparse_only:=parse_only;
  963. parse_only:=true;
  964. pd:=parse_proc_dec(aktclass);
  965. { this is for error recovery as well as forward }
  966. { interface mappings, i.e. mapping to a method }
  967. { which isn't declared yet }
  968. if assigned(pd) then
  969. begin
  970. parse_object_proc_directives(pd);
  971. calc_parast(pd);
  972. { add definition to procsym }
  973. proc_add_definition(pd);
  974. { add procdef options to objectdef options }
  975. if (po_msgint in pd.procoptions) then
  976. include(aktclass.objectoptions,oo_has_msgint);
  977. if (po_msgstr in pd.procoptions) then
  978. include(aktclass.objectoptions,oo_has_msgstr);
  979. if (po_virtualmethod in pd.procoptions) then
  980. include(aktclass.objectoptions,oo_has_virtual);
  981. chkcpp(pd);
  982. end;
  983. parse_only:=oldparse_only;
  984. end;
  985. _CONSTRUCTOR :
  986. begin
  987. if (sp_published in current_object_option) and
  988. not(oo_can_have_published in aktclass.objectoptions) then
  989. Message(parser_e_cant_have_published);
  990. if not(sp_public in current_object_option) then
  991. Message(parser_w_constructor_should_be_public);
  992. if is_interface(aktclass) then
  993. Message(parser_e_no_con_des_in_interfaces);
  994. oldparse_only:=parse_only;
  995. parse_only:=true;
  996. pd:=constructor_head;
  997. parse_object_proc_directives(pd);
  998. calc_parast(pd);
  999. { add definition to procsym }
  1000. proc_add_definition(pd);
  1001. { add procdef options to objectdef options }
  1002. if (po_virtualmethod in pd.procoptions) then
  1003. include(aktclass.objectoptions,oo_has_virtual);
  1004. chkcpp(pd);
  1005. parse_only:=oldparse_only;
  1006. end;
  1007. _DESTRUCTOR :
  1008. begin
  1009. if (sp_published in current_object_option) and
  1010. not(oo_can_have_published in aktclass.objectoptions) then
  1011. Message(parser_e_cant_have_published);
  1012. if there_is_a_destructor then
  1013. Message(parser_n_only_one_destructor);
  1014. if is_interface(aktclass) then
  1015. Message(parser_e_no_con_des_in_interfaces);
  1016. if not(sp_public in current_object_option) then
  1017. Message(parser_w_destructor_should_be_public);
  1018. there_is_a_destructor:=true;
  1019. oldparse_only:=parse_only;
  1020. parse_only:=true;
  1021. pd:=destructor_head;
  1022. parse_object_proc_directives(pd);
  1023. calc_parast(pd);
  1024. { add definition to procsym }
  1025. proc_add_definition(pd);
  1026. { add procdef options to objectdef options }
  1027. if (po_virtualmethod in pd.procoptions) then
  1028. include(aktclass.objectoptions,oo_has_virtual);
  1029. chkcpp(pd);
  1030. parse_only:=oldparse_only;
  1031. end;
  1032. _END :
  1033. 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. end;
  1042. { generate vmt space if needed }
  1043. if not(oo_has_vmt in aktclass.objectoptions) and
  1044. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  1045. (classtype in [odt_class])
  1046. ) then
  1047. aktclass.insertvmt;
  1048. if is_interface(aktclass) then
  1049. setinterfacemethodoptions;
  1050. { reset }
  1051. testcurobject:=0;
  1052. curobjectname:='';
  1053. typecanbeforward:=storetypecanbeforward;
  1054. { restore old state }
  1055. symtablestack:=symtablestack.next;
  1056. aktobjectdef:=nil;
  1057. {Restore procinfo}
  1058. current_procinfo.free;
  1059. current_procinfo:=old_current_procinfo;
  1060. current_object_option:=old_object_option;
  1061. object_dec:=aktclass;
  1062. end;
  1063. end.
  1064. {
  1065. $Log$
  1066. Revision 1.65 2003-05-09 17:47:02 peter
  1067. * self moved to hidden parameter
  1068. * removed hdisposen,hnewn,selfn
  1069. Revision 1.64 2003/05/05 14:53:16 peter
  1070. * vs_hidden replaced by is_hidden boolean
  1071. Revision 1.63 2003/04/27 11:21:33 peter
  1072. * aktprocdef renamed to current_procdef
  1073. * procinfo renamed to current_procinfo
  1074. * procinfo will now be stored in current_module so it can be
  1075. cleaned up properly
  1076. * gen_main_procsym changed to create_main_proc and release_main_proc
  1077. to also generate a tprocinfo structure
  1078. * fixed unit implicit initfinal
  1079. Revision 1.62 2003/04/27 07:29:50 peter
  1080. * current_procdef cleanup, current_procdef is now always nil when parsing
  1081. a new procdef declaration
  1082. * aktprocsym removed
  1083. * lexlevel removed, use symtable.symtablelevel instead
  1084. * implicit init/final code uses the normal genentry/genexit
  1085. * funcret state checking updated for new funcret handling
  1086. Revision 1.61 2003/04/26 00:32:37 peter
  1087. * start search for overriden properties in the parent class
  1088. Revision 1.60 2003/04/25 20:59:33 peter
  1089. * removed funcretn,funcretsym, function result is now in varsym
  1090. and aliases for result and function name are added using absolutesym
  1091. * vs_hidden parameter for funcret passed in parameter
  1092. * vs_hidden fixes
  1093. * writenode changed to printnode and released from extdebug
  1094. * -vp option added to generate a tree.log with the nodetree
  1095. * nicer printnode for statements, callnode
  1096. Revision 1.59 2003/04/10 17:57:52 peter
  1097. * vs_hidden released
  1098. Revision 1.58 2003/01/09 21:52:37 peter
  1099. * merged some verbosity options.
  1100. * V_LineInfo is a verbosity flag to include line info
  1101. Revision 1.57 2002/11/25 17:43:21 peter
  1102. * splitted defbase in defutil,symutil,defcmp
  1103. * merged isconvertable and is_equal into compare_defs(_ext)
  1104. * made operator search faster by walking the list only once
  1105. Revision 1.56 2002/11/17 16:31:56 carl
  1106. * memory optimization (3-4%) : cleanup of tai fields,
  1107. cleanup of tdef and tsym fields.
  1108. * make it work for m68k
  1109. Revision 1.55 2002/10/05 12:43:25 carl
  1110. * fixes for Delphi 6 compilation
  1111. (warning : Some features do not work under Delphi)
  1112. Revision 1.54 2002/10/02 18:20:20 peter
  1113. * don't allow interface without m_class mode
  1114. Revision 1.53 2002/09/27 21:13:28 carl
  1115. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  1116. Revision 1.52 2002/09/16 14:11:13 peter
  1117. * add argument to equal_paras() to support default values or not
  1118. Revision 1.51 2002/09/09 17:34:15 peter
  1119. * tdicationary.replace added to replace and item in a dictionary. This
  1120. is only allowed for the same name
  1121. * varsyms are inserted in symtable before the types are parsed. This
  1122. fixes the long standing "var longint : longint" bug
  1123. - consume_idlist and idstringlist removed. The loops are inserted
  1124. at the callers place and uses the symtable for duplicate id checking
  1125. Revision 1.50 2002/09/03 16:26:26 daniel
  1126. * Make Tprocdef.defs protected
  1127. Revision 1.49 2002/08/17 09:23:38 florian
  1128. * first part of procinfo rewrite
  1129. Revision 1.48 2002/08/09 07:33:02 florian
  1130. * a couple of interface related fixes
  1131. Revision 1.47 2002/07/20 11:57:55 florian
  1132. * types.pas renamed to defbase.pas because D6 contains a types
  1133. unit so this would conflicts if D6 programms are compiled
  1134. + Willamette/SSE2 instructions to assembler added
  1135. Revision 1.46 2002/07/01 16:23:53 peter
  1136. * cg64 patch
  1137. * basics for currency
  1138. * asnode updates for class and interface (not finished)
  1139. Revision 1.45 2002/05/18 13:34:12 peter
  1140. * readded missing revisions
  1141. Revision 1.44 2002/05/16 19:46:42 carl
  1142. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1143. + try to fix temp allocation (still in ifdef)
  1144. + generic constructor calls
  1145. + start of tassembler / tmodulebase class cleanup
  1146. Revision 1.42 2002/05/12 16:53:08 peter
  1147. * moved entry and exitcode to ncgutil and cgobj
  1148. * foreach gets extra argument for passing local data to the
  1149. iterator function
  1150. * -CR checks also class typecasts at runtime by changing them
  1151. into as
  1152. * fixed compiler to cycle with the -CR option
  1153. * fixed stabs with elf writer, finally the global variables can
  1154. be watched
  1155. * removed a lot of routines from cga unit and replaced them by
  1156. calls to cgobj
  1157. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1158. u32bit then the other is typecasted also to u32bit without giving
  1159. a rangecheck warning/error.
  1160. * fixed pascal calling method with reversing also the high tree in
  1161. the parast, detected by tcalcst3 test
  1162. Revision 1.41 2002/04/21 19:02:04 peter
  1163. * removed newn and disposen nodes, the code is now directly
  1164. inlined from pexpr
  1165. * -an option that will write the secondpass nodes to the .s file, this
  1166. requires EXTDEBUG define to actually write the info
  1167. * fixed various internal errors and crashes due recent code changes
  1168. Revision 1.40 2002/04/19 15:46:02 peter
  1169. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1170. in most cases and not written to the ppu
  1171. * add mangeledname_prefix() routine to generate the prefix of
  1172. manglednames depending on the current procedure, object and module
  1173. * removed static procprefix since the mangledname is now build only
  1174. on demand from tprocdef.mangledname
  1175. Revision 1.39 2002/04/04 19:06:00 peter
  1176. * removed unused units
  1177. * use tlocation.size in cg.a_*loc*() routines
  1178. Revision 1.38 2002/01/25 17:38:19 peter
  1179. * fixed default value for properties with index values
  1180. Revision 1.37 2002/01/24 18:25:48 peter
  1181. * implicit result variable generation for assembler routines
  1182. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1183. Revision 1.36 2002/01/06 12:08:15 peter
  1184. * removed uauto from orddef, use new range_to_basetype generating
  1185. the correct ordinal type for a range
  1186. }