pdecobj.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does object types for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecobj;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,symtype,symdef;
  23. { parses a object declaration }
  24. function object_dec(const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tdef;
  25. implementation
  26. uses
  27. cutils,
  28. globals,verbose,systems,tokens,
  29. symconst,symbase,symsym,symtable,
  30. node,nld,nmem,ncon,ncnv,ncal,
  31. scanner,
  32. pbase,pexpr,pdecsub,pdecvar,ptype,pdecl
  33. ;
  34. const
  35. { Please leave this here, this module should NOT use
  36. these variables.
  37. Declaring it as string here results in an error when compiling (PFV) }
  38. current_procinfo = 'error';
  39. function constructor_head:tprocdef;
  40. var
  41. pd : tprocdef;
  42. begin
  43. result:=nil;
  44. consume(_CONSTRUCTOR);
  45. { must be at same level as in implementation }
  46. parse_proc_head(current_objectdef,potype_constructor,pd);
  47. if not assigned(pd) then
  48. begin
  49. consume(_SEMICOLON);
  50. exit;
  51. end;
  52. if (cs_constructor_name in current_settings.globalswitches) and
  53. (pd.procsym.name<>'INIT') then
  54. Message(parser_e_constructorname_must_be_init);
  55. consume(_SEMICOLON);
  56. include(current_objectdef.objectoptions,oo_has_constructor);
  57. { Set return type, class constructors return the
  58. created instance, object constructors return boolean }
  59. if is_class(pd._class) then
  60. pd.returndef:=pd._class
  61. else
  62. {$ifdef CPU64bitaddr}
  63. pd.returndef:=bool64type;
  64. {$else CPU64bitaddr}
  65. pd.returndef:=bool32type;
  66. {$endif CPU64bitaddr}
  67. result:=pd;
  68. end;
  69. procedure property_dec;
  70. var
  71. p : tpropertysym;
  72. begin
  73. { check for a class }
  74. if not((is_class_or_interface_or_dispinterface(current_objectdef)) or
  75. (not(m_tp7 in current_settings.modeswitches) and (is_object(current_objectdef)))) then
  76. Message(parser_e_syntax_error);
  77. consume(_PROPERTY);
  78. p:=read_property_dec(current_objectdef);
  79. consume(_SEMICOLON);
  80. if try_to_consume(_DEFAULT) then
  81. begin
  82. if oo_has_default_property in current_objectdef.objectoptions then
  83. message(parser_e_only_one_default_property);
  84. include(current_objectdef.objectoptions,oo_has_default_property);
  85. include(p.propoptions,ppo_defaultproperty);
  86. if not(ppo_hasparameters in p.propoptions) then
  87. message(parser_e_property_need_paras);
  88. consume(_SEMICOLON);
  89. end;
  90. { hint directives, these can be separated by semicolons here,
  91. that needs to be handled here with a loop (PFV) }
  92. while try_consume_hintdirective(p.symoptions) do
  93. Consume(_SEMICOLON);
  94. end;
  95. function destructor_head:tprocdef;
  96. var
  97. pd : tprocdef;
  98. begin
  99. result:=nil;
  100. consume(_DESTRUCTOR);
  101. parse_proc_head(current_objectdef,potype_destructor,pd);
  102. if not assigned(pd) then
  103. begin
  104. consume(_SEMICOLON);
  105. exit;
  106. end;
  107. if (cs_constructor_name in current_settings.globalswitches) and
  108. (pd.procsym.name<>'DONE') then
  109. Message(parser_e_destructorname_must_be_done);
  110. if not(pd.maxparacount=0) and
  111. (m_fpc in current_settings.modeswitches) then
  112. Message(parser_e_no_paras_for_destructor);
  113. consume(_SEMICOLON);
  114. include(current_objectdef.objectoptions,oo_has_destructor);
  115. { no return value }
  116. pd.returndef:=voidtype;
  117. result:=pd;
  118. end;
  119. procedure setinterfacemethodoptions;
  120. var
  121. i : longint;
  122. def : tdef;
  123. begin
  124. include(current_objectdef.objectoptions,oo_has_virtual);
  125. for i:=0 to current_objectdef.symtable.DefList.count-1 do
  126. begin
  127. def:=tdef(current_objectdef.symtable.DefList[i]);
  128. if assigned(def) and
  129. (def.typ=procdef) then
  130. begin
  131. include(tprocdef(def).procoptions,po_virtualmethod);
  132. tprocdef(def).forwarddef:=false;
  133. end;
  134. end;
  135. end;
  136. function readobjecttype : tobjecttyp;
  137. begin
  138. result:=odt_none;
  139. { distinguish classes and objects }
  140. case token of
  141. _OBJECT:
  142. begin
  143. result:=odt_object;
  144. consume(_OBJECT)
  145. end;
  146. _CPPCLASS:
  147. begin
  148. result:=odt_cppclass;
  149. consume(_CPPCLASS);
  150. end;
  151. _DISPINTERFACE:
  152. begin
  153. { need extra check here since interface is a keyword
  154. in all pascal modes }
  155. if not(m_class in current_settings.modeswitches) then
  156. Message(parser_f_need_objfpc_or_delphi_mode);
  157. result:=odt_dispinterface;
  158. consume(_DISPINTERFACE);
  159. end;
  160. _INTERFACE:
  161. begin
  162. { need extra check here since interface is a keyword
  163. in all pascal modes }
  164. if not(m_class in current_settings.modeswitches) then
  165. Message(parser_f_need_objfpc_or_delphi_mode);
  166. if current_settings.interfacetype=it_interfacecom then
  167. result:=odt_interfacecom
  168. else {it_interfacecorba}
  169. result:=odt_interfacecorba;
  170. consume(_INTERFACE);
  171. end;
  172. _CLASS:
  173. begin
  174. result:=odt_class;
  175. consume(_CLASS);
  176. end;
  177. else
  178. begin
  179. { this is error but try to recover }
  180. result:=odt_class;
  181. consume(_OBJECT);
  182. end;
  183. end;
  184. end;
  185. procedure handleImplementedInterface(intfdef : tobjectdef);
  186. begin
  187. if not is_interface(intfdef) then
  188. begin
  189. Message1(type_e_interface_type_expected,intfdef.typename);
  190. exit;
  191. end;
  192. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  193. Message1(sym_e_duplicate_id,intfdef.objname^)
  194. else
  195. begin
  196. { allocate and prepare the GUID only if the class
  197. implements some interfaces. }
  198. if current_objectdef.ImplementedInterfaces.count = 0 then
  199. current_objectdef.prepareguid;
  200. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  201. end;
  202. end;
  203. procedure readImplementedInterfaces;
  204. var
  205. hdef : tdef;
  206. begin
  207. while try_to_consume(_COMMA) do
  208. begin
  209. id_type(hdef,false);
  210. if (hdef.typ<>objectdef) then
  211. begin
  212. Message1(type_e_interface_type_expected,hdef.typename);
  213. continue;
  214. end;
  215. handleImplementedInterface(tobjectdef(hdef));
  216. end;
  217. end;
  218. procedure readinterfaceiid;
  219. var
  220. p : tnode;
  221. valid : boolean;
  222. begin
  223. p:=comp_expr(true);
  224. if p.nodetype=stringconstn then
  225. begin
  226. stringdispose(current_objectdef.iidstr);
  227. current_objectdef.iidstr:=stringdup(strpas(tstringconstnode(p).value_str));
  228. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  229. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  230. not assigned(current_objectdef.iidguid) and
  231. not valid then
  232. Message(parser_e_improper_guid_syntax);
  233. include(current_objectdef.objectoptions,oo_has_valid_guid);
  234. end
  235. else
  236. Message(parser_e_illegal_expression);
  237. p.free;
  238. end;
  239. procedure parse_parent_classes;
  240. var
  241. intfchildof,
  242. childof : tobjectdef;
  243. hdef : tdef;
  244. hasparentdefined : boolean;
  245. begin
  246. childof:=nil;
  247. intfchildof:=nil;
  248. hasparentdefined:=false;
  249. { reads the parent class }
  250. if try_to_consume(_LKLAMMER) then
  251. begin
  252. { use single_type instead of id_type for specialize support }
  253. single_type(hdef,false);
  254. if (not assigned(hdef)) or
  255. (hdef.typ<>objectdef) then
  256. begin
  257. if assigned(hdef) then
  258. Message1(type_e_class_type_expected,hdef.typename);
  259. end
  260. else
  261. begin
  262. childof:=tobjectdef(hdef);
  263. { a mix of class, interfaces, objects and cppclasses
  264. isn't allowed }
  265. case current_objectdef.objecttype of
  266. odt_class:
  267. if not(is_class(childof)) then
  268. begin
  269. if is_interface(childof) then
  270. begin
  271. { we insert the interface after the child
  272. is set, see below
  273. }
  274. intfchildof:=childof;
  275. childof:=class_tobject;
  276. end
  277. else
  278. Message(parser_e_mix_of_classes_and_objects);
  279. end;
  280. odt_interfacecorba,
  281. odt_interfacecom:
  282. begin
  283. if not(is_interface(childof)) then
  284. Message(parser_e_mix_of_classes_and_objects);
  285. current_objectdef.objecttype:=childof.objecttype;
  286. current_objectdef.objecttype:=current_objectdef.objecttype;
  287. end;
  288. odt_cppclass:
  289. if not(is_cppclass(childof)) then
  290. Message(parser_e_mix_of_classes_and_objects);
  291. odt_object:
  292. if not(is_object(childof)) then
  293. Message(parser_e_mix_of_classes_and_objects);
  294. odt_dispinterface:
  295. Message(parser_e_dispinterface_cant_have_parent);
  296. end;
  297. end;
  298. hasparentdefined:=true;
  299. end;
  300. { no generic as parents }
  301. if assigned(childof) and
  302. (df_generic in childof.defoptions) then
  303. begin
  304. Message(parser_e_no_generics_as_types);
  305. childof:=nil;
  306. end;
  307. { if no parent class, then a class get tobject as parent }
  308. if not assigned(childof) then
  309. begin
  310. case current_objectdef.objecttype of
  311. odt_class:
  312. if current_objectdef<>class_tobject then
  313. childof:=class_tobject;
  314. odt_interfacecom:
  315. if current_objectdef<>interface_iunknown then
  316. childof:=interface_iunknown;
  317. end;
  318. end;
  319. if assigned(childof) then
  320. begin
  321. { Forbid not completly defined objects to be used as parents. This will
  322. also prevent circular loops of classes, because we set the forward flag
  323. at the start of the new definition and will reset it below after the
  324. parent has been set }
  325. if not(oo_is_forward in childof.objectoptions) then
  326. current_objectdef.set_parent(childof)
  327. else
  328. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  329. end;
  330. { remove forward flag, is resolved }
  331. exclude(current_objectdef.objectoptions,oo_is_forward);
  332. if hasparentdefined then
  333. begin
  334. if current_objectdef.objecttype=odt_class then
  335. begin
  336. if assigned(intfchildof) then
  337. handleImplementedInterface(intfchildof);
  338. readImplementedInterfaces;
  339. end;
  340. consume(_RKLAMMER);
  341. end;
  342. end;
  343. procedure parse_guid;
  344. begin
  345. { read GUID }
  346. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  347. try_to_consume(_LECKKLAMMER) then
  348. begin
  349. readinterfaceiid;
  350. consume(_RECKKLAMMER);
  351. end
  352. else if (current_objectdef.objecttype=odt_dispinterface) then
  353. message(parser_e_dispinterface_needs_a_guid);
  354. end;
  355. function try_parse_class_forward_decl:boolean;
  356. begin
  357. result:=false;
  358. if (token<>_SEMICOLON) then
  359. exit;
  360. if (cs_compilesystem in current_settings.moduleswitches) then
  361. begin
  362. case current_objectdef.objecttype of
  363. odt_interfacecom :
  364. if (current_objectdef.objname^='IUNKNOWN') then
  365. interface_iunknown:=current_objectdef;
  366. odt_class :
  367. if (current_objectdef.objname^='TOBJECT') then
  368. class_tobject:=current_objectdef;
  369. end;
  370. end;
  371. { enable published? }
  372. if (cs_generate_rtti in current_settings.localswitches) and
  373. (current_objectdef.objecttype in [odt_interfacecom,odt_class]) then
  374. include(current_objectdef.objectoptions,oo_can_have_published);
  375. { all classes must have a vmt at offset zero }
  376. if current_objectdef.objecttype=odt_class then
  377. current_objectdef.insertvmt;
  378. result:=true;
  379. end;
  380. function try_parse_class_reference:tdef;
  381. var
  382. hdef : tdef;
  383. begin
  384. result:=nil;
  385. { Delphi only allows class of in type blocks.
  386. Note that when parsing the type of a variable declaration
  387. the blocktype is bt_type so the check for typecanbeforward
  388. is also necessary (PFV) }
  389. if (token<>_OF) or
  390. (
  391. (m_delphi in current_settings.modeswitches) and
  392. not((block_type=bt_type) and typecanbeforward)
  393. ) then
  394. exit;
  395. consume(_OF);
  396. single_type(hdef,typecanbeforward);
  397. { must be a forward def or a class }
  398. if (hdef.typ=forwarddef) or
  399. is_class(hdef) then
  400. result:=tclassrefdef.create(hdef)
  401. else
  402. begin
  403. Message1(type_e_class_type_expected,generrordef.typename);
  404. result:=generrordef;
  405. end;
  406. end;
  407. procedure insert_generic_parameter_types(genericdef:tstoreddef;genericlist:TFPObjectList);
  408. var
  409. i : longint;
  410. generictype : ttypesym;
  411. begin
  412. current_objectdef.genericdef:=genericdef;
  413. if not assigned(genericlist) then
  414. exit;
  415. for i:=0 to genericlist.count-1 do
  416. begin
  417. generictype:=ttypesym(genericlist[i]);
  418. if generictype.typedef.typ=undefineddef then
  419. include(current_objectdef.defoptions,df_generic)
  420. else
  421. include(current_objectdef.defoptions,df_specialization);
  422. symtablestack.top.insert(generictype);
  423. end;
  424. end;
  425. procedure parse_object_members;
  426. procedure chkcpp(pd:tprocdef);
  427. begin
  428. if is_cppclass(pd._class) then
  429. begin
  430. pd.proccalloption:=pocall_cppdecl;
  431. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  432. end;
  433. end;
  434. procedure maybe_parse_hint_directives(pd:tprocdef);
  435. var
  436. dummysymoptions : tsymoptions;
  437. begin
  438. dummysymoptions:=[];
  439. while try_consume_hintdirective(dummysymoptions) do
  440. Consume(_SEMICOLON);
  441. if assigned(pd) then
  442. pd.symoptions:=pd.symoptions+dummysymoptions;
  443. end;
  444. var
  445. pd : tprocdef;
  446. has_destructor,
  447. oldparse_only,
  448. old_parse_generic : boolean;
  449. object_member_blocktype : tblock_type;
  450. begin
  451. { empty class declaration ? }
  452. if (current_objectdef.objecttype=odt_class) and
  453. (token=_SEMICOLON) then
  454. exit;
  455. old_parse_generic:=parse_generic;
  456. parse_generic:=(df_generic in current_objectdef.defoptions);
  457. { in "publishable" classes the default access type is published }
  458. if (oo_can_have_published in current_objectdef.objectoptions) then
  459. current_object_option:=[sp_published]
  460. else
  461. current_object_option:=[sp_public];
  462. testcurobject:=1;
  463. has_destructor:=false;
  464. object_member_blocktype:=bt_general;
  465. repeat
  466. case token of
  467. _TYPE :
  468. begin
  469. if ([df_generic,df_specialization]*current_objectdef.defoptions)=[] then
  470. Message(parser_e_type_and_var_only_in_generics);
  471. consume(_TYPE);
  472. object_member_blocktype:=bt_type;
  473. end;
  474. _VAR :
  475. begin
  476. if ([df_generic,df_specialization]*current_objectdef.defoptions)=[] then
  477. Message(parser_e_type_and_var_only_in_generics);
  478. consume(_VAR);
  479. object_member_blocktype:=bt_general;
  480. end;
  481. _ID :
  482. begin
  483. case idtoken of
  484. _PRIVATE :
  485. begin
  486. if is_interface(current_objectdef) then
  487. Message(parser_e_no_access_specifier_in_interfaces);
  488. consume(_PRIVATE);
  489. current_object_option:=[sp_private];
  490. include(current_objectdef.objectoptions,oo_has_private);
  491. end;
  492. _PROTECTED :
  493. begin
  494. if is_interface(current_objectdef) then
  495. Message(parser_e_no_access_specifier_in_interfaces);
  496. consume(_PROTECTED);
  497. current_object_option:=[sp_protected];
  498. include(current_objectdef.objectoptions,oo_has_protected);
  499. end;
  500. _PUBLIC :
  501. begin
  502. if is_interface(current_objectdef) then
  503. Message(parser_e_no_access_specifier_in_interfaces);
  504. consume(_PUBLIC);
  505. current_object_option:=[sp_public];
  506. end;
  507. _PUBLISHED :
  508. begin
  509. { we've to check for a pushlished section in non- }
  510. { publishable classes later, if a real declaration }
  511. { this is the way, delphi does it }
  512. if is_interface(current_objectdef) then
  513. Message(parser_e_no_access_specifier_in_interfaces);
  514. consume(_PUBLISHED);
  515. current_object_option:=[sp_published];
  516. end;
  517. _STRICT :
  518. begin
  519. if is_interface(current_objectdef) then
  520. Message(parser_e_no_access_specifier_in_interfaces);
  521. consume(_STRICT);
  522. if token=_ID then
  523. begin
  524. case idtoken of
  525. _PRIVATE:
  526. begin
  527. consume(_PRIVATE);
  528. current_object_option:=[sp_strictprivate];
  529. include(current_objectdef.objectoptions,oo_has_strictprivate);
  530. end;
  531. _PROTECTED:
  532. begin
  533. consume(_PROTECTED);
  534. current_object_option:=[sp_strictprotected];
  535. include(current_objectdef.objectoptions,oo_has_strictprotected);
  536. end;
  537. else
  538. message(parser_e_protected_or_private_expected);
  539. end;
  540. end
  541. else
  542. message(parser_e_protected_or_private_expected);
  543. end;
  544. else
  545. begin
  546. if object_member_blocktype=bt_general then
  547. begin
  548. if is_interface(current_objectdef) then
  549. Message(parser_e_no_vars_in_interfaces);
  550. if (sp_published in current_object_option) and
  551. not(oo_can_have_published in current_objectdef.objectoptions) then
  552. Message(parser_e_cant_have_published);
  553. read_record_fields([vd_object])
  554. end
  555. else
  556. types_dec;
  557. end;
  558. end;
  559. end;
  560. _PROPERTY :
  561. begin
  562. property_dec;
  563. end;
  564. _PROCEDURE,
  565. _FUNCTION,
  566. _CLASS :
  567. begin
  568. if (sp_published in current_object_option) and
  569. not(oo_can_have_published in current_objectdef.objectoptions) then
  570. Message(parser_e_cant_have_published);
  571. oldparse_only:=parse_only;
  572. parse_only:=true;
  573. pd:=parse_proc_dec(current_objectdef);
  574. { this is for error recovery as well as forward }
  575. { interface mappings, i.e. mapping to a method }
  576. { which isn't declared yet }
  577. if assigned(pd) then
  578. begin
  579. parse_object_proc_directives(pd);
  580. { all Macintosh Object Pascal methods are virtual. }
  581. { this can't be a class method, because macpas mode }
  582. { has no m_class }
  583. if (m_mac in current_settings.modeswitches) then
  584. include(pd.procoptions,po_virtualmethod);
  585. handle_calling_convention(pd);
  586. { add definition to procsym }
  587. proc_add_definition(pd);
  588. { add procdef options to objectdef options }
  589. if (po_msgint in pd.procoptions) then
  590. include(current_objectdef.objectoptions,oo_has_msgint);
  591. if (po_msgstr in pd.procoptions) then
  592. include(current_objectdef.objectoptions,oo_has_msgstr);
  593. if (po_virtualmethod in pd.procoptions) then
  594. include(current_objectdef.objectoptions,oo_has_virtual);
  595. chkcpp(pd);
  596. end;
  597. maybe_parse_hint_directives(pd);
  598. parse_only:=oldparse_only;
  599. end;
  600. _CONSTRUCTOR :
  601. begin
  602. if (sp_published in current_object_option) and
  603. not(oo_can_have_published in current_objectdef.objectoptions) then
  604. Message(parser_e_cant_have_published);
  605. if not(sp_public in current_object_option) and
  606. not(sp_published in current_object_option) then
  607. Message(parser_w_constructor_should_be_public);
  608. if is_interface(current_objectdef) then
  609. Message(parser_e_no_con_des_in_interfaces);
  610. oldparse_only:=parse_only;
  611. parse_only:=true;
  612. pd:=constructor_head;
  613. parse_object_proc_directives(pd);
  614. handle_calling_convention(pd);
  615. { add definition to procsym }
  616. proc_add_definition(pd);
  617. { add procdef options to objectdef options }
  618. if (po_virtualmethod in pd.procoptions) then
  619. include(current_objectdef.objectoptions,oo_has_virtual);
  620. chkcpp(pd);
  621. maybe_parse_hint_directives(pd);
  622. parse_only:=oldparse_only;
  623. end;
  624. _DESTRUCTOR :
  625. begin
  626. if (sp_published in current_object_option) and
  627. not(oo_can_have_published in current_objectdef.objectoptions) then
  628. Message(parser_e_cant_have_published);
  629. if has_destructor then
  630. Message(parser_n_only_one_destructor);
  631. has_destructor:=true;
  632. if is_interface(current_objectdef) then
  633. Message(parser_e_no_con_des_in_interfaces);
  634. if not(sp_public in current_object_option) then
  635. Message(parser_w_destructor_should_be_public);
  636. oldparse_only:=parse_only;
  637. parse_only:=true;
  638. pd:=destructor_head;
  639. parse_object_proc_directives(pd);
  640. handle_calling_convention(pd);
  641. { add definition to procsym }
  642. proc_add_definition(pd);
  643. { add procdef options to objectdef options }
  644. if (po_virtualmethod in pd.procoptions) then
  645. include(current_objectdef.objectoptions,oo_has_virtual);
  646. chkcpp(pd);
  647. maybe_parse_hint_directives(pd);
  648. parse_only:=oldparse_only;
  649. end;
  650. _END :
  651. begin
  652. consume(_END);
  653. break;
  654. end;
  655. else
  656. consume(_ID); { Give a ident expected message, like tp7 }
  657. end;
  658. until false;
  659. { restore }
  660. testcurobject:=0;
  661. parse_generic:=old_parse_generic;
  662. end;
  663. function object_dec(const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tdef;
  664. label
  665. myexit;
  666. var
  667. objecttype : tobjecttyp;
  668. old_object_option : tsymoptions;
  669. old_typecanbeforward : boolean;
  670. old_current_objectdef : tobjectdef;
  671. begin
  672. old_object_option:=current_object_option;
  673. old_current_objectdef:=current_objectdef;
  674. old_typecanbeforward:=typecanbeforward;
  675. current_objectdef:=nil;
  676. { objects and class types can't be declared local }
  677. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable]) and
  678. not assigned(genericlist) then
  679. Message(parser_e_no_local_objects);
  680. { for tp7 don't allow forward types }
  681. if (m_tp7 in current_settings.modeswitches) then
  682. typecanbeforward:=false;
  683. { get type of objectdef }
  684. objecttype:=readobjecttype;
  685. { reuse forward objectdef? }
  686. if assigned(fd) then
  687. begin
  688. if fd.objecttype<>objecttype then
  689. begin
  690. Message(parser_e_forward_mismatch);
  691. { recover }
  692. current_objectdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  693. include(current_objectdef.objectoptions,oo_is_forward);
  694. end
  695. else
  696. current_objectdef:=fd
  697. end
  698. else
  699. begin
  700. { Handle class of ... class references }
  701. if objecttype=odt_class then
  702. begin
  703. result:=try_parse_class_reference;
  704. if assigned(result) then
  705. goto myexit;
  706. end;
  707. { anonym objects aren't allow (o : object a : longint; end;) }
  708. if n='' then
  709. Message(parser_f_no_anonym_objects);
  710. { create new class }
  711. current_objectdef:=tobjectdef.create(objecttype,n,nil);
  712. { include always the forward flag, it'll be removed after the parent class have been
  713. added. This is to prevent circular childof loops }
  714. include(current_objectdef.objectoptions,oo_is_forward);
  715. { is this a forward declaration? }
  716. if try_parse_class_forward_decl then
  717. begin
  718. result:=current_objectdef;
  719. goto myexit;
  720. end;
  721. end;
  722. { set published flag in $M+ mode, it can also be inherited and will
  723. be added when the parent class set with tobjectdef.set_parent (PFV) }
  724. if (cs_generate_rtti in current_settings.localswitches) then
  725. include(current_objectdef.objectoptions,oo_can_have_published);
  726. { parse list of parent classes }
  727. parse_parent_classes;
  728. { parse optional GUID for interfaces }
  729. parse_guid;
  730. { parse and insert object members }
  731. symtablestack.push(current_objectdef.symtable);
  732. insert_generic_parameter_types(genericdef,genericlist);
  733. parse_object_members;
  734. symtablestack.pop(current_objectdef.symtable);
  735. { generate vmt space if needed }
  736. if not(oo_has_vmt in current_objectdef.objectoptions) and
  737. (
  738. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_objectdef.objectoptions<>[]) or
  739. (current_objectdef.objecttype in [odt_class])
  740. ) then
  741. current_objectdef.insertvmt;
  742. if is_interface(current_objectdef) then
  743. setinterfacemethodoptions;
  744. { return defined objectdef }
  745. result:=current_objectdef;
  746. myexit:
  747. { restore old state }
  748. current_objectdef:=old_current_objectdef;
  749. typecanbeforward:=old_typecanbeforward;
  750. current_object_option:=old_object_option;
  751. end;
  752. end.