pdecobj.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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(is_classproperty:boolean);
  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(is_classproperty, 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 setobjcclassmethodoptions;
  167. var
  168. i : longint;
  169. def : tdef;
  170. begin
  171. for i:=0 to current_objectdef.symtable.DefList.count-1 do
  172. begin
  173. def:=tdef(current_objectdef.symtable.DefList[i]);
  174. if assigned(def) and
  175. (def.typ=procdef) then
  176. begin
  177. include(tprocdef(def).procoptions,po_virtualmethod);
  178. end;
  179. end;
  180. end;
  181. procedure handleImplementedInterface(intfdef : tobjectdef);
  182. begin
  183. if not is_interface(intfdef) then
  184. begin
  185. Message1(type_e_interface_type_expected,intfdef.typename);
  186. exit;
  187. end;
  188. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  189. Message1(sym_e_duplicate_id,intfdef.objname^)
  190. else
  191. begin
  192. { allocate and prepare the GUID only if the class
  193. implements some interfaces. }
  194. if current_objectdef.ImplementedInterfaces.count = 0 then
  195. current_objectdef.prepareguid;
  196. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  197. end;
  198. end;
  199. procedure handleImplementedProtocol(intfdef : tobjectdef);
  200. begin
  201. if not is_objcprotocol(intfdef) then
  202. begin
  203. Message1(type_e_protocol_type_expected,intfdef.typename);
  204. exit;
  205. end;
  206. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  207. Message1(sym_e_duplicate_id,intfdef.objname^)
  208. else
  209. begin
  210. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  211. end;
  212. end;
  213. procedure readImplementedInterfacesAndProtocols(intf: boolean);
  214. var
  215. hdef : tdef;
  216. begin
  217. while try_to_consume(_COMMA) do
  218. begin
  219. id_type(hdef,false);
  220. if (hdef.typ<>objectdef) then
  221. begin
  222. if intf then
  223. Message1(type_e_interface_type_expected,hdef.typename)
  224. else
  225. Message1(type_e_protocol_type_expected,hdef.typename);
  226. continue;
  227. end;
  228. if intf then
  229. handleImplementedInterface(tobjectdef(hdef))
  230. else
  231. handleImplementedProtocol(tobjectdef(hdef));
  232. end;
  233. end;
  234. procedure readinterfaceiid;
  235. var
  236. p : tnode;
  237. valid : boolean;
  238. begin
  239. p:=comp_expr(true);
  240. if p.nodetype=stringconstn then
  241. begin
  242. stringdispose(current_objectdef.iidstr);
  243. current_objectdef.iidstr:=stringdup(strpas(tstringconstnode(p).value_str));
  244. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  245. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  246. not valid then
  247. Message(parser_e_improper_guid_syntax);
  248. include(current_objectdef.objectoptions,oo_has_valid_guid);
  249. end
  250. else
  251. Message(parser_e_illegal_expression);
  252. p.free;
  253. end;
  254. procedure parse_object_options;
  255. begin
  256. if current_objectdef.objecttype in [odt_object,odt_class] then
  257. begin
  258. while true do
  259. begin
  260. if try_to_consume(_ABSTRACT) then
  261. include(current_objectdef.objectoptions,oo_is_abstract)
  262. else
  263. if try_to_consume(_SEALED) then
  264. include(current_objectdef.objectoptions,oo_is_sealed)
  265. else
  266. break;
  267. end;
  268. if [oo_is_abstract, oo_is_sealed] * current_objectdef.objectoptions = [oo_is_abstract, oo_is_sealed] then
  269. Message(parser_e_abstract_and_sealed_conflict);
  270. end;
  271. end;
  272. procedure parse_parent_classes;
  273. var
  274. intfchildof,
  275. childof : tobjectdef;
  276. hdef : tdef;
  277. hasparentdefined : boolean;
  278. begin
  279. childof:=nil;
  280. intfchildof:=nil;
  281. hasparentdefined:=false;
  282. { reads the parent class }
  283. if (token=_LKLAMMER) or
  284. is_objccategory(current_objectdef) then
  285. begin
  286. consume(_LKLAMMER);
  287. { use single_type instead of id_type for specialize support }
  288. single_type(hdef,false,false);
  289. if (not assigned(hdef)) or
  290. (hdef.typ<>objectdef) then
  291. begin
  292. if assigned(hdef) then
  293. Message1(type_e_class_type_expected,hdef.typename)
  294. else if is_objccategory(current_objectdef) then
  295. { a category must specify the class to extend }
  296. Message(type_e_objcclass_type_expected);
  297. end
  298. else
  299. begin
  300. childof:=tobjectdef(hdef);
  301. { a mix of class, interfaces, objects and cppclasses
  302. isn't allowed }
  303. case current_objectdef.objecttype of
  304. odt_class:
  305. if not(is_class(childof)) then
  306. begin
  307. if is_interface(childof) then
  308. begin
  309. { we insert the interface after the child
  310. is set, see below
  311. }
  312. intfchildof:=childof;
  313. childof:=class_tobject;
  314. end
  315. else
  316. Message(parser_e_mix_of_classes_and_objects);
  317. end
  318. else
  319. if oo_is_sealed in childof.objectoptions then
  320. Message1(parser_e_sealed_descendant,childof.typename);
  321. odt_interfacecorba,
  322. odt_interfacecom:
  323. begin
  324. if not(is_interface(childof)) then
  325. Message(parser_e_mix_of_classes_and_objects);
  326. current_objectdef.objecttype:=childof.objecttype;
  327. end;
  328. odt_cppclass:
  329. if not(is_cppclass(childof)) then
  330. Message(parser_e_mix_of_classes_and_objects);
  331. odt_objcclass:
  332. if not(is_objcclass(childof) or
  333. is_objccategory(childof)) then
  334. begin
  335. if is_objcprotocol(childof) then
  336. begin
  337. if not(oo_is_classhelper in current_objectdef.objectoptions) then
  338. begin
  339. intfchildof:=childof;
  340. childof:=nil;
  341. CGMessage(parser_h_no_objc_parent);
  342. end
  343. else
  344. { a category must specify the class to extend }
  345. CGMessage(type_e_objcclass_type_expected);
  346. end
  347. else
  348. Message(parser_e_mix_of_classes_and_objects);
  349. end;
  350. odt_objcprotocol:
  351. begin
  352. if not(is_objcprotocol(childof)) then
  353. Message(parser_e_mix_of_classes_and_objects);
  354. intfchildof:=childof;
  355. childof:=nil;
  356. end;
  357. odt_object:
  358. if not(is_object(childof)) then
  359. Message(parser_e_mix_of_classes_and_objects)
  360. else
  361. if oo_is_sealed in childof.objectoptions then
  362. Message1(parser_e_sealed_descendant,childof.typename);
  363. odt_dispinterface:
  364. Message(parser_e_dispinterface_cant_have_parent);
  365. end;
  366. end;
  367. hasparentdefined:=true;
  368. end;
  369. { no generic as parents }
  370. if assigned(childof) and
  371. (df_generic in childof.defoptions) then
  372. begin
  373. Message(parser_e_no_generics_as_types);
  374. childof:=nil;
  375. end;
  376. { if no parent class, then a class get tobject as parent }
  377. if not assigned(childof) then
  378. begin
  379. case current_objectdef.objecttype of
  380. odt_class:
  381. if current_objectdef<>class_tobject then
  382. childof:=class_tobject;
  383. odt_interfacecom:
  384. if current_objectdef<>interface_iunknown then
  385. childof:=interface_iunknown;
  386. odt_objcclass:
  387. CGMessage(parser_h_no_objc_parent);
  388. end;
  389. end;
  390. if assigned(childof) then
  391. begin
  392. { Forbid not completly defined objects to be used as parents. This will
  393. also prevent circular loops of classes, because we set the forward flag
  394. at the start of the new definition and will reset it below after the
  395. parent has been set }
  396. if not(oo_is_forward in childof.objectoptions) then
  397. current_objectdef.set_parent(childof)
  398. else
  399. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^);
  400. end;
  401. { remove forward flag, is resolved }
  402. exclude(current_objectdef.objectoptions,oo_is_forward);
  403. if hasparentdefined then
  404. begin
  405. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  406. begin
  407. if assigned(intfchildof) then
  408. if current_objectdef.objecttype=odt_class then
  409. handleImplementedInterface(intfchildof)
  410. else
  411. handleImplementedProtocol(intfchildof);
  412. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  413. end;
  414. consume(_RKLAMMER);
  415. end;
  416. end;
  417. procedure parse_guid;
  418. begin
  419. { read GUID }
  420. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  421. try_to_consume(_LECKKLAMMER) then
  422. begin
  423. readinterfaceiid;
  424. consume(_RECKKLAMMER);
  425. end
  426. else if (current_objectdef.objecttype=odt_dispinterface) then
  427. message(parser_e_dispinterface_needs_a_guid);
  428. end;
  429. procedure insert_generic_parameter_types(genericdef:tstoreddef;genericlist:TFPObjectList);
  430. var
  431. i : longint;
  432. generictype : ttypesym;
  433. begin
  434. current_objectdef.genericdef:=genericdef;
  435. if not assigned(genericlist) then
  436. exit;
  437. for i:=0 to genericlist.count-1 do
  438. begin
  439. generictype:=ttypesym(genericlist[i]);
  440. if generictype.typedef.typ=undefineddef then
  441. include(current_objectdef.defoptions,df_generic)
  442. else
  443. include(current_objectdef.defoptions,df_specialization);
  444. symtablestack.top.insert(generictype);
  445. end;
  446. end;
  447. procedure parse_object_members;
  448. procedure chkobjc(pd: tprocdef);
  449. begin
  450. if is_objc_class_or_protocol(pd._class) then
  451. begin
  452. include(pd.procoptions,po_objc);
  453. end;
  454. end;
  455. procedure chkcpp(pd:tprocdef);
  456. begin
  457. { nothing currently }
  458. end;
  459. procedure maybe_parse_hint_directives(pd:tprocdef);
  460. var
  461. dummysymoptions : tsymoptions;
  462. deprecatedmsg : pshortstring;
  463. begin
  464. dummysymoptions:=[];
  465. deprecatedmsg:=nil;
  466. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  467. Consume(_SEMICOLON);
  468. if assigned(pd) then
  469. begin
  470. pd.symoptions:=pd.symoptions+dummysymoptions;
  471. pd.deprecatedmsg:=deprecatedmsg;
  472. end
  473. else
  474. stringdispose(deprecatedmsg);
  475. end;
  476. var
  477. pd : tprocdef;
  478. has_destructor,
  479. oldparse_only,
  480. old_parse_generic : boolean;
  481. object_member_blocktype : tblock_type;
  482. fields_allowed, is_classdef, classfields: boolean;
  483. vdoptions: tvar_dec_options;
  484. begin
  485. { empty class declaration ? }
  486. if (current_objectdef.objecttype in [odt_class,odt_objcclass]) and
  487. (token=_SEMICOLON) then
  488. exit;
  489. old_parse_generic:=parse_generic;
  490. parse_generic:=(df_generic in current_objectdef.defoptions);
  491. { in "publishable" classes the default access type is published }
  492. if (oo_can_have_published in current_objectdef.objectoptions) then
  493. current_objectdef.symtable.currentvisibility:=vis_published
  494. else
  495. current_objectdef.symtable.currentvisibility:=vis_public;
  496. testcurobject:=1;
  497. has_destructor:=false;
  498. fields_allowed:=true;
  499. is_classdef:=false;
  500. classfields:=false;
  501. object_member_blocktype:=bt_general;
  502. repeat
  503. case token of
  504. _TYPE :
  505. begin
  506. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  507. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  508. Message(parser_e_type_var_const_only_in_generics_and_classes);
  509. consume(_TYPE);
  510. object_member_blocktype:=bt_type;
  511. end;
  512. _VAR :
  513. begin
  514. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  515. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  516. Message(parser_e_type_var_const_only_in_generics_and_classes);
  517. consume(_VAR);
  518. fields_allowed:=true;
  519. object_member_blocktype:=bt_general;
  520. classfields:=is_classdef;
  521. is_classdef:=false;
  522. end;
  523. _CONST:
  524. begin
  525. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  526. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  527. Message(parser_e_type_var_const_only_in_generics_and_classes);
  528. consume(_CONST);
  529. object_member_blocktype:=bt_const;
  530. end;
  531. _ID :
  532. begin
  533. if is_objcprotocol(current_objectdef) and
  534. ((idtoken=_REQUIRED) or
  535. (idtoken=_OPTIONAL)) then
  536. begin
  537. current_objectdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  538. consume(idtoken)
  539. end
  540. else case idtoken of
  541. _PRIVATE :
  542. begin
  543. if is_interface(current_objectdef) or
  544. is_objc_protocol_or_category(current_objectdef) then
  545. Message(parser_e_no_access_specifier_in_interfaces);
  546. consume(_PRIVATE);
  547. current_objectdef.symtable.currentvisibility:=vis_private;
  548. include(current_objectdef.objectoptions,oo_has_private);
  549. fields_allowed:=true;
  550. end;
  551. _PROTECTED :
  552. begin
  553. if is_interface(current_objectdef) or
  554. is_objc_protocol_or_category(current_objectdef) then
  555. Message(parser_e_no_access_specifier_in_interfaces);
  556. consume(_PROTECTED);
  557. current_objectdef.symtable.currentvisibility:=vis_protected;
  558. include(current_objectdef.objectoptions,oo_has_protected);
  559. fields_allowed:=true;
  560. end;
  561. _PUBLIC :
  562. begin
  563. if is_interface(current_objectdef) or
  564. is_objc_protocol_or_category(current_objectdef) then
  565. Message(parser_e_no_access_specifier_in_interfaces);
  566. consume(_PUBLIC);
  567. current_objectdef.symtable.currentvisibility:=vis_public;
  568. fields_allowed:=true;
  569. end;
  570. _PUBLISHED :
  571. begin
  572. { we've to check for a pushlished section in non- }
  573. { publishable classes later, if a real declaration }
  574. { this is the way, delphi does it }
  575. if is_interface(current_objectdef) then
  576. Message(parser_e_no_access_specifier_in_interfaces);
  577. { Objective-C classes do not support "published",
  578. as basically everything is published. }
  579. if is_objc_class_or_protocol(current_objectdef) then
  580. Message(parser_e_no_objc_published);
  581. consume(_PUBLISHED);
  582. current_objectdef.symtable.currentvisibility:=vis_published;
  583. fields_allowed:=true;
  584. end;
  585. _STRICT :
  586. begin
  587. if is_interface(current_objectdef) or
  588. is_objc_protocol_or_category(current_objectdef) then
  589. Message(parser_e_no_access_specifier_in_interfaces);
  590. consume(_STRICT);
  591. if token=_ID then
  592. begin
  593. case idtoken of
  594. _PRIVATE:
  595. begin
  596. consume(_PRIVATE);
  597. current_objectdef.symtable.currentvisibility:=vis_strictprivate;
  598. include(current_objectdef.objectoptions,oo_has_strictprivate);
  599. end;
  600. _PROTECTED:
  601. begin
  602. consume(_PROTECTED);
  603. current_objectdef.symtable.currentvisibility:=vis_strictprotected;
  604. include(current_objectdef.objectoptions,oo_has_strictprotected);
  605. end;
  606. else
  607. message(parser_e_protected_or_private_expected);
  608. end;
  609. end
  610. else
  611. message(parser_e_protected_or_private_expected);
  612. fields_allowed:=true;
  613. end;
  614. else
  615. begin
  616. if object_member_blocktype=bt_general then
  617. begin
  618. if is_interface(current_objectdef) or
  619. is_objc_protocol_or_category(current_objectdef) then
  620. Message(parser_e_no_vars_in_interfaces);
  621. if (current_objectdef.symtable.currentvisibility=vis_published) and
  622. not(oo_can_have_published in current_objectdef.objectoptions) then
  623. Message(parser_e_cant_have_published);
  624. if (not fields_allowed) then
  625. Message(parser_e_field_not_allowed_here);
  626. vdoptions:=[vd_object];
  627. if classfields then
  628. include(vdoptions,vd_class);
  629. read_record_fields(vdoptions);
  630. end
  631. else if object_member_blocktype=bt_type then
  632. types_dec(true)
  633. else if object_member_blocktype=bt_const then
  634. consts_dec(true)
  635. else
  636. internalerror(201001110);
  637. end;
  638. end;
  639. end;
  640. _PROPERTY :
  641. begin
  642. property_dec(is_classdef);
  643. fields_allowed:=false;
  644. is_classdef:=false;
  645. end;
  646. _CLASS:
  647. begin
  648. is_classdef:=false;
  649. { read class method }
  650. if try_to_consume(_CLASS) then
  651. begin
  652. { class method only allowed for procedures and functions }
  653. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR]) then
  654. Message(parser_e_procedure_or_function_expected);
  655. if is_interface(current_objectdef) then
  656. Message(parser_e_no_static_method_in_interfaces)
  657. else
  658. { class methods are also allowed for Objective-C protocols }
  659. is_classdef:=true;
  660. end;
  661. end;
  662. _PROCEDURE,
  663. _FUNCTION:
  664. begin
  665. if (current_objectdef.symtable.currentvisibility=vis_published) and
  666. not(oo_can_have_published in current_objectdef.objectoptions) then
  667. Message(parser_e_cant_have_published);
  668. oldparse_only:=parse_only;
  669. parse_only:=true;
  670. pd:=parse_proc_dec(is_classdef, current_objectdef);
  671. { this is for error recovery as well as forward }
  672. { interface mappings, i.e. mapping to a method }
  673. { which isn't declared yet }
  674. if assigned(pd) then
  675. begin
  676. parse_object_proc_directives(pd);
  677. { check if dispid is set }
  678. if is_dispinterface(pd._class) and not (po_dispid in pd.procoptions) then
  679. begin
  680. pd.dispid:=pd._class.get_next_dispid;
  681. include(pd.procoptions, po_dispid);
  682. end;
  683. { all Macintosh Object Pascal methods are virtual. }
  684. { this can't be a class method, because macpas mode }
  685. { has no m_class }
  686. if (m_mac in current_settings.modeswitches) then
  687. include(pd.procoptions,po_virtualmethod);
  688. handle_calling_convention(pd);
  689. { add definition to procsym }
  690. proc_add_definition(pd);
  691. { add procdef options to objectdef options }
  692. if (po_msgint in pd.procoptions) then
  693. include(current_objectdef.objectoptions,oo_has_msgint);
  694. if (po_msgstr in pd.procoptions) then
  695. include(current_objectdef.objectoptions,oo_has_msgstr);
  696. if (po_virtualmethod in pd.procoptions) then
  697. include(current_objectdef.objectoptions,oo_has_virtual);
  698. chkcpp(pd);
  699. chkobjc(pd);
  700. end;
  701. maybe_parse_hint_directives(pd);
  702. parse_only:=oldparse_only;
  703. fields_allowed:=false;
  704. is_classdef:=false;
  705. end;
  706. _CONSTRUCTOR :
  707. begin
  708. if (current_objectdef.symtable.currentvisibility=vis_published) and
  709. not(oo_can_have_published in current_objectdef.objectoptions) then
  710. Message(parser_e_cant_have_published);
  711. if not(current_objectdef.symtable.currentvisibility in [vis_public,vis_published]) then
  712. Message(parser_w_constructor_should_be_public);
  713. if is_interface(current_objectdef) then
  714. Message(parser_e_no_con_des_in_interfaces);
  715. { Objective-C does not know the concept of a constructor }
  716. if is_objc_class_or_protocol(current_objectdef) then
  717. Message(parser_e_objc_no_constructor_destructor);
  718. oldparse_only:=parse_only;
  719. parse_only:=true;
  720. pd:=constructor_head;
  721. parse_object_proc_directives(pd);
  722. handle_calling_convention(pd);
  723. { add definition to procsym }
  724. proc_add_definition(pd);
  725. { add procdef options to objectdef options }
  726. if (po_virtualmethod in pd.procoptions) then
  727. include(current_objectdef.objectoptions,oo_has_virtual);
  728. chkcpp(pd);
  729. maybe_parse_hint_directives(pd);
  730. parse_only:=oldparse_only;
  731. fields_allowed:=false;
  732. end;
  733. _DESTRUCTOR :
  734. begin
  735. if (current_objectdef.symtable.currentvisibility=vis_published) and
  736. not(oo_can_have_published in current_objectdef.objectoptions) then
  737. Message(parser_e_cant_have_published);
  738. if has_destructor then
  739. Message(parser_n_only_one_destructor);
  740. has_destructor:=true;
  741. if is_interface(current_objectdef) then
  742. Message(parser_e_no_con_des_in_interfaces);
  743. if (current_objectdef.symtable.currentvisibility<>vis_public) then
  744. Message(parser_w_destructor_should_be_public);
  745. { Objective-C does not know the concept of a destructor }
  746. if is_objc_class_or_protocol(current_objectdef) then
  747. Message(parser_e_objc_no_constructor_destructor);
  748. oldparse_only:=parse_only;
  749. parse_only:=true;
  750. pd:=destructor_head;
  751. parse_object_proc_directives(pd);
  752. handle_calling_convention(pd);
  753. { add definition to procsym }
  754. proc_add_definition(pd);
  755. { add procdef options to objectdef options }
  756. if (po_virtualmethod in pd.procoptions) then
  757. include(current_objectdef.objectoptions,oo_has_virtual);
  758. chkcpp(pd);
  759. maybe_parse_hint_directives(pd);
  760. parse_only:=oldparse_only;
  761. fields_allowed:=false;
  762. end;
  763. _END :
  764. begin
  765. consume(_END);
  766. break;
  767. end;
  768. else
  769. consume(_ID); { Give a ident expected message, like tp7 }
  770. end;
  771. until false;
  772. { restore }
  773. testcurobject:=0;
  774. parse_generic:=old_parse_generic;
  775. end;
  776. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  777. var
  778. old_current_objectdef : tobjectdef;
  779. begin
  780. old_current_objectdef:=current_objectdef;
  781. current_objectdef:=nil;
  782. { objects and class types can't be declared local }
  783. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable]) and
  784. not assigned(genericlist) then
  785. Message(parser_e_no_local_objects);
  786. { reuse forward objectdef? }
  787. if assigned(fd) then
  788. begin
  789. if fd.objecttype<>objecttype then
  790. begin
  791. Message(parser_e_forward_mismatch);
  792. { recover }
  793. current_objectdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  794. include(current_objectdef.objectoptions,oo_is_forward);
  795. end
  796. else
  797. current_objectdef:=fd
  798. end
  799. else
  800. begin
  801. { anonym objects aren't allow (o : object a : longint; end;) }
  802. if n='' then
  803. Message(parser_f_no_anonym_objects);
  804. { create new class }
  805. current_objectdef:=tobjectdef.create(objecttype,n,nil);
  806. { include always the forward flag, it'll be removed after the parent class have been
  807. added. This is to prevent circular childof loops }
  808. include(current_objectdef.objectoptions,oo_is_forward);
  809. if (cs_compilesystem in current_settings.moduleswitches) then
  810. begin
  811. case current_objectdef.objecttype of
  812. odt_interfacecom :
  813. if (current_objectdef.objname^='IUNKNOWN') then
  814. interface_iunknown:=current_objectdef;
  815. odt_class :
  816. if (current_objectdef.objname^='TOBJECT') then
  817. class_tobject:=current_objectdef;
  818. end;
  819. end;
  820. if (current_module.modulename^='OBJCBASE') then
  821. begin
  822. case current_objectdef.objecttype of
  823. odt_objcclass:
  824. if (current_objectdef.objname^='Protocol') then
  825. objc_protocoltype:=current_objectdef;
  826. end;
  827. end;
  828. end;
  829. { set published flag in $M+ mode, it can also be inherited and will
  830. be added when the parent class set with tobjectdef.set_parent (PFV) }
  831. if (cs_generate_rtti in current_settings.localswitches) and
  832. (current_objectdef.objecttype in [odt_interfacecom,odt_class]) then
  833. include(current_objectdef.objectoptions,oo_can_have_published);
  834. { forward def? }
  835. if not assigned(fd) and
  836. (token=_SEMICOLON) then
  837. begin
  838. { add to the list of definitions to check that the forward
  839. is resolved. this is required for delphi mode }
  840. current_module.checkforwarddefs.add(current_objectdef);
  841. end
  842. else
  843. begin
  844. { change objccategories into objcclass helpers }
  845. if (objecttype=odt_objccategory) then
  846. begin
  847. current_objectdef.objecttype:=odt_objcclass;
  848. include(current_objectdef.objectoptions,oo_is_classhelper);
  849. end;
  850. { parse list of options (abstract / sealed) }
  851. parse_object_options;
  852. { parse list of parent classes }
  853. parse_parent_classes;
  854. { parse optional GUID for interfaces }
  855. parse_guid;
  856. { parse and insert object members }
  857. symtablestack.push(current_objectdef.symtable);
  858. insert_generic_parameter_types(genericdef,genericlist);
  859. parse_object_members;
  860. symtablestack.pop(current_objectdef.symtable);
  861. end;
  862. { generate vmt space if needed }
  863. if not(oo_has_vmt in current_objectdef.objectoptions) and
  864. not(oo_is_forward in current_objectdef.objectoptions) and
  865. (
  866. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_objectdef.objectoptions<>[]) or
  867. (current_objectdef.objecttype in [odt_class])
  868. ) then
  869. current_objectdef.insertvmt;
  870. { for implemented classes with a vmt check if there is a constructor }
  871. if (oo_has_vmt in current_objectdef.objectoptions) and
  872. not(oo_is_forward in current_objectdef.objectoptions) and
  873. not(oo_has_constructor in current_objectdef.objectoptions) and
  874. not is_objc_class_or_protocol(current_objectdef) then
  875. Message1(parser_w_virtual_without_constructor,current_objectdef.objrealname^);
  876. if is_interface(current_objectdef) or
  877. is_objcprotocol(current_objectdef) then
  878. setinterfacemethodoptions
  879. else if is_objcclass(current_objectdef) then
  880. setobjcclassmethodoptions;
  881. { return defined objectdef }
  882. result:=current_objectdef;
  883. { restore old state }
  884. current_objectdef:=old_current_objectdef;
  885. end;
  886. end.