pdecobj.pas 42 KB

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