pdecobj.pas 34 KB

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