pdecobj.pas 41 KB

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