pdecobj.pas 42 KB

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