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. 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 (oo_is_forward in intfdef.objectoptions) then
  258. begin
  259. Message1(parser_e_forward_protocol_declaration_must_be_resolved,intfdef.objrealname^);
  260. exit;
  261. end;
  262. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  263. Message1(sym_e_duplicate_id,intfdef.objname^)
  264. else
  265. begin
  266. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  267. end;
  268. end;
  269. procedure readImplementedInterfacesAndProtocols(intf: boolean);
  270. var
  271. hdef : tdef;
  272. begin
  273. while try_to_consume(_COMMA) do
  274. begin
  275. id_type(hdef,false);
  276. if (hdef.typ<>objectdef) then
  277. begin
  278. if intf then
  279. Message1(type_e_interface_type_expected,hdef.typename)
  280. else
  281. Message1(type_e_protocol_type_expected,hdef.typename);
  282. continue;
  283. end;
  284. if intf then
  285. handleImplementedInterface(tobjectdef(hdef))
  286. else
  287. handleImplementedProtocol(tobjectdef(hdef));
  288. end;
  289. end;
  290. procedure readinterfaceiid;
  291. var
  292. p : tnode;
  293. valid : boolean;
  294. begin
  295. p:=comp_expr(true,false);
  296. if p.nodetype=stringconstn then
  297. begin
  298. stringdispose(current_objectdef.iidstr);
  299. current_objectdef.iidstr:=stringdup(strpas(tstringconstnode(p).value_str));
  300. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  301. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  302. not valid then
  303. Message(parser_e_improper_guid_syntax);
  304. include(current_objectdef.objectoptions,oo_has_valid_guid);
  305. end
  306. else
  307. Message(parser_e_illegal_expression);
  308. p.free;
  309. end;
  310. procedure parse_object_options;
  311. begin
  312. if current_objectdef.objecttype in [odt_object,odt_class] then
  313. begin
  314. while true do
  315. begin
  316. if try_to_consume(_ABSTRACT) then
  317. include(current_objectdef.objectoptions,oo_is_abstract)
  318. else
  319. if try_to_consume(_SEALED) then
  320. include(current_objectdef.objectoptions,oo_is_sealed)
  321. else
  322. break;
  323. end;
  324. if [oo_is_abstract, oo_is_sealed] * current_objectdef.objectoptions = [oo_is_abstract, oo_is_sealed] then
  325. Message(parser_e_abstract_and_sealed_conflict);
  326. end;
  327. end;
  328. procedure parse_parent_classes;
  329. var
  330. intfchildof,
  331. childof : tobjectdef;
  332. hdef : tdef;
  333. hasparentdefined : boolean;
  334. begin
  335. childof:=nil;
  336. intfchildof:=nil;
  337. hasparentdefined:=false;
  338. { reads the parent class }
  339. if (token=_LKLAMMER) or
  340. is_objccategory(current_objectdef) then
  341. begin
  342. consume(_LKLAMMER);
  343. { use single_type instead of id_type for specialize support }
  344. single_type(hdef,false,false);
  345. if (not assigned(hdef)) or
  346. (hdef.typ<>objectdef) then
  347. begin
  348. if assigned(hdef) then
  349. Message1(type_e_class_type_expected,hdef.typename)
  350. else if is_objccategory(current_objectdef) then
  351. { a category must specify the class to extend }
  352. Message(type_e_objcclass_type_expected);
  353. end
  354. else
  355. begin
  356. childof:=tobjectdef(hdef);
  357. { a mix of class, interfaces, objects and cppclasses
  358. isn't allowed }
  359. case current_objectdef.objecttype of
  360. odt_class:
  361. if not(is_class(childof)) then
  362. begin
  363. if is_interface(childof) then
  364. begin
  365. { we insert the interface after the child
  366. is set, see below
  367. }
  368. intfchildof:=childof;
  369. childof:=class_tobject;
  370. end
  371. else
  372. Message(parser_e_mix_of_classes_and_objects);
  373. end
  374. else
  375. if oo_is_sealed in childof.objectoptions then
  376. Message1(parser_e_sealed_descendant,childof.typename);
  377. odt_interfacecorba,
  378. odt_interfacecom:
  379. begin
  380. if not(is_interface(childof)) then
  381. Message(parser_e_mix_of_classes_and_objects);
  382. current_objectdef.objecttype:=childof.objecttype;
  383. end;
  384. odt_cppclass:
  385. if not(is_cppclass(childof)) then
  386. Message(parser_e_mix_of_classes_and_objects);
  387. odt_objcclass:
  388. if not(is_objcclass(childof) or
  389. is_objccategory(childof)) then
  390. begin
  391. if is_objcprotocol(childof) then
  392. begin
  393. if not(oo_is_classhelper in current_objectdef.objectoptions) then
  394. begin
  395. intfchildof:=childof;
  396. childof:=nil;
  397. CGMessage(parser_h_no_objc_parent);
  398. end
  399. else
  400. { a category must specify the class to extend }
  401. CGMessage(type_e_objcclass_type_expected);
  402. end
  403. else
  404. Message(parser_e_mix_of_classes_and_objects);
  405. end
  406. else
  407. childof:=find_real_objcclass_definition(childof,true);
  408. odt_objcprotocol:
  409. begin
  410. if not(is_objcprotocol(childof)) then
  411. Message(parser_e_mix_of_classes_and_objects);
  412. intfchildof:=childof;
  413. childof:=nil;
  414. end;
  415. odt_object:
  416. if not(is_object(childof)) then
  417. Message(parser_e_mix_of_classes_and_objects)
  418. else
  419. if oo_is_sealed in childof.objectoptions then
  420. Message1(parser_e_sealed_descendant,childof.typename);
  421. odt_dispinterface:
  422. Message(parser_e_dispinterface_cant_have_parent);
  423. end;
  424. end;
  425. hasparentdefined:=true;
  426. end;
  427. { no generic as parents }
  428. if assigned(childof) and
  429. (df_generic in childof.defoptions) then
  430. begin
  431. Message(parser_e_no_generics_as_types);
  432. childof:=nil;
  433. end;
  434. { if no parent class, then a class get tobject as parent }
  435. if not assigned(childof) then
  436. begin
  437. case current_objectdef.objecttype of
  438. odt_class:
  439. if current_objectdef<>class_tobject then
  440. childof:=class_tobject;
  441. odt_interfacecom:
  442. if current_objectdef<>interface_iunknown then
  443. childof:=interface_iunknown;
  444. odt_objcclass:
  445. CGMessage(parser_h_no_objc_parent);
  446. end;
  447. end;
  448. if assigned(childof) then
  449. begin
  450. { Forbid not completly defined objects to be used as parents. This will
  451. also prevent circular loops of classes, because we set the forward flag
  452. at the start of the new definition and will reset it below after the
  453. parent has been set }
  454. if (oo_is_forward in childof.objectoptions) then
  455. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  456. else if not(oo_is_formal in childof.objectoptions) then
  457. current_objectdef.set_parent(childof)
  458. else
  459. Message1(sym_e_objc_formal_class_not_resolved,childof.objrealname^);
  460. end;
  461. { remove forward flag, is resolved }
  462. exclude(current_objectdef.objectoptions,oo_is_forward);
  463. if hasparentdefined then
  464. begin
  465. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  466. begin
  467. if assigned(intfchildof) then
  468. if current_objectdef.objecttype=odt_class then
  469. handleImplementedInterface(intfchildof)
  470. else
  471. handleImplementedProtocol(intfchildof);
  472. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  473. end;
  474. consume(_RKLAMMER);
  475. end;
  476. end;
  477. procedure parse_guid;
  478. begin
  479. { read GUID }
  480. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  481. try_to_consume(_LECKKLAMMER) then
  482. begin
  483. readinterfaceiid;
  484. consume(_RECKKLAMMER);
  485. end
  486. else if (current_objectdef.objecttype=odt_dispinterface) then
  487. message(parser_e_dispinterface_needs_a_guid);
  488. end;
  489. procedure insert_generic_parameter_types(genericdef:tstoreddef;genericlist:TFPObjectList);
  490. var
  491. i : longint;
  492. generictype : ttypesym;
  493. begin
  494. current_objectdef.genericdef:=genericdef;
  495. if not assigned(genericlist) then
  496. exit;
  497. for i:=0 to genericlist.count-1 do
  498. begin
  499. generictype:=ttypesym(genericlist[i]);
  500. if generictype.typedef.typ=undefineddef then
  501. include(current_objectdef.defoptions,df_generic)
  502. else
  503. include(current_objectdef.defoptions,df_specialization);
  504. symtablestack.top.insert(generictype);
  505. end;
  506. end;
  507. procedure parse_object_members;
  508. procedure chkobjc(pd: tprocdef);
  509. begin
  510. if is_objc_class_or_protocol(pd.struct) then
  511. begin
  512. include(pd.procoptions,po_objc);
  513. end;
  514. end;
  515. procedure chkcpp(pd:tprocdef);
  516. begin
  517. { nothing currently }
  518. end;
  519. procedure maybe_parse_hint_directives(pd:tprocdef);
  520. var
  521. dummysymoptions : tsymoptions;
  522. deprecatedmsg : pshortstring;
  523. begin
  524. dummysymoptions:=[];
  525. deprecatedmsg:=nil;
  526. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  527. Consume(_SEMICOLON);
  528. if assigned(pd) then
  529. begin
  530. pd.symoptions:=pd.symoptions+dummysymoptions;
  531. pd.deprecatedmsg:=deprecatedmsg;
  532. end
  533. else
  534. stringdispose(deprecatedmsg);
  535. end;
  536. var
  537. pd : tprocdef;
  538. has_destructor,
  539. oldparse_only,
  540. old_parse_generic : boolean;
  541. object_member_blocktype : tblock_type;
  542. fields_allowed, is_classdef, classfields: boolean;
  543. vdoptions: tvar_dec_options;
  544. begin
  545. { empty class declaration ? }
  546. if (current_objectdef.objecttype in [odt_class,odt_objcclass]) and
  547. (token=_SEMICOLON) then
  548. exit;
  549. old_parse_generic:=parse_generic;
  550. parse_generic:=(df_generic in current_objectdef.defoptions);
  551. { in "publishable" classes the default access type is published }
  552. if (oo_can_have_published in current_objectdef.objectoptions) then
  553. current_objectdef.symtable.currentvisibility:=vis_published
  554. else
  555. current_objectdef.symtable.currentvisibility:=vis_public;
  556. has_destructor:=false;
  557. fields_allowed:=true;
  558. is_classdef:=false;
  559. classfields:=false;
  560. object_member_blocktype:=bt_general;
  561. repeat
  562. case token of
  563. _TYPE :
  564. begin
  565. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  566. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  567. Message(parser_e_type_var_const_only_in_generics_and_classes);
  568. consume(_TYPE);
  569. object_member_blocktype:=bt_type;
  570. end;
  571. _VAR :
  572. begin
  573. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  574. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  575. Message(parser_e_type_var_const_only_in_generics_and_classes);
  576. consume(_VAR);
  577. fields_allowed:=true;
  578. object_member_blocktype:=bt_general;
  579. classfields:=is_classdef;
  580. is_classdef:=false;
  581. end;
  582. _CONST:
  583. begin
  584. if (([df_generic,df_specialization]*current_objectdef.defoptions)=[]) and
  585. not(current_objectdef.objecttype in [odt_class,odt_object]) then
  586. Message(parser_e_type_var_const_only_in_generics_and_classes);
  587. consume(_CONST);
  588. object_member_blocktype:=bt_const;
  589. end;
  590. _ID :
  591. begin
  592. if is_objcprotocol(current_objectdef) and
  593. ((idtoken=_REQUIRED) or
  594. (idtoken=_OPTIONAL)) then
  595. begin
  596. current_objectdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  597. consume(idtoken)
  598. end
  599. else case idtoken of
  600. _PRIVATE :
  601. begin
  602. if is_interface(current_objectdef) or
  603. is_objc_protocol_or_category(current_objectdef) then
  604. Message(parser_e_no_access_specifier_in_interfaces);
  605. consume(_PRIVATE);
  606. current_objectdef.symtable.currentvisibility:=vis_private;
  607. include(current_objectdef.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_objectdef) or
  616. is_objc_protocol_or_category(current_objectdef) then
  617. Message(parser_e_no_access_specifier_in_interfaces);
  618. consume(_PROTECTED);
  619. current_objectdef.symtable.currentvisibility:=vis_protected;
  620. include(current_objectdef.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_objectdef) or
  629. is_objc_protocol_or_category(current_objectdef) then
  630. Message(parser_e_no_access_specifier_in_interfaces);
  631. consume(_PUBLIC);
  632. current_objectdef.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_objectdef) 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_objectdef) then
  648. Message(parser_e_no_objc_published);
  649. consume(_PUBLISHED);
  650. current_objectdef.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_objectdef) or
  659. is_objc_protocol_or_category(current_objectdef) 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_objectdef.symtable.currentvisibility:=vis_strictprivate;
  669. include(current_objectdef.objectoptions,oo_has_strictprivate);
  670. end;
  671. _PROTECTED:
  672. begin
  673. consume(_PROTECTED);
  674. current_objectdef.symtable.currentvisibility:=vis_strictprotected;
  675. include(current_objectdef.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_objectdef) or
  693. is_objc_protocol_or_category(current_objectdef) then
  694. Message(parser_e_no_vars_in_interfaces);
  695. if (current_objectdef.symtable.currentvisibility=vis_published) and
  696. not(oo_can_have_published in current_objectdef.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_objectdef) 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_objectdef.symtable.currentvisibility=vis_published) and
  741. not(oo_can_have_published in current_objectdef.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_objectdef);
  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_objectdef.objectoptions,oo_has_msgint);
  769. if (po_msgstr in pd.procoptions) then
  770. include(current_objectdef.objectoptions,oo_has_msgstr);
  771. if (po_virtualmethod in pd.procoptions) then
  772. include(current_objectdef.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_objectdef.symtable.currentvisibility=vis_published) and
  784. not(oo_can_have_published in current_objectdef.objectoptions) then
  785. Message(parser_e_cant_have_published);
  786. if not is_classdef and not(current_objectdef.symtable.currentvisibility in [vis_public,vis_published]) then
  787. Message(parser_w_constructor_should_be_public);
  788. if is_interface(current_objectdef) 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_objectdef) 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_objectdef.objectoptions) then
  795. Message1(parser_e_only_one_class_constructor_allowed, current_objectdef.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_objectdef.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_objectdef.symtable.currentvisibility=vis_published) and
  818. not(oo_can_have_published in current_objectdef.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_objectdef) then
  826. Message(parser_e_no_con_des_in_interfaces);
  827. if not is_classdef and (current_objectdef.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_objectdef) 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_objectdef.objectoptions) then
  834. Message1(parser_e_only_one_class_destructor_allowed, current_objectdef.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_objectdef.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_objectdef : tobjectdef;
  869. old_current_genericdef : tobjectdef;
  870. old_current_specializedef : tobjectdef;
  871. begin
  872. old_current_objectdef:=current_objectdef;
  873. old_current_genericdef:=current_genericdef;
  874. old_current_specializedef:=current_specializedef;
  875. current_objectdef:=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_objectdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  890. include(current_objectdef.objectoptions,oo_is_forward);
  891. end
  892. else
  893. current_objectdef:=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_objectdef:=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_objectdef.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_objectdef.objname^='IUNKNOWN') then
  910. interface_iunknown:=current_objectdef;
  911. odt_class :
  912. if (current_objectdef.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_objectdef
  928. { reject declaration of generic class inside generic class }
  929. else if assigned(genericlist) then
  930. current_genericdef:=current_objectdef;
  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_objectdef.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_objectdef);
  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_objectdef.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_objectdef.symtable);
  959. insert_generic_parameter_types(genericdef,genericlist);
  960. { parse and insert object members }
  961. parse_object_members;
  962. symtablestack.pop(current_objectdef.symtable);
  963. end;
  964. { generate vmt space if needed }
  965. if not(oo_has_vmt in current_objectdef.objectoptions) and
  966. not(oo_is_forward in current_objectdef.objectoptions) and
  967. (
  968. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_objectdef.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_objectdef.objectoptions) and
  974. not(oo_is_forward in current_objectdef.objectoptions) and
  975. not(oo_has_constructor in current_objectdef.objectoptions) and
  976. not is_objc_class_or_protocol(current_objectdef) then
  977. Message1(parser_w_virtual_without_constructor,current_objectdef.objrealname^);
  978. if is_interface(current_objectdef) or
  979. is_objcprotocol(current_objectdef) then
  980. setinterfacemethodoptions
  981. else if is_objcclass(current_objectdef) then
  982. setobjcclassmethodoptions;
  983. { return defined objectdef }
  984. result:=current_objectdef;
  985. { restore old state }
  986. current_objectdef:=old_current_objectdef;
  987. current_genericdef:=old_current_genericdef;
  988. current_specializedef:=old_current_specializedef;
  989. end;
  990. end.