pdecobj.pas 42 KB

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