pdecobj.pas 51 KB

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