pdecobj.pas 50 KB

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