2
0

pdecobj.pas 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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,[cpo_allowdefaults]);
  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. IncompatibleTypes(def,p.proptype.def);
  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,[cpo_allowdefaults]);
  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. IncompatibleTypes(def,p.proptype.def);
  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 try_to_consume(_LKLAMMER) then
  794. begin
  795. id_type(tt,pattern,false);
  796. childof:=tobjectdef(tt.def);
  797. if (not assigned(childof)) or
  798. (childof.deftype<>objectdef) then
  799. begin
  800. if assigned(childof) then
  801. Message1(type_e_class_type_expected,childof.typename);
  802. childof:=nil;
  803. aktclass:=tobjectdef.create(classtype,n,nil);
  804. end
  805. else
  806. begin
  807. { a mix of class, interfaces, objects and cppclasses
  808. isn't allowed }
  809. case classtype of
  810. odt_class:
  811. if not(is_class(childof)) then
  812. begin
  813. if is_interface(childof) then
  814. begin
  815. { we insert the interface after the child
  816. is set, see below
  817. }
  818. hp:=childof;
  819. childof:=class_tobject;
  820. end
  821. else
  822. Message(parser_e_mix_of_classes_and_objects);
  823. end;
  824. odt_interfacecorba,
  825. odt_interfacecom:
  826. if not(is_interface(childof)) then
  827. Message(parser_e_mix_of_classes_and_objects);
  828. odt_cppclass:
  829. if not(is_cppclass(childof)) then
  830. Message(parser_e_mix_of_classes_and_objects);
  831. odt_object:
  832. if not(is_object(childof)) then
  833. Message(parser_e_mix_of_classes_and_objects);
  834. end;
  835. { the forward of the child must be resolved to get
  836. correct field addresses }
  837. if assigned(fd) then
  838. begin
  839. if (oo_is_forward in childof.objectoptions) then
  840. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  841. aktclass:=fd;
  842. { we must inherit several options !!
  843. this was missing !!
  844. all is now done in set_parent
  845. including symtable datasize setting PM }
  846. fd.set_parent(childof);
  847. end
  848. else
  849. aktclass:=tobjectdef.create(classtype,n,childof);
  850. if aktclass.objecttype=odt_class then
  851. begin
  852. if assigned(hp) then
  853. handleimplementedinterface(hp);
  854. readimplementedinterfaces;
  855. end;
  856. end;
  857. consume(_RKLAMMER);
  858. end
  859. { if no parent class, then a class get tobject as parent }
  860. else if classtype in [odt_class,odt_interfacecom] then
  861. setclassparent
  862. else
  863. aktclass:=tobjectdef.create(classtype,n,nil);
  864. { read GUID }
  865. if (classtype in [odt_interfacecom,odt_interfacecorba]) and
  866. try_to_consume(_LECKKLAMMER) then
  867. begin
  868. readinterfaceiid;
  869. consume(_RECKKLAMMER);
  870. end;
  871. end;
  872. procedure chkcpp(pd:tprocdef);
  873. begin
  874. if is_cppclass(pd._class) then
  875. begin
  876. pd.proccalloption:=pocall_cppdecl;
  877. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  878. end;
  879. end;
  880. var
  881. pd : tprocdef;
  882. begin
  883. old_object_option:=current_object_option;
  884. { forward is resolved }
  885. if assigned(fd) then
  886. exclude(fd.objectoptions,oo_is_forward);
  887. { objects and class types can't be declared local }
  888. if not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) then
  889. Message(parser_e_no_local_objects);
  890. storetypecanbeforward:=typecanbeforward;
  891. { for tp7 don't allow forward types }
  892. if (m_tp7 in aktmodeswitches) then
  893. typecanbeforward:=false;
  894. if not(readobjecttype) then
  895. exit;
  896. { also anonym objects aren't allow (o : object a : longint; end;) }
  897. if n='' then
  898. Message(parser_f_no_anonym_objects);
  899. { read list of parent classes }
  900. readparentclasses;
  901. { default access is public }
  902. there_is_a_destructor:=false;
  903. current_object_option:=[sp_public];
  904. { set class flags and inherits published }
  905. setclassattributes;
  906. aktobjectdef:=aktclass;
  907. aktclass.symtable.next:=symtablestack;
  908. symtablestack:=aktclass.symtable;
  909. testcurobject:=1;
  910. curobjectname:=Upper(n);
  911. { short class declaration ? }
  912. if (classtype<>odt_class) or (token<>_SEMICOLON) then
  913. begin
  914. { Parse componenten }
  915. repeat
  916. case token of
  917. _ID :
  918. begin
  919. case idtoken of
  920. _PRIVATE :
  921. begin
  922. if is_interface(aktclass) then
  923. Message(parser_e_no_access_specifier_in_interfaces);
  924. consume(_PRIVATE);
  925. current_object_option:=[sp_private];
  926. include(aktclass.objectoptions,oo_has_private);
  927. end;
  928. _PROTECTED :
  929. begin
  930. if is_interface(aktclass) then
  931. Message(parser_e_no_access_specifier_in_interfaces);
  932. consume(_PROTECTED);
  933. current_object_option:=[sp_protected];
  934. include(aktclass.objectoptions,oo_has_protected);
  935. end;
  936. _PUBLIC :
  937. begin
  938. if is_interface(aktclass) then
  939. Message(parser_e_no_access_specifier_in_interfaces);
  940. consume(_PUBLIC);
  941. current_object_option:=[sp_public];
  942. end;
  943. _PUBLISHED :
  944. begin
  945. { we've to check for a pushlished section in non- }
  946. { publishable classes later, if a real declaration }
  947. { this is the way, delphi does it }
  948. if is_interface(aktclass) then
  949. Message(parser_e_no_access_specifier_in_interfaces);
  950. consume(_PUBLISHED);
  951. current_object_option:=[sp_published];
  952. end;
  953. else
  954. begin
  955. if is_interface(aktclass) then
  956. Message(parser_e_no_vars_in_interfaces);
  957. if (sp_published in current_object_option) and
  958. not(oo_can_have_published in aktclass.objectoptions) then
  959. Message(parser_e_cant_have_published);
  960. read_var_decs(false,true,false);
  961. end;
  962. end;
  963. end;
  964. _PROPERTY :
  965. begin
  966. property_dec;
  967. end;
  968. _PROCEDURE,
  969. _FUNCTION,
  970. _CLASS :
  971. begin
  972. if (sp_published in current_object_option) and
  973. not(oo_can_have_published in aktclass.objectoptions) then
  974. Message(parser_e_cant_have_published);
  975. oldparse_only:=parse_only;
  976. parse_only:=true;
  977. pd:=parse_proc_dec(aktclass);
  978. { this is for error recovery as well as forward }
  979. { interface mappings, i.e. mapping to a method }
  980. { which isn't declared yet }
  981. if assigned(pd) then
  982. begin
  983. parse_object_proc_directives(pd);
  984. handle_calling_convention(pd);
  985. calc_parast(pd);
  986. { add definition to procsym }
  987. proc_add_definition(pd);
  988. { add procdef options to objectdef options }
  989. if (po_msgint in pd.procoptions) then
  990. include(aktclass.objectoptions,oo_has_msgint);
  991. if (po_msgstr in pd.procoptions) then
  992. include(aktclass.objectoptions,oo_has_msgstr);
  993. if (po_virtualmethod in pd.procoptions) then
  994. include(aktclass.objectoptions,oo_has_virtual);
  995. chkcpp(pd);
  996. end;
  997. parse_only:=oldparse_only;
  998. end;
  999. _CONSTRUCTOR :
  1000. begin
  1001. if (sp_published in current_object_option) and
  1002. not(oo_can_have_published in aktclass.objectoptions) then
  1003. Message(parser_e_cant_have_published);
  1004. if not(sp_public in current_object_option) then
  1005. Message(parser_w_constructor_should_be_public);
  1006. if is_interface(aktclass) then
  1007. Message(parser_e_no_con_des_in_interfaces);
  1008. oldparse_only:=parse_only;
  1009. parse_only:=true;
  1010. pd:=constructor_head;
  1011. parse_object_proc_directives(pd);
  1012. handle_calling_convention(pd);
  1013. calc_parast(pd);
  1014. { add definition to procsym }
  1015. proc_add_definition(pd);
  1016. { add procdef options to objectdef options }
  1017. if (po_virtualmethod in pd.procoptions) then
  1018. include(aktclass.objectoptions,oo_has_virtual);
  1019. chkcpp(pd);
  1020. parse_only:=oldparse_only;
  1021. end;
  1022. _DESTRUCTOR :
  1023. begin
  1024. if (sp_published in current_object_option) and
  1025. not(oo_can_have_published in aktclass.objectoptions) then
  1026. Message(parser_e_cant_have_published);
  1027. if there_is_a_destructor then
  1028. Message(parser_n_only_one_destructor);
  1029. if is_interface(aktclass) then
  1030. Message(parser_e_no_con_des_in_interfaces);
  1031. if not(sp_public in current_object_option) then
  1032. Message(parser_w_destructor_should_be_public);
  1033. there_is_a_destructor:=true;
  1034. oldparse_only:=parse_only;
  1035. parse_only:=true;
  1036. pd:=destructor_head;
  1037. parse_object_proc_directives(pd);
  1038. handle_calling_convention(pd);
  1039. calc_parast(pd);
  1040. { add definition to procsym }
  1041. proc_add_definition(pd);
  1042. { add procdef options to objectdef options }
  1043. if (po_virtualmethod in pd.procoptions) then
  1044. include(aktclass.objectoptions,oo_has_virtual);
  1045. chkcpp(pd);
  1046. parse_only:=oldparse_only;
  1047. end;
  1048. _END :
  1049. begin
  1050. consume(_END);
  1051. break;
  1052. end;
  1053. else
  1054. consume(_ID); { Give a ident expected message, like tp7 }
  1055. end;
  1056. until false;
  1057. end;
  1058. { generate vmt space if needed }
  1059. if not(oo_has_vmt in aktclass.objectoptions) and
  1060. (([oo_has_virtual,oo_has_constructor,oo_has_destructor]*aktclass.objectoptions<>[]) or
  1061. (classtype in [odt_class])
  1062. ) then
  1063. aktclass.insertvmt;
  1064. if is_interface(aktclass) then
  1065. setinterfacemethodoptions;
  1066. { reset }
  1067. testcurobject:=0;
  1068. curobjectname:='';
  1069. typecanbeforward:=storetypecanbeforward;
  1070. { restore old state }
  1071. symtablestack:=symtablestack.next;
  1072. aktobjectdef:=nil;
  1073. current_object_option:=old_object_option;
  1074. object_dec:=aktclass;
  1075. end;
  1076. end.
  1077. {
  1078. $Log$
  1079. Revision 1.72 2003-10-30 16:23:13 peter
  1080. * don't search for overloads in parents for constructors
  1081. Revision 1.71 2003/10/22 15:22:33 peter
  1082. * fixed unitsym-globalsymtable relation so the uses of a unit
  1083. is counted correctly
  1084. Revision 1.70 2003/10/21 18:16:13 peter
  1085. * IncompatibleTypes() added that will include unit names when
  1086. the typenames are the same
  1087. Revision 1.69 2003/10/07 16:06:30 peter
  1088. * tsymlist.def renamed to tsymlist.procdef
  1089. * tsymlist.procdef is now only used to store the procdef
  1090. Revision 1.68 2003/10/02 21:15:12 peter
  1091. * support nil as default value
  1092. * when no default property is allowed don't check default value
  1093. Revision 1.67 2003/06/13 21:19:30 peter
  1094. * current_procdef removed, use current_procinfo.procdef instead
  1095. Revision 1.66 2003/05/23 14:27:35 peter
  1096. * remove some unit dependencies
  1097. * current_procinfo changes to store more info
  1098. Revision 1.65 2003/05/09 17:47:02 peter
  1099. * self moved to hidden parameter
  1100. * removed hdisposen,hnewn,selfn
  1101. Revision 1.64 2003/05/05 14:53:16 peter
  1102. * vs_hidden replaced by is_hidden boolean
  1103. Revision 1.63 2003/04/27 11:21:33 peter
  1104. * aktprocdef renamed to current_procinfo.procdef
  1105. * procinfo renamed to current_procinfo
  1106. * procinfo will now be stored in current_module so it can be
  1107. cleaned up properly
  1108. * gen_main_procsym changed to create_main_proc and release_main_proc
  1109. to also generate a tprocinfo structure
  1110. * fixed unit implicit initfinal
  1111. Revision 1.62 2003/04/27 07:29:50 peter
  1112. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1113. a new procdef declaration
  1114. * aktprocsym removed
  1115. * lexlevel removed, use symtable.symtablelevel instead
  1116. * implicit init/final code uses the normal genentry/genexit
  1117. * funcret state checking updated for new funcret handling
  1118. Revision 1.61 2003/04/26 00:32:37 peter
  1119. * start search for overriden properties in the parent class
  1120. Revision 1.60 2003/04/25 20:59:33 peter
  1121. * removed funcretn,funcretsym, function result is now in varsym
  1122. and aliases for result and function name are added using absolutesym
  1123. * vs_hidden parameter for funcret passed in parameter
  1124. * vs_hidden fixes
  1125. * writenode changed to printnode and released from extdebug
  1126. * -vp option added to generate a tree.log with the nodetree
  1127. * nicer printnode for statements, callnode
  1128. Revision 1.59 2003/04/10 17:57:52 peter
  1129. * vs_hidden released
  1130. Revision 1.58 2003/01/09 21:52:37 peter
  1131. * merged some verbosity options.
  1132. * V_LineInfo is a verbosity flag to include line info
  1133. Revision 1.57 2002/11/25 17:43:21 peter
  1134. * splitted defbase in defutil,symutil,defcmp
  1135. * merged isconvertable and is_equal into compare_defs(_ext)
  1136. * made operator search faster by walking the list only once
  1137. Revision 1.56 2002/11/17 16:31:56 carl
  1138. * memory optimization (3-4%) : cleanup of tai fields,
  1139. cleanup of tdef and tsym fields.
  1140. * make it work for m68k
  1141. Revision 1.55 2002/10/05 12:43:25 carl
  1142. * fixes for Delphi 6 compilation
  1143. (warning : Some features do not work under Delphi)
  1144. Revision 1.54 2002/10/02 18:20:20 peter
  1145. * don't allow interface without m_class mode
  1146. Revision 1.53 2002/09/27 21:13:28 carl
  1147. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  1148. Revision 1.52 2002/09/16 14:11:13 peter
  1149. * add argument to equal_paras() to support default values or not
  1150. Revision 1.51 2002/09/09 17:34:15 peter
  1151. * tdicationary.replace added to replace and item in a dictionary. This
  1152. is only allowed for the same name
  1153. * varsyms are inserted in symtable before the types are parsed. This
  1154. fixes the long standing "var longint : longint" bug
  1155. - consume_idlist and idstringlist removed. The loops are inserted
  1156. at the callers place and uses the symtable for duplicate id checking
  1157. Revision 1.50 2002/09/03 16:26:26 daniel
  1158. * Make Tprocdef.defs protected
  1159. Revision 1.49 2002/08/17 09:23:38 florian
  1160. * first part of procinfo rewrite
  1161. Revision 1.48 2002/08/09 07:33:02 florian
  1162. * a couple of interface related fixes
  1163. Revision 1.47 2002/07/20 11:57:55 florian
  1164. * types.pas renamed to defbase.pas because D6 contains a types
  1165. unit so this would conflicts if D6 programms are compiled
  1166. + Willamette/SSE2 instructions to assembler added
  1167. Revision 1.46 2002/07/01 16:23:53 peter
  1168. * cg64 patch
  1169. * basics for currency
  1170. * asnode updates for class and interface (not finished)
  1171. Revision 1.45 2002/05/18 13:34:12 peter
  1172. * readded missing revisions
  1173. Revision 1.44 2002/05/16 19:46:42 carl
  1174. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1175. + try to fix temp allocation (still in ifdef)
  1176. + generic constructor calls
  1177. + start of tassembler / tmodulebase class cleanup
  1178. Revision 1.42 2002/05/12 16:53:08 peter
  1179. * moved entry and exitcode to ncgutil and cgobj
  1180. * foreach gets extra argument for passing local data to the
  1181. iterator function
  1182. * -CR checks also class typecasts at runtime by changing them
  1183. into as
  1184. * fixed compiler to cycle with the -CR option
  1185. * fixed stabs with elf writer, finally the global variables can
  1186. be watched
  1187. * removed a lot of routines from cga unit and replaced them by
  1188. calls to cgobj
  1189. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1190. u32bit then the other is typecasted also to u32bit without giving
  1191. a rangecheck warning/error.
  1192. * fixed pascal calling method with reversing also the high tree in
  1193. the parast, detected by tcalcst3 test
  1194. Revision 1.41 2002/04/21 19:02:04 peter
  1195. * removed newn and disposen nodes, the code is now directly
  1196. inlined from pexpr
  1197. * -an option that will write the secondpass nodes to the .s file, this
  1198. requires EXTDEBUG define to actually write the info
  1199. * fixed various internal errors and crashes due recent code changes
  1200. Revision 1.40 2002/04/19 15:46:02 peter
  1201. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1202. in most cases and not written to the ppu
  1203. * add mangeledname_prefix() routine to generate the prefix of
  1204. manglednames depending on the current procedure, object and module
  1205. * removed static procprefix since the mangledname is now build only
  1206. on demand from tprocdef.mangledname
  1207. Revision 1.39 2002/04/04 19:06:00 peter
  1208. * removed unused units
  1209. * use tlocation.size in cg.a_*loc*() routines
  1210. Revision 1.38 2002/01/25 17:38:19 peter
  1211. * fixed default value for properties with index values
  1212. Revision 1.37 2002/01/24 18:25:48 peter
  1213. * implicit result variable generation for assembler routines
  1214. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1215. Revision 1.36 2002/01/06 12:08:15 peter
  1216. * removed uauto from orddef, use new range_to_basetype generating
  1217. the correct ordinal type for a range
  1218. }