pdecobj.pas 41 KB

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