pdecobj.pas 40 KB

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