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