pdecobj.pas 40 KB

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