pdecobj.pas 40 KB

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