pdecobj.pas 29 KB

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