pdecobj.pas 42 KB

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