pdecobj.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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) then
  341. begin
  342. consume(_LKLAMMER);
  343. { use single_type instead of id_type for specialize support }
  344. single_type(hdef,false,false);
  345. if (not assigned(hdef)) or
  346. (hdef.typ<>objectdef) then
  347. begin
  348. if assigned(hdef) then
  349. Message1(type_e_class_type_expected,hdef.typename)
  350. else if is_objccategory(current_objectdef) then
  351. { a category must specify the class to extend }
  352. Message(type_e_objcclass_type_expected);
  353. end
  354. else
  355. begin
  356. childof:=tobjectdef(hdef);
  357. { a mix of class, interfaces, objects and cppclasses
  358. isn't allowed }
  359. case current_objectdef.objecttype of
  360. odt_class:
  361. if not(is_class(childof)) then
  362. begin
  363. if is_interface(childof) then
  364. begin
  365. { we insert the interface after the child
  366. is set, see below
  367. }
  368. intfchildof:=childof;
  369. childof:=class_tobject;
  370. end
  371. else
  372. Message(parser_e_mix_of_classes_and_objects);
  373. end
  374. else
  375. if oo_is_sealed in childof.objectoptions then
  376. Message1(parser_e_sealed_descendant,childof.typename);
  377. odt_interfacecorba,
  378. odt_interfacecom:
  379. begin
  380. if not(is_interface(childof)) then
  381. Message(parser_e_mix_of_classes_and_objects);
  382. current_objectdef.objecttype:=childof.objecttype;
  383. end;
  384. odt_cppclass:
  385. if not(is_cppclass(childof)) then
  386. Message(parser_e_mix_of_classes_and_objects);
  387. odt_objcclass:
  388. if not(is_objcclass(childof) or
  389. is_objccategory(childof)) then
  390. begin
  391. if is_objcprotocol(childof) then
  392. begin
  393. if not(oo_is_classhelper in current_objectdef.objectoptions) then
  394. begin
  395. intfchildof:=childof;
  396. childof:=nil;
  397. CGMessage(parser_h_no_objc_parent);
  398. end
  399. else
  400. { a category must specify the class to extend }
  401. CGMessage(type_e_objcclass_type_expected);
  402. end
  403. else
  404. Message(parser_e_mix_of_classes_and_objects);
  405. end
  406. else
  407. childof:=find_real_objcclass_definition(childof,true);
  408. odt_objcprotocol:
  409. begin
  410. if not(is_objcprotocol(childof)) then
  411. Message(parser_e_mix_of_classes_and_objects);
  412. intfchildof:=childof;
  413. childof:=nil;
  414. end;
  415. odt_object:
  416. if not(is_object(childof)) then
  417. Message(parser_e_mix_of_classes_and_objects)
  418. else
  419. if oo_is_sealed in childof.objectoptions then
  420. Message1(parser_e_sealed_descendant,childof.typename);
  421. odt_dispinterface:
  422. Message(parser_e_dispinterface_cant_have_parent);
  423. end;
  424. end;
  425. hasparentdefined:=true;
  426. end;
  427. { no generic as parents }
  428. if assigned(childof) and
  429. (df_generic in childof.defoptions) then
  430. begin
  431. Message(parser_e_no_generics_as_types);
  432. childof:=nil;
  433. end;
  434. { if no parent class, then a class get tobject as parent }
  435. if not assigned(childof) then
  436. begin
  437. case current_objectdef.objecttype of
  438. odt_class:
  439. if current_objectdef<>class_tobject then
  440. childof:=class_tobject;
  441. odt_interfacecom:
  442. if current_objectdef<>interface_iunknown then
  443. childof:=interface_iunknown;
  444. odt_objcclass:
  445. CGMessage(parser_h_no_objc_parent);
  446. end;
  447. end;
  448. if assigned(childof) then
  449. begin
  450. { Forbid not completly defined objects to be used as parents. This will
  451. also prevent circular loops of classes, because we set the forward flag
  452. at the start of the new definition and will reset it below after the
  453. parent has been set }
  454. if (oo_is_forward in childof.objectoptions) then
  455. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  456. else if not(oo_is_formal in childof.objectoptions) then
  457. current_objectdef.set_parent(childof)
  458. else
  459. Message1(sym_e_objc_formal_class_not_resolved,childof.objrealname^);
  460. end;
  461. { remove forward flag, is resolved }
  462. exclude(current_objectdef.objectoptions,oo_is_forward);
  463. if hasparentdefined then
  464. begin
  465. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  466. begin
  467. if assigned(intfchildof) then
  468. if current_objectdef.objecttype=odt_class then
  469. handleImplementedInterface(intfchildof)
  470. else
  471. handleImplementedProtocol(intfchildof);
  472. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  473. end;
  474. consume(_RKLAMMER);
  475. end;
  476. end;
  477. procedure parse_guid;
  478. begin
  479. { read GUID }
  480. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  481. try_to_consume(_LECKKLAMMER) then
  482. begin
  483. readinterfaceiid;
  484. consume(_RECKKLAMMER);
  485. end
  486. else if (current_objectdef.objecttype=odt_dispinterface) then
  487. message(parser_e_dispinterface_needs_a_guid);
  488. end;
  489. procedure insert_generic_parameter_types(genericdef:tstoreddef;genericlist:TFPObjectList);
  490. var
  491. i : longint;
  492. generictype : ttypesym;
  493. begin
  494. current_objectdef.genericdef:=genericdef;
  495. if not assigned(genericlist) then
  496. exit;
  497. for i:=0 to genericlist.count-1 do
  498. begin
  499. generictype:=ttypesym(genericlist[i]);
  500. if generictype.typedef.typ=undefineddef then
  501. include(current_objectdef.defoptions,df_generic)
  502. else
  503. include(current_objectdef.defoptions,df_specialization);
  504. symtablestack.top.insert(generictype);
  505. end;
  506. end;
  507. procedure parse_object_members;
  508. procedure chkobjc(pd: tprocdef);
  509. begin
  510. if is_objc_class_or_protocol(pd.struct) then
  511. begin
  512. include(pd.procoptions,po_objc);
  513. end;
  514. end;
  515. procedure chkcpp(pd:tprocdef);
  516. begin
  517. { nothing currently }
  518. end;
  519. procedure maybe_parse_hint_directives(pd:tprocdef);
  520. var
  521. dummysymoptions : tsymoptions;
  522. deprecatedmsg : pshortstring;
  523. begin
  524. dummysymoptions:=[];
  525. deprecatedmsg:=nil;
  526. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  527. Consume(_SEMICOLON);
  528. if assigned(pd) then
  529. begin
  530. pd.symoptions:=pd.symoptions+dummysymoptions;
  531. pd.deprecatedmsg:=deprecatedmsg;
  532. end
  533. else
  534. stringdispose(deprecatedmsg);
  535. end;
  536. var
  537. pd : tprocdef;
  538. has_destructor,
  539. oldparse_only,
  540. old_parse_generic : boolean;
  541. object_member_blocktype : tblock_type;
  542. fields_allowed, is_classdef, classfields: boolean;
  543. vdoptions: tvar_dec_options;
  544. begin
  545. { empty class declaration ? }
  546. if (current_objectdef.objecttype in [odt_class,odt_objcclass]) and
  547. (token=_SEMICOLON) then
  548. exit;
  549. old_parse_generic:=parse_generic;
  550. parse_generic:=(df_generic in current_objectdef.defoptions);
  551. { in "publishable" classes the default access type is published }
  552. if (oo_can_have_published in current_objectdef.objectoptions) then
  553. current_objectdef.symtable.currentvisibility:=vis_published
  554. else
  555. current_objectdef.symtable.currentvisibility:=vis_public;
  556. testcurobject:=1;
  557. has_destructor:=false;
  558. fields_allowed:=true;
  559. is_classdef:=false;
  560. classfields:=false;
  561. object_member_blocktype:=bt_general;
  562. repeat
  563. case token of
  564. _TYPE :
  565. begin
  566. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  567. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  568. Message(parser_e_type_var_const_only_in_generics_and_classes);
  569. consume(_TYPE);
  570. object_member_blocktype:=bt_type;
  571. end;
  572. _VAR :
  573. begin
  574. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  575. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  576. Message(parser_e_type_var_const_only_in_generics_and_classes);
  577. consume(_VAR);
  578. fields_allowed:=true;
  579. object_member_blocktype:=bt_general;
  580. classfields:=is_classdef;
  581. is_classdef:=false;
  582. end;
  583. _CONST:
  584. begin
  585. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  586. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  587. Message(parser_e_type_var_const_only_in_generics_and_classes);
  588. consume(_CONST);
  589. object_member_blocktype:=bt_const;
  590. end;
  591. _ID :
  592. begin
  593. if is_objcprotocol(current_objectdef) and
  594. ((idtoken=_REQUIRED) or
  595. (idtoken=_OPTIONAL)) then
  596. begin
  597. current_objectdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  598. consume(idtoken)
  599. end
  600. else case idtoken of
  601. _PRIVATE :
  602. begin
  603. if is_interface(current_objectdef) or
  604. is_objc_protocol_or_category(current_objectdef) then
  605. Message(parser_e_no_access_specifier_in_interfaces);
  606. consume(_PRIVATE);
  607. current_objectdef.symtable.currentvisibility:=vis_private;
  608. include(current_objectdef.objectoptions,oo_has_private);
  609. fields_allowed:=true;
  610. is_classdef:=false;
  611. classfields:=false;
  612. object_member_blocktype:=bt_general;
  613. end;
  614. _PROTECTED :
  615. begin
  616. if is_interface(current_objectdef) or
  617. is_objc_protocol_or_category(current_objectdef) then
  618. Message(parser_e_no_access_specifier_in_interfaces);
  619. consume(_PROTECTED);
  620. current_objectdef.symtable.currentvisibility:=vis_protected;
  621. include(current_objectdef.objectoptions,oo_has_protected);
  622. fields_allowed:=true;
  623. is_classdef:=false;
  624. classfields:=false;
  625. object_member_blocktype:=bt_general;
  626. end;
  627. _PUBLIC :
  628. begin
  629. if is_interface(current_objectdef) or
  630. is_objc_protocol_or_category(current_objectdef) then
  631. Message(parser_e_no_access_specifier_in_interfaces);
  632. consume(_PUBLIC);
  633. current_objectdef.symtable.currentvisibility:=vis_public;
  634. fields_allowed:=true;
  635. is_classdef:=false;
  636. classfields:=false;
  637. object_member_blocktype:=bt_general;
  638. end;
  639. _PUBLISHED :
  640. begin
  641. { we've to check for a pushlished section in non- }
  642. { publishable classes later, if a real declaration }
  643. { this is the way, delphi does it }
  644. if is_interface(current_objectdef) then
  645. Message(parser_e_no_access_specifier_in_interfaces);
  646. { Objective-C classes do not support "published",
  647. as basically everything is published. }
  648. if is_objc_class_or_protocol(current_objectdef) then
  649. Message(parser_e_no_objc_published);
  650. consume(_PUBLISHED);
  651. current_objectdef.symtable.currentvisibility:=vis_published;
  652. fields_allowed:=true;
  653. is_classdef:=false;
  654. classfields:=false;
  655. object_member_blocktype:=bt_general;
  656. end;
  657. _STRICT :
  658. begin
  659. if is_interface(current_objectdef) or
  660. is_objc_protocol_or_category(current_objectdef) then
  661. Message(parser_e_no_access_specifier_in_interfaces);
  662. consume(_STRICT);
  663. if token=_ID then
  664. begin
  665. case idtoken of
  666. _PRIVATE:
  667. begin
  668. consume(_PRIVATE);
  669. current_objectdef.symtable.currentvisibility:=vis_strictprivate;
  670. include(current_objectdef.objectoptions,oo_has_strictprivate);
  671. end;
  672. _PROTECTED:
  673. begin
  674. consume(_PROTECTED);
  675. current_objectdef.symtable.currentvisibility:=vis_strictprotected;
  676. include(current_objectdef.objectoptions,oo_has_strictprotected);
  677. end;
  678. else
  679. message(parser_e_protected_or_private_expected);
  680. end;
  681. end
  682. else
  683. message(parser_e_protected_or_private_expected);
  684. fields_allowed:=true;
  685. is_classdef:=false;
  686. classfields:=false;
  687. object_member_blocktype:=bt_general;
  688. end
  689. else
  690. begin
  691. if object_member_blocktype=bt_general then
  692. begin
  693. if is_interface(current_objectdef) or
  694. is_objc_protocol_or_category(current_objectdef) then
  695. Message(parser_e_no_vars_in_interfaces);
  696. if (current_objectdef.symtable.currentvisibility=vis_published) and
  697. not(oo_can_have_published in current_objectdef.objectoptions) then
  698. Message(parser_e_cant_have_published);
  699. if (not fields_allowed) then
  700. Message(parser_e_field_not_allowed_here);
  701. vdoptions:=[vd_object];
  702. if classfields then
  703. include(vdoptions,vd_class);
  704. read_record_fields(vdoptions);
  705. end
  706. else if object_member_blocktype=bt_type then
  707. types_dec(true)
  708. else if object_member_blocktype=bt_const then
  709. consts_dec(true)
  710. else
  711. internalerror(201001110);
  712. end;
  713. end;
  714. end;
  715. _PROPERTY :
  716. begin
  717. struct_property_dec(is_classdef);
  718. fields_allowed:=false;
  719. is_classdef:=false;
  720. end;
  721. _CLASS:
  722. begin
  723. is_classdef:=false;
  724. { read class method }
  725. if try_to_consume(_CLASS) then
  726. begin
  727. { class modifier is only allowed for procedures, functions, }
  728. { constructors, destructors, fields and properties }
  729. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  730. Message(parser_e_procedure_or_function_expected);
  731. if is_interface(current_objectdef) then
  732. Message(parser_e_no_static_method_in_interfaces)
  733. else
  734. { class methods are also allowed for Objective-C protocols }
  735. is_classdef:=true;
  736. end;
  737. end;
  738. _PROCEDURE,
  739. _FUNCTION:
  740. begin
  741. if (current_objectdef.symtable.currentvisibility=vis_published) and
  742. not(oo_can_have_published in current_objectdef.objectoptions) then
  743. Message(parser_e_cant_have_published);
  744. oldparse_only:=parse_only;
  745. parse_only:=true;
  746. pd:=parse_proc_dec(is_classdef,current_objectdef);
  747. { this is for error recovery as well as forward }
  748. { interface mappings, i.e. mapping to a method }
  749. { which isn't declared yet }
  750. if assigned(pd) then
  751. begin
  752. parse_object_proc_directives(pd);
  753. { check if dispid is set }
  754. if is_dispinterface(pd.struct) and not (po_dispid in pd.procoptions) then
  755. begin
  756. pd.dispid:=tobjectdef(pd.struct).get_next_dispid;
  757. include(pd.procoptions, po_dispid);
  758. end;
  759. { all Macintosh Object Pascal methods are virtual. }
  760. { this can't be a class method, because macpas mode }
  761. { has no m_class }
  762. if (m_mac in current_settings.modeswitches) then
  763. include(pd.procoptions,po_virtualmethod);
  764. handle_calling_convention(pd);
  765. { add definition to procsym }
  766. proc_add_definition(pd);
  767. { add procdef options to objectdef options }
  768. if (po_msgint in pd.procoptions) then
  769. include(current_objectdef.objectoptions,oo_has_msgint);
  770. if (po_msgstr in pd.procoptions) then
  771. include(current_objectdef.objectoptions,oo_has_msgstr);
  772. if (po_virtualmethod in pd.procoptions) then
  773. include(current_objectdef.objectoptions,oo_has_virtual);
  774. chkcpp(pd);
  775. chkobjc(pd);
  776. end;
  777. maybe_parse_hint_directives(pd);
  778. parse_only:=oldparse_only;
  779. fields_allowed:=false;
  780. is_classdef:=false;
  781. end;
  782. _CONSTRUCTOR :
  783. begin
  784. if (current_objectdef.symtable.currentvisibility=vis_published) and
  785. not(oo_can_have_published in current_objectdef.objectoptions) then
  786. Message(parser_e_cant_have_published);
  787. if not is_classdef and not(current_objectdef.symtable.currentvisibility in [vis_public,vis_published]) then
  788. Message(parser_w_constructor_should_be_public);
  789. if is_interface(current_objectdef) then
  790. Message(parser_e_no_con_des_in_interfaces);
  791. { Objective-C does not know the concept of a constructor }
  792. if is_objc_class_or_protocol(current_objectdef) then
  793. Message(parser_e_objc_no_constructor_destructor);
  794. { only 1 class constructor is allowed }
  795. if is_classdef and (oo_has_class_constructor in current_objectdef.objectoptions) then
  796. Message1(parser_e_only_one_class_constructor_allowed, current_objectdef.objrealname^);
  797. oldparse_only:=parse_only;
  798. parse_only:=true;
  799. if is_classdef then
  800. pd:=class_constructor_head
  801. else
  802. pd:=constructor_head;
  803. parse_object_proc_directives(pd);
  804. handle_calling_convention(pd);
  805. { add definition to procsym }
  806. proc_add_definition(pd);
  807. { add procdef options to objectdef options }
  808. if (po_virtualmethod in pd.procoptions) then
  809. include(current_objectdef.objectoptions,oo_has_virtual);
  810. chkcpp(pd);
  811. maybe_parse_hint_directives(pd);
  812. parse_only:=oldparse_only;
  813. fields_allowed:=false;
  814. is_classdef:=false;
  815. end;
  816. _DESTRUCTOR :
  817. begin
  818. if (current_objectdef.symtable.currentvisibility=vis_published) and
  819. not(oo_can_have_published in current_objectdef.objectoptions) then
  820. Message(parser_e_cant_have_published);
  821. if not is_classdef then
  822. if has_destructor then
  823. Message(parser_n_only_one_destructor)
  824. else
  825. has_destructor:=true;
  826. if is_interface(current_objectdef) then
  827. Message(parser_e_no_con_des_in_interfaces);
  828. if not is_classdef and (current_objectdef.symtable.currentvisibility<>vis_public) then
  829. Message(parser_w_destructor_should_be_public);
  830. { Objective-C does not know the concept of a destructor }
  831. if is_objc_class_or_protocol(current_objectdef) then
  832. Message(parser_e_objc_no_constructor_destructor);
  833. { only 1 class destructor is allowed }
  834. if is_classdef and (oo_has_class_destructor in current_objectdef.objectoptions) then
  835. Message1(parser_e_only_one_class_destructor_allowed, current_objectdef.objrealname^);
  836. oldparse_only:=parse_only;
  837. parse_only:=true;
  838. if is_classdef then
  839. pd:=class_destructor_head
  840. else
  841. pd:=destructor_head;
  842. parse_object_proc_directives(pd);
  843. handle_calling_convention(pd);
  844. { add definition to procsym }
  845. proc_add_definition(pd);
  846. { add procdef options to objectdef options }
  847. if (po_virtualmethod in pd.procoptions) then
  848. include(current_objectdef.objectoptions,oo_has_virtual);
  849. chkcpp(pd);
  850. maybe_parse_hint_directives(pd);
  851. parse_only:=oldparse_only;
  852. fields_allowed:=false;
  853. is_classdef:=false;
  854. end;
  855. _END :
  856. begin
  857. consume(_END);
  858. break;
  859. end;
  860. else
  861. consume(_ID); { Give a ident expected message, like tp7 }
  862. end;
  863. until false;
  864. { restore }
  865. testcurobject:=0;
  866. parse_generic:=old_parse_generic;
  867. end;
  868. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  869. var
  870. old_current_objectdef : tobjectdef;
  871. old_current_genericdef : tobjectdef;
  872. old_current_specializedef : tobjectdef;
  873. begin
  874. old_current_objectdef:=current_objectdef;
  875. old_current_genericdef:=current_genericdef;
  876. old_current_specializedef:=current_specializedef;
  877. current_objectdef:=nil;
  878. current_genericdef:=nil;
  879. current_specializedef:=nil;
  880. { objects and class types can't be declared local }
  881. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable]) and
  882. not assigned(genericlist) then
  883. Message(parser_e_no_local_objects);
  884. { reuse forward objectdef? }
  885. if assigned(fd) then
  886. begin
  887. if fd.objecttype<>objecttype then
  888. begin
  889. Message(parser_e_forward_mismatch);
  890. { recover }
  891. current_objectdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  892. include(current_objectdef.objectoptions,oo_is_forward);
  893. end
  894. else
  895. current_objectdef:=fd
  896. end
  897. else
  898. begin
  899. { anonym objects aren't allow (o : object a : longint; end;) }
  900. if n='' then
  901. Message(parser_f_no_anonym_objects);
  902. { create new class }
  903. current_objectdef:=tobjectdef.create(objecttype,n,nil);
  904. { include always the forward flag, it'll be removed after the parent class have been
  905. added. This is to prevent circular childof loops }
  906. include(current_objectdef.objectoptions,oo_is_forward);
  907. if (cs_compilesystem in current_settings.moduleswitches) then
  908. begin
  909. case current_objectdef.objecttype of
  910. odt_interfacecom :
  911. if (current_objectdef.objname^='IUNKNOWN') then
  912. interface_iunknown:=current_objectdef;
  913. odt_class :
  914. if (current_objectdef.objname^='TOBJECT') then
  915. class_tobject:=current_objectdef;
  916. end;
  917. end;
  918. if (current_module.modulename^='OBJCBASE') then
  919. begin
  920. case current_objectdef.objecttype of
  921. odt_objcclass:
  922. if (current_objectdef.objname^='Protocol') then
  923. objc_protocoltype:=current_objectdef;
  924. end;
  925. end;
  926. end;
  927. { usage of specialized type inside its generic template }
  928. if assigned(genericdef) then
  929. current_specializedef:=current_objectdef
  930. { reject declaration of generic class inside generic class }
  931. else if assigned(genericlist) then
  932. current_genericdef:=current_objectdef;
  933. { set published flag in $M+ mode, it can also be inherited and will
  934. be added when the parent class set with tobjectdef.set_parent (PFV) }
  935. if (cs_generate_rtti in current_settings.localswitches) and
  936. (current_objectdef.objecttype in [odt_interfacecom,odt_class]) then
  937. include(current_objectdef.objectoptions,oo_can_have_published);
  938. { forward def? }
  939. if not assigned(fd) and
  940. (token=_SEMICOLON) then
  941. begin
  942. { add to the list of definitions to check that the forward
  943. is resolved. this is required for delphi mode }
  944. current_module.checkforwarddefs.add(current_objectdef);
  945. end
  946. else
  947. begin
  948. { change objccategories into objcclass helpers }
  949. if (objecttype=odt_objccategory) then
  950. begin
  951. current_objectdef.objecttype:=odt_objcclass;
  952. include(current_objectdef.objectoptions,oo_is_classhelper);
  953. end;
  954. { parse list of options (abstract / sealed) }
  955. parse_object_options;
  956. { parse list of parent classes }
  957. parse_parent_classes;
  958. { parse optional GUID for interfaces }
  959. parse_guid;
  960. symtablestack.push(current_objectdef.symtable);
  961. insert_generic_parameter_types(genericdef,genericlist);
  962. { parse and insert object members }
  963. parse_object_members;
  964. symtablestack.pop(current_objectdef.symtable);
  965. end;
  966. { generate vmt space if needed }
  967. if not(oo_has_vmt in current_objectdef.objectoptions) and
  968. not(oo_is_forward in current_objectdef.objectoptions) and
  969. (
  970. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_objectdef.objectoptions<>[]) or
  971. (current_objectdef.objecttype in [odt_class])
  972. ) then
  973. current_objectdef.insertvmt;
  974. { for implemented classes with a vmt check if there is a constructor }
  975. if (oo_has_vmt in current_objectdef.objectoptions) and
  976. not(oo_is_forward in current_objectdef.objectoptions) and
  977. not(oo_has_constructor in current_objectdef.objectoptions) and
  978. not is_objc_class_or_protocol(current_objectdef) then
  979. Message1(parser_w_virtual_without_constructor,current_objectdef.objrealname^);
  980. if is_interface(current_objectdef) or
  981. is_objcprotocol(current_objectdef) then
  982. setinterfacemethodoptions
  983. else if is_objcclass(current_objectdef) then
  984. setobjcclassmethodoptions;
  985. { return defined objectdef }
  986. result:=current_objectdef;
  987. { restore old state }
  988. current_objectdef:=old_current_objectdef;
  989. current_genericdef:=old_current_genericdef;
  990. current_specializedef:=old_current_specializedef;
  991. end;
  992. end.