pdecobj.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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,symconst,symtype,symdef;
  23. { parses a object declaration }
  24. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  25. implementation
  26. uses
  27. cutils,
  28. globals,verbose,systems,tokens,
  29. symbase,symsym,symtable,
  30. node,nld,nmem,ncon,ncnv,ncal,
  31. fmodule,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. if (token=_COLON) then
  89. begin
  90. Message(parser_e_field_not_allowed_here);
  91. consume_all_until(_SEMICOLON);
  92. end;
  93. consume(_SEMICOLON);
  94. end;
  95. { parse possible enumerator modifier }
  96. if try_to_consume(_ENUMERATOR) then
  97. begin
  98. if (token = _ID) then
  99. begin
  100. if pattern='CURRENT' then
  101. begin
  102. if oo_has_enumerator_current in current_objectdef.objectoptions then
  103. message(parser_e_only_one_enumerator_current);
  104. if not p.propaccesslist[palt_read].empty then
  105. begin
  106. include(current_objectdef.objectoptions,oo_has_enumerator_current);
  107. include(p.propoptions,ppo_enumerator_current);
  108. end
  109. else
  110. Message(parser_e_enumerator_current_is_not_valid) // property has no reader
  111. end
  112. else
  113. Message1(parser_e_invalid_enumerator_identifier, pattern);
  114. consume(token);
  115. end
  116. else
  117. Message(parser_e_enumerator_identifier_required);
  118. consume(_SEMICOLON);
  119. end;
  120. { hint directives, these can be separated by semicolons here,
  121. that needs to be handled here with a loop (PFV) }
  122. while try_consume_hintdirective(p.symoptions,p.deprecatedmsg) do
  123. Consume(_SEMICOLON);
  124. end;
  125. function destructor_head:tprocdef;
  126. var
  127. pd : tprocdef;
  128. begin
  129. result:=nil;
  130. consume(_DESTRUCTOR);
  131. parse_proc_head(current_objectdef,potype_destructor,pd);
  132. if not assigned(pd) then
  133. begin
  134. consume(_SEMICOLON);
  135. exit;
  136. end;
  137. if (cs_constructor_name in current_settings.globalswitches) and
  138. (pd.procsym.name<>'DONE') then
  139. Message(parser_e_destructorname_must_be_done);
  140. if not(pd.maxparacount=0) and
  141. (m_fpc in current_settings.modeswitches) then
  142. Message(parser_e_no_paras_for_destructor);
  143. consume(_SEMICOLON);
  144. include(current_objectdef.objectoptions,oo_has_destructor);
  145. { no return value }
  146. pd.returndef:=voidtype;
  147. result:=pd;
  148. end;
  149. procedure setinterfacemethodoptions;
  150. var
  151. i : longint;
  152. def : tdef;
  153. begin
  154. include(current_objectdef.objectoptions,oo_has_virtual);
  155. for i:=0 to current_objectdef.symtable.DefList.count-1 do
  156. begin
  157. def:=tdef(current_objectdef.symtable.DefList[i]);
  158. if assigned(def) and
  159. (def.typ=procdef) then
  160. begin
  161. include(tprocdef(def).procoptions,po_virtualmethod);
  162. tprocdef(def).forwarddef:=false;
  163. end;
  164. end;
  165. end;
  166. procedure handleImplementedInterface(intfdef : tobjectdef);
  167. begin
  168. if not is_interface(intfdef) then
  169. begin
  170. Message1(type_e_interface_type_expected,intfdef.typename);
  171. exit;
  172. end;
  173. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  174. Message1(sym_e_duplicate_id,intfdef.objname^)
  175. else
  176. begin
  177. { allocate and prepare the GUID only if the class
  178. implements some interfaces. }
  179. if current_objectdef.ImplementedInterfaces.count = 0 then
  180. current_objectdef.prepareguid;
  181. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  182. end;
  183. end;
  184. procedure readImplementedInterfaces;
  185. var
  186. hdef : tdef;
  187. begin
  188. while try_to_consume(_COMMA) do
  189. begin
  190. id_type(hdef,false);
  191. if (hdef.typ<>objectdef) then
  192. begin
  193. Message1(type_e_interface_type_expected,hdef.typename);
  194. continue;
  195. end;
  196. handleImplementedInterface(tobjectdef(hdef));
  197. end;
  198. end;
  199. procedure readinterfaceiid;
  200. var
  201. p : tnode;
  202. valid : boolean;
  203. begin
  204. p:=comp_expr(true);
  205. if p.nodetype=stringconstn then
  206. begin
  207. stringdispose(current_objectdef.iidstr);
  208. current_objectdef.iidstr:=stringdup(strpas(tstringconstnode(p).value_str));
  209. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  210. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  211. not valid then
  212. Message(parser_e_improper_guid_syntax);
  213. include(current_objectdef.objectoptions,oo_has_valid_guid);
  214. end
  215. else
  216. Message(parser_e_illegal_expression);
  217. p.free;
  218. end;
  219. procedure parse_object_options;
  220. begin
  221. if current_objectdef.objecttype = odt_class then
  222. begin
  223. while true do
  224. begin
  225. if try_to_consume(_ABSTRACT) then
  226. include(current_objectdef.objectoptions,oo_is_abstract)
  227. else
  228. if try_to_consume(_SEALED) then
  229. include(current_objectdef.objectoptions,oo_is_sealed)
  230. else
  231. break;
  232. end;
  233. if [oo_is_abstract, oo_is_sealed] * current_objectdef.objectoptions = [oo_is_abstract, oo_is_sealed] then
  234. Message(parser_e_abstract_and_sealed_conflict);
  235. end;
  236. end;
  237. procedure parse_parent_classes;
  238. var
  239. intfchildof,
  240. childof : tobjectdef;
  241. hdef : tdef;
  242. hasparentdefined : boolean;
  243. begin
  244. childof:=nil;
  245. intfchildof:=nil;
  246. hasparentdefined:=false;
  247. { reads the parent class }
  248. if try_to_consume(_LKLAMMER) then
  249. begin
  250. { use single_type instead of id_type for specialize support }
  251. single_type(hdef,false,false);
  252. if (not assigned(hdef)) or
  253. (hdef.typ<>objectdef) then
  254. begin
  255. if assigned(hdef) then
  256. Message1(type_e_class_type_expected,hdef.typename);
  257. end
  258. else
  259. begin
  260. childof:=tobjectdef(hdef);
  261. { a mix of class, interfaces, objects and cppclasses
  262. isn't allowed }
  263. case current_objectdef.objecttype of
  264. odt_class:
  265. if not(is_class(childof)) then
  266. begin
  267. if is_interface(childof) then
  268. begin
  269. { we insert the interface after the child
  270. is set, see below
  271. }
  272. intfchildof:=childof;
  273. childof:=class_tobject;
  274. end
  275. else
  276. Message(parser_e_mix_of_classes_and_objects);
  277. end
  278. else
  279. if oo_is_sealed in childof.objectoptions then
  280. Message1(parser_e_sealed_descendant,childof.typename);
  281. odt_interfacecorba,
  282. odt_interfacecom:
  283. begin
  284. if not(is_interface(childof)) then
  285. Message(parser_e_mix_of_classes_and_objects);
  286. current_objectdef.objecttype:=childof.objecttype;
  287. current_objectdef.objecttype:=current_objectdef.objecttype;
  288. end;
  289. odt_cppclass:
  290. if not(is_cppclass(childof)) then
  291. Message(parser_e_mix_of_classes_and_objects);
  292. odt_object:
  293. if not(is_object(childof)) then
  294. Message(parser_e_mix_of_classes_and_objects);
  295. odt_dispinterface:
  296. Message(parser_e_dispinterface_cant_have_parent);
  297. end;
  298. end;
  299. hasparentdefined:=true;
  300. end;
  301. { no generic as parents }
  302. if assigned(childof) and
  303. (df_generic in childof.defoptions) then
  304. begin
  305. Message(parser_e_no_generics_as_types);
  306. childof:=nil;
  307. end;
  308. { if no parent class, then a class get tobject as parent }
  309. if not assigned(childof) then
  310. begin
  311. case current_objectdef.objecttype of
  312. odt_class:
  313. if current_objectdef<>class_tobject then
  314. childof:=class_tobject;
  315. odt_interfacecom:
  316. if current_objectdef<>interface_iunknown then
  317. childof:=interface_iunknown;
  318. end;
  319. end;
  320. if assigned(childof) then
  321. begin
  322. { Forbid not completly defined objects to be used as parents. This will
  323. also prevent circular loops of classes, because we set the forward flag
  324. at the start of the new definition and will reset it below after the
  325. parent has been set }
  326. if not(oo_is_forward in childof.objectoptions) then
  327. current_objectdef.set_parent(childof)
  328. else
  329. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  330. end;
  331. { remove forward flag, is resolved }
  332. exclude(current_objectdef.objectoptions,oo_is_forward);
  333. if hasparentdefined then
  334. begin
  335. if current_objectdef.objecttype=odt_class then
  336. begin
  337. if assigned(intfchildof) then
  338. handleImplementedInterface(intfchildof);
  339. readImplementedInterfaces;
  340. end;
  341. consume(_RKLAMMER);
  342. end;
  343. end;
  344. procedure parse_guid;
  345. begin
  346. { read GUID }
  347. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  348. try_to_consume(_LECKKLAMMER) then
  349. begin
  350. readinterfaceiid;
  351. consume(_RECKKLAMMER);
  352. end
  353. else if (current_objectdef.objecttype=odt_dispinterface) then
  354. message(parser_e_dispinterface_needs_a_guid);
  355. end;
  356. procedure insert_generic_parameter_types(genericdef:tstoreddef;genericlist:TFPObjectList);
  357. var
  358. i : longint;
  359. generictype : ttypesym;
  360. begin
  361. current_objectdef.genericdef:=genericdef;
  362. if not assigned(genericlist) then
  363. exit;
  364. for i:=0 to genericlist.count-1 do
  365. begin
  366. generictype:=ttypesym(genericlist[i]);
  367. if generictype.typedef.typ=undefineddef then
  368. include(current_objectdef.defoptions,df_generic)
  369. else
  370. include(current_objectdef.defoptions,df_specialization);
  371. symtablestack.top.insert(generictype);
  372. end;
  373. end;
  374. procedure parse_object_members;
  375. procedure chkcpp(pd:tprocdef);
  376. begin
  377. if is_cppclass(pd._class) then
  378. begin
  379. pd.proccalloption:=pocall_cppdecl;
  380. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  381. end;
  382. end;
  383. procedure maybe_parse_hint_directives(pd:tprocdef);
  384. var
  385. dummysymoptions : tsymoptions;
  386. deprecatedmsg : pshortstring;
  387. begin
  388. dummysymoptions:=[];
  389. deprecatedmsg:=nil;
  390. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  391. Consume(_SEMICOLON);
  392. if assigned(pd) then
  393. begin
  394. pd.symoptions:=pd.symoptions+dummysymoptions;
  395. pd.deprecatedmsg:=deprecatedmsg;
  396. end
  397. else
  398. stringdispose(deprecatedmsg);
  399. end;
  400. var
  401. pd : tprocdef;
  402. has_destructor,
  403. oldparse_only,
  404. old_parse_generic : boolean;
  405. object_member_blocktype : tblock_type;
  406. fields_allowed: boolean;
  407. begin
  408. { empty class declaration ? }
  409. if (current_objectdef.objecttype=odt_class) and
  410. (token=_SEMICOLON) then
  411. exit;
  412. old_parse_generic:=parse_generic;
  413. parse_generic:=(df_generic in current_objectdef.defoptions);
  414. { in "publishable" classes the default access type is published }
  415. if (oo_can_have_published in current_objectdef.objectoptions) then
  416. current_objectdef.symtable.currentvisibility:=vis_published
  417. else
  418. current_objectdef.symtable.currentvisibility:=vis_public;
  419. testcurobject:=1;
  420. has_destructor:=false;
  421. fields_allowed:=true;
  422. object_member_blocktype:=bt_general;
  423. repeat
  424. case token of
  425. _TYPE :
  426. begin
  427. if ([df_generic,df_specialization]*current_objectdef.defoptions)=[] then
  428. Message(parser_e_type_and_var_only_in_generics);
  429. consume(_TYPE);
  430. object_member_blocktype:=bt_type;
  431. end;
  432. _VAR :
  433. begin
  434. if ([df_generic,df_specialization]*current_objectdef.defoptions)=[] then
  435. Message(parser_e_type_and_var_only_in_generics);
  436. consume(_VAR);
  437. object_member_blocktype:=bt_general;
  438. end;
  439. _ID :
  440. begin
  441. case idtoken of
  442. _PRIVATE :
  443. begin
  444. if is_interface(current_objectdef) then
  445. Message(parser_e_no_access_specifier_in_interfaces);
  446. consume(_PRIVATE);
  447. current_objectdef.symtable.currentvisibility:=vis_private;
  448. include(current_objectdef.objectoptions,oo_has_private);
  449. fields_allowed:=true;
  450. end;
  451. _PROTECTED :
  452. begin
  453. if is_interface(current_objectdef) then
  454. Message(parser_e_no_access_specifier_in_interfaces);
  455. consume(_PROTECTED);
  456. current_objectdef.symtable.currentvisibility:=vis_protected;
  457. include(current_objectdef.objectoptions,oo_has_protected);
  458. fields_allowed:=true;
  459. end;
  460. _PUBLIC :
  461. begin
  462. if is_interface(current_objectdef) then
  463. Message(parser_e_no_access_specifier_in_interfaces);
  464. consume(_PUBLIC);
  465. current_objectdef.symtable.currentvisibility:=vis_public;
  466. fields_allowed:=true;
  467. end;
  468. _PUBLISHED :
  469. begin
  470. { we've to check for a pushlished section in non- }
  471. { publishable classes later, if a real declaration }
  472. { this is the way, delphi does it }
  473. if is_interface(current_objectdef) then
  474. Message(parser_e_no_access_specifier_in_interfaces);
  475. consume(_PUBLISHED);
  476. current_objectdef.symtable.currentvisibility:=vis_published;
  477. fields_allowed:=true;
  478. end;
  479. _STRICT :
  480. begin
  481. if is_interface(current_objectdef) then
  482. Message(parser_e_no_access_specifier_in_interfaces);
  483. consume(_STRICT);
  484. if token=_ID then
  485. begin
  486. case idtoken of
  487. _PRIVATE:
  488. begin
  489. consume(_PRIVATE);
  490. current_objectdef.symtable.currentvisibility:=vis_strictprivate;
  491. include(current_objectdef.objectoptions,oo_has_strictprivate);
  492. end;
  493. _PROTECTED:
  494. begin
  495. consume(_PROTECTED);
  496. current_objectdef.symtable.currentvisibility:=vis_strictprotected;
  497. include(current_objectdef.objectoptions,oo_has_strictprotected);
  498. end;
  499. else
  500. message(parser_e_protected_or_private_expected);
  501. end;
  502. end
  503. else
  504. message(parser_e_protected_or_private_expected);
  505. fields_allowed:=true;
  506. end;
  507. else
  508. begin
  509. if object_member_blocktype=bt_general then
  510. begin
  511. if is_interface(current_objectdef) then
  512. Message(parser_e_no_vars_in_interfaces);
  513. if (current_objectdef.symtable.currentvisibility=vis_published) and
  514. not(oo_can_have_published in current_objectdef.objectoptions) then
  515. Message(parser_e_cant_have_published);
  516. if (not fields_allowed) then
  517. Message(parser_e_field_not_allowed_here);
  518. read_record_fields([vd_object])
  519. end
  520. else
  521. types_dec;
  522. end;
  523. end;
  524. end;
  525. _PROPERTY :
  526. begin
  527. property_dec;
  528. fields_allowed:=false;
  529. end;
  530. _PROCEDURE,
  531. _FUNCTION,
  532. _CLASS :
  533. begin
  534. if (current_objectdef.symtable.currentvisibility=vis_published) and
  535. not(oo_can_have_published in current_objectdef.objectoptions) then
  536. Message(parser_e_cant_have_published);
  537. oldparse_only:=parse_only;
  538. parse_only:=true;
  539. pd:=parse_proc_dec(current_objectdef);
  540. { this is for error recovery as well as forward }
  541. { interface mappings, i.e. mapping to a method }
  542. { which isn't declared yet }
  543. if assigned(pd) then
  544. begin
  545. parse_object_proc_directives(pd);
  546. { all Macintosh Object Pascal methods are virtual. }
  547. { this can't be a class method, because macpas mode }
  548. { has no m_class }
  549. if (m_mac in current_settings.modeswitches) then
  550. include(pd.procoptions,po_virtualmethod);
  551. handle_calling_convention(pd);
  552. { add definition to procsym }
  553. proc_add_definition(pd);
  554. { add procdef options to objectdef options }
  555. if (po_msgint in pd.procoptions) then
  556. include(current_objectdef.objectoptions,oo_has_msgint);
  557. if (po_msgstr in pd.procoptions) then
  558. include(current_objectdef.objectoptions,oo_has_msgstr);
  559. if (po_virtualmethod in pd.procoptions) then
  560. include(current_objectdef.objectoptions,oo_has_virtual);
  561. chkcpp(pd);
  562. end;
  563. maybe_parse_hint_directives(pd);
  564. parse_only:=oldparse_only;
  565. fields_allowed:=false;
  566. end;
  567. _CONSTRUCTOR :
  568. begin
  569. if (current_objectdef.symtable.currentvisibility=vis_published) and
  570. not(oo_can_have_published in current_objectdef.objectoptions) then
  571. Message(parser_e_cant_have_published);
  572. if not(current_objectdef.symtable.currentvisibility in [vis_public,vis_published]) then
  573. Message(parser_w_constructor_should_be_public);
  574. if is_interface(current_objectdef) then
  575. Message(parser_e_no_con_des_in_interfaces);
  576. oldparse_only:=parse_only;
  577. parse_only:=true;
  578. pd:=constructor_head;
  579. parse_object_proc_directives(pd);
  580. handle_calling_convention(pd);
  581. { add definition to procsym }
  582. proc_add_definition(pd);
  583. { add procdef options to objectdef options }
  584. if (po_virtualmethod in pd.procoptions) then
  585. include(current_objectdef.objectoptions,oo_has_virtual);
  586. chkcpp(pd);
  587. maybe_parse_hint_directives(pd);
  588. parse_only:=oldparse_only;
  589. fields_allowed:=false;
  590. end;
  591. _DESTRUCTOR :
  592. begin
  593. if (current_objectdef.symtable.currentvisibility=vis_published) and
  594. not(oo_can_have_published in current_objectdef.objectoptions) then
  595. Message(parser_e_cant_have_published);
  596. if has_destructor then
  597. Message(parser_n_only_one_destructor);
  598. has_destructor:=true;
  599. if is_interface(current_objectdef) then
  600. Message(parser_e_no_con_des_in_interfaces);
  601. if (current_objectdef.symtable.currentvisibility<>vis_public) then
  602. Message(parser_w_destructor_should_be_public);
  603. oldparse_only:=parse_only;
  604. parse_only:=true;
  605. pd:=destructor_head;
  606. parse_object_proc_directives(pd);
  607. handle_calling_convention(pd);
  608. { add definition to procsym }
  609. proc_add_definition(pd);
  610. { add procdef options to objectdef options }
  611. if (po_virtualmethod in pd.procoptions) then
  612. include(current_objectdef.objectoptions,oo_has_virtual);
  613. chkcpp(pd);
  614. maybe_parse_hint_directives(pd);
  615. parse_only:=oldparse_only;
  616. fields_allowed:=false;
  617. end;
  618. _END :
  619. begin
  620. consume(_END);
  621. break;
  622. end;
  623. else
  624. consume(_ID); { Give a ident expected message, like tp7 }
  625. end;
  626. until false;
  627. { restore }
  628. testcurobject:=0;
  629. parse_generic:=old_parse_generic;
  630. end;
  631. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  632. var
  633. old_current_objectdef : tobjectdef;
  634. begin
  635. old_current_objectdef:=current_objectdef;
  636. current_objectdef:=nil;
  637. { objects and class types can't be declared local }
  638. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable]) and
  639. not assigned(genericlist) then
  640. Message(parser_e_no_local_objects);
  641. { reuse forward objectdef? }
  642. if assigned(fd) then
  643. begin
  644. if fd.objecttype<>objecttype then
  645. begin
  646. Message(parser_e_forward_mismatch);
  647. { recover }
  648. current_objectdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  649. include(current_objectdef.objectoptions,oo_is_forward);
  650. end
  651. else
  652. current_objectdef:=fd
  653. end
  654. else
  655. begin
  656. { anonym objects aren't allow (o : object a : longint; end;) }
  657. if n='' then
  658. Message(parser_f_no_anonym_objects);
  659. { create new class }
  660. current_objectdef:=tobjectdef.create(objecttype,n,nil);
  661. { include always the forward flag, it'll be removed after the parent class have been
  662. added. This is to prevent circular childof loops }
  663. include(current_objectdef.objectoptions,oo_is_forward);
  664. if (cs_compilesystem in current_settings.moduleswitches) then
  665. begin
  666. case current_objectdef.objecttype of
  667. odt_interfacecom :
  668. if (current_objectdef.objname^='IUNKNOWN') then
  669. interface_iunknown:=current_objectdef;
  670. odt_class :
  671. if (current_objectdef.objname^='TOBJECT') then
  672. class_tobject:=current_objectdef;
  673. end;
  674. end;
  675. end;
  676. { set published flag in $M+ mode, it can also be inherited and will
  677. be added when the parent class set with tobjectdef.set_parent (PFV) }
  678. if (cs_generate_rtti in current_settings.localswitches) and
  679. (current_objectdef.objecttype in [odt_interfacecom,odt_class]) then
  680. include(current_objectdef.objectoptions,oo_can_have_published);
  681. { forward def? }
  682. if not assigned(fd) and
  683. (token=_SEMICOLON) then
  684. begin
  685. { add to the list of definitions to check that the forward
  686. is resolved. this is required for delphi mode }
  687. current_module.checkforwarddefs.add(current_objectdef);
  688. end
  689. else
  690. begin
  691. { parse list of options (abstract / sealed) }
  692. parse_object_options;
  693. { parse list of parent classes }
  694. parse_parent_classes;
  695. { parse optional GUID for interfaces }
  696. parse_guid;
  697. { parse and insert object members }
  698. symtablestack.push(current_objectdef.symtable);
  699. insert_generic_parameter_types(genericdef,genericlist);
  700. parse_object_members;
  701. symtablestack.pop(current_objectdef.symtable);
  702. end;
  703. { generate vmt space if needed }
  704. if not(oo_has_vmt in current_objectdef.objectoptions) and
  705. (
  706. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_objectdef.objectoptions<>[]) or
  707. (current_objectdef.objecttype in [odt_class])
  708. ) then
  709. current_objectdef.insertvmt;
  710. { for implemented classes with a vmt check if there is a constructor }
  711. if (oo_has_vmt in current_objectdef.objectoptions) and
  712. not(oo_is_forward in current_objectdef.objectoptions) and
  713. not(oo_has_constructor in current_objectdef.objectoptions) then
  714. Message1(parser_w_virtual_without_constructor,current_objectdef.objrealname^);
  715. if is_interface(current_objectdef) then
  716. setinterfacemethodoptions;
  717. { return defined objectdef }
  718. result:=current_objectdef;
  719. { restore old state }
  720. current_objectdef:=old_current_objectdef;
  721. end;
  722. end.