pdecobj.pas 43 KB

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