pdecobj.pas 42 KB

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