pdecobj.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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(def:tstoreddef;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(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  493. var
  494. i: longint;
  495. generictype: ttypesym;
  496. st: tsymtable;
  497. begin
  498. def.genericdef:=genericdef;
  499. if not assigned(genericlist) then
  500. exit;
  501. case def.typ of
  502. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  503. arraydef: st:=tarraydef(def).symtable;
  504. else
  505. internalerror(201101020);
  506. end;
  507. for i:=0 to genericlist.count-1 do
  508. begin
  509. generictype:=ttypesym(genericlist[i]);
  510. if generictype.typedef.typ=undefineddef then
  511. include(def.defoptions,df_generic)
  512. else
  513. include(def.defoptions,df_specialization);
  514. st.insert(generictype);
  515. end;
  516. end;
  517. procedure parse_object_members;
  518. procedure chkobjc(pd: tprocdef);
  519. begin
  520. if is_objc_class_or_protocol(pd.struct) then
  521. begin
  522. include(pd.procoptions,po_objc);
  523. end;
  524. end;
  525. procedure chkcpp(pd:tprocdef);
  526. begin
  527. { nothing currently }
  528. end;
  529. procedure maybe_parse_hint_directives(pd:tprocdef);
  530. var
  531. dummysymoptions : tsymoptions;
  532. deprecatedmsg : pshortstring;
  533. begin
  534. dummysymoptions:=[];
  535. deprecatedmsg:=nil;
  536. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  537. Consume(_SEMICOLON);
  538. if assigned(pd) then
  539. begin
  540. pd.symoptions:=pd.symoptions+dummysymoptions;
  541. pd.deprecatedmsg:=deprecatedmsg;
  542. end
  543. else
  544. stringdispose(deprecatedmsg);
  545. end;
  546. var
  547. pd : tprocdef;
  548. has_destructor,
  549. oldparse_only,
  550. old_parse_generic: boolean;
  551. object_member_blocktype : tblock_type;
  552. fields_allowed, is_classdef, classfields: boolean;
  553. vdoptions: tvar_dec_options;
  554. begin
  555. { empty class declaration ? }
  556. if (current_objectdef.objecttype in [odt_class,odt_objcclass]) and
  557. (token=_SEMICOLON) then
  558. exit;
  559. old_parse_generic:=parse_generic;
  560. parse_generic:=(df_generic in current_structdef.defoptions);
  561. { in "publishable" classes the default access type is published }
  562. if (oo_can_have_published in current_structdef.objectoptions) then
  563. current_structdef.symtable.currentvisibility:=vis_published
  564. else
  565. current_structdef.symtable.currentvisibility:=vis_public;
  566. has_destructor:=false;
  567. fields_allowed:=true;
  568. is_classdef:=false;
  569. classfields:=false;
  570. object_member_blocktype:=bt_general;
  571. repeat
  572. case token of
  573. _TYPE :
  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(_TYPE);
  578. object_member_blocktype:=bt_type;
  579. end;
  580. _VAR :
  581. begin
  582. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  583. Message(parser_e_type_var_const_only_in_records_and_classes);
  584. consume(_VAR);
  585. fields_allowed:=true;
  586. object_member_blocktype:=bt_general;
  587. classfields:=is_classdef;
  588. is_classdef:=false;
  589. end;
  590. _CONST:
  591. begin
  592. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  593. Message(parser_e_type_var_const_only_in_records_and_classes);
  594. consume(_CONST);
  595. object_member_blocktype:=bt_const;
  596. end;
  597. _ID :
  598. begin
  599. if is_objcprotocol(current_structdef) and
  600. ((idtoken=_REQUIRED) or
  601. (idtoken=_OPTIONAL)) then
  602. begin
  603. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  604. consume(idtoken)
  605. end
  606. else case idtoken of
  607. _PRIVATE :
  608. begin
  609. if is_interface(current_structdef) or
  610. is_objc_protocol_or_category(current_structdef) then
  611. Message(parser_e_no_access_specifier_in_interfaces);
  612. consume(_PRIVATE);
  613. current_structdef.symtable.currentvisibility:=vis_private;
  614. include(current_structdef.objectoptions,oo_has_private);
  615. fields_allowed:=true;
  616. is_classdef:=false;
  617. classfields:=false;
  618. object_member_blocktype:=bt_general;
  619. end;
  620. _PROTECTED :
  621. begin
  622. if is_interface(current_structdef) or
  623. is_objc_protocol_or_category(current_structdef) then
  624. Message(parser_e_no_access_specifier_in_interfaces);
  625. consume(_PROTECTED);
  626. current_structdef.symtable.currentvisibility:=vis_protected;
  627. include(current_structdef.objectoptions,oo_has_protected);
  628. fields_allowed:=true;
  629. is_classdef:=false;
  630. classfields:=false;
  631. object_member_blocktype:=bt_general;
  632. end;
  633. _PUBLIC :
  634. begin
  635. if is_interface(current_structdef) or
  636. is_objc_protocol_or_category(current_structdef) then
  637. Message(parser_e_no_access_specifier_in_interfaces);
  638. consume(_PUBLIC);
  639. current_structdef.symtable.currentvisibility:=vis_public;
  640. fields_allowed:=true;
  641. is_classdef:=false;
  642. classfields:=false;
  643. object_member_blocktype:=bt_general;
  644. end;
  645. _PUBLISHED :
  646. begin
  647. { we've to check for a pushlished section in non- }
  648. { publishable classes later, if a real declaration }
  649. { this is the way, delphi does it }
  650. if is_interface(current_structdef) then
  651. Message(parser_e_no_access_specifier_in_interfaces);
  652. { Objective-C classes do not support "published",
  653. as basically everything is published. }
  654. if is_objc_class_or_protocol(current_structdef) then
  655. Message(parser_e_no_objc_published);
  656. consume(_PUBLISHED);
  657. current_structdef.symtable.currentvisibility:=vis_published;
  658. fields_allowed:=true;
  659. is_classdef:=false;
  660. classfields:=false;
  661. object_member_blocktype:=bt_general;
  662. end;
  663. _STRICT :
  664. begin
  665. if is_interface(current_structdef) or
  666. is_objc_protocol_or_category(current_structdef) then
  667. Message(parser_e_no_access_specifier_in_interfaces);
  668. consume(_STRICT);
  669. if token=_ID then
  670. begin
  671. case idtoken of
  672. _PRIVATE:
  673. begin
  674. consume(_PRIVATE);
  675. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  676. include(current_structdef.objectoptions,oo_has_strictprivate);
  677. end;
  678. _PROTECTED:
  679. begin
  680. consume(_PROTECTED);
  681. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  682. include(current_structdef.objectoptions,oo_has_strictprotected);
  683. end;
  684. else
  685. message(parser_e_protected_or_private_expected);
  686. end;
  687. end
  688. else
  689. message(parser_e_protected_or_private_expected);
  690. fields_allowed:=true;
  691. is_classdef:=false;
  692. classfields:=false;
  693. object_member_blocktype:=bt_general;
  694. end
  695. else
  696. begin
  697. if object_member_blocktype=bt_general then
  698. begin
  699. if is_interface(current_structdef) or
  700. is_objc_protocol_or_category(current_structdef) then
  701. Message(parser_e_no_vars_in_interfaces);
  702. if (current_structdef.symtable.currentvisibility=vis_published) and
  703. not(oo_can_have_published in current_structdef.objectoptions) then
  704. Message(parser_e_cant_have_published);
  705. if (not fields_allowed) then
  706. Message(parser_e_field_not_allowed_here);
  707. vdoptions:=[vd_object];
  708. if classfields then
  709. include(vdoptions,vd_class);
  710. read_record_fields(vdoptions);
  711. end
  712. else if object_member_blocktype=bt_type then
  713. types_dec(true)
  714. else if object_member_blocktype=bt_const then
  715. consts_dec(true)
  716. else
  717. internalerror(201001110);
  718. end;
  719. end;
  720. end;
  721. _PROPERTY :
  722. begin
  723. struct_property_dec(is_classdef);
  724. fields_allowed:=false;
  725. is_classdef:=false;
  726. end;
  727. _CLASS:
  728. begin
  729. is_classdef:=false;
  730. { read class method }
  731. if try_to_consume(_CLASS) then
  732. begin
  733. { class modifier is only allowed for procedures, functions, }
  734. { constructors, destructors, fields and properties }
  735. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  736. Message(parser_e_procedure_or_function_expected);
  737. if is_interface(current_structdef) then
  738. Message(parser_e_no_static_method_in_interfaces)
  739. else
  740. { class methods are also allowed for Objective-C protocols }
  741. is_classdef:=true;
  742. end;
  743. end;
  744. _PROCEDURE,
  745. _FUNCTION:
  746. begin
  747. if (current_structdef.symtable.currentvisibility=vis_published) and
  748. not(oo_can_have_published in current_structdef.objectoptions) then
  749. Message(parser_e_cant_have_published);
  750. oldparse_only:=parse_only;
  751. parse_only:=true;
  752. pd:=parse_proc_dec(is_classdef,current_structdef);
  753. { this is for error recovery as well as forward }
  754. { interface mappings, i.e. mapping to a method }
  755. { which isn't declared yet }
  756. if assigned(pd) then
  757. begin
  758. parse_object_proc_directives(pd);
  759. { check if dispid is set }
  760. if is_dispinterface(pd.struct) and not (po_dispid in pd.procoptions) then
  761. begin
  762. pd.dispid:=tobjectdef(pd.struct).get_next_dispid;
  763. include(pd.procoptions, po_dispid);
  764. end;
  765. { all Macintosh Object Pascal methods are virtual. }
  766. { this can't be a class method, because macpas mode }
  767. { has no m_class }
  768. if (m_mac in current_settings.modeswitches) then
  769. include(pd.procoptions,po_virtualmethod);
  770. handle_calling_convention(pd);
  771. { add definition to procsym }
  772. proc_add_definition(pd);
  773. { add procdef options to objectdef options }
  774. if (po_msgint in pd.procoptions) then
  775. include(current_structdef.objectoptions,oo_has_msgint);
  776. if (po_msgstr in pd.procoptions) then
  777. include(current_structdef.objectoptions,oo_has_msgstr);
  778. if (po_virtualmethod in pd.procoptions) then
  779. include(current_structdef.objectoptions,oo_has_virtual);
  780. chkcpp(pd);
  781. chkobjc(pd);
  782. end;
  783. maybe_parse_hint_directives(pd);
  784. parse_only:=oldparse_only;
  785. fields_allowed:=false;
  786. is_classdef:=false;
  787. end;
  788. _CONSTRUCTOR :
  789. begin
  790. if (current_structdef.symtable.currentvisibility=vis_published) and
  791. not(oo_can_have_published in current_structdef.objectoptions) then
  792. Message(parser_e_cant_have_published);
  793. if not is_classdef and not(current_structdef.symtable.currentvisibility in [vis_public,vis_published]) then
  794. Message(parser_w_constructor_should_be_public);
  795. if is_interface(current_structdef) then
  796. Message(parser_e_no_con_des_in_interfaces);
  797. { Objective-C does not know the concept of a constructor }
  798. if is_objc_class_or_protocol(current_structdef) then
  799. Message(parser_e_objc_no_constructor_destructor);
  800. { only 1 class constructor is allowed }
  801. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  802. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  803. oldparse_only:=parse_only;
  804. parse_only:=true;
  805. if is_classdef then
  806. pd:=class_constructor_head
  807. else
  808. pd:=constructor_head;
  809. parse_object_proc_directives(pd);
  810. handle_calling_convention(pd);
  811. { add definition to procsym }
  812. proc_add_definition(pd);
  813. { add procdef options to objectdef options }
  814. if (po_virtualmethod in pd.procoptions) then
  815. include(current_structdef.objectoptions,oo_has_virtual);
  816. chkcpp(pd);
  817. maybe_parse_hint_directives(pd);
  818. parse_only:=oldparse_only;
  819. fields_allowed:=false;
  820. is_classdef:=false;
  821. end;
  822. _DESTRUCTOR :
  823. begin
  824. if (current_structdef.symtable.currentvisibility=vis_published) and
  825. not(oo_can_have_published in current_structdef.objectoptions) then
  826. Message(parser_e_cant_have_published);
  827. if not is_classdef then
  828. if has_destructor then
  829. Message(parser_n_only_one_destructor)
  830. else
  831. has_destructor:=true;
  832. if is_interface(current_structdef) then
  833. Message(parser_e_no_con_des_in_interfaces);
  834. if not is_classdef and (current_structdef.symtable.currentvisibility<>vis_public) then
  835. Message(parser_w_destructor_should_be_public);
  836. { Objective-C does not know the concept of a destructor }
  837. if is_objc_class_or_protocol(current_structdef) then
  838. Message(parser_e_objc_no_constructor_destructor);
  839. { only 1 class destructor is allowed }
  840. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  841. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  842. oldparse_only:=parse_only;
  843. parse_only:=true;
  844. if is_classdef then
  845. pd:=class_destructor_head
  846. else
  847. pd:=destructor_head;
  848. parse_object_proc_directives(pd);
  849. handle_calling_convention(pd);
  850. { add definition to procsym }
  851. proc_add_definition(pd);
  852. { add procdef options to objectdef options }
  853. if (po_virtualmethod in pd.procoptions) then
  854. include(current_structdef.objectoptions,oo_has_virtual);
  855. chkcpp(pd);
  856. maybe_parse_hint_directives(pd);
  857. parse_only:=oldparse_only;
  858. fields_allowed:=false;
  859. is_classdef:=false;
  860. end;
  861. _END :
  862. begin
  863. consume(_END);
  864. break;
  865. end;
  866. else
  867. consume(_ID); { Give a ident expected message, like tp7 }
  868. end;
  869. until false;
  870. { restore }
  871. parse_generic:=old_parse_generic;
  872. end;
  873. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  874. var
  875. old_current_structdef: tabstractrecorddef;
  876. old_current_genericdef,
  877. old_current_specializedef: tstoreddef;
  878. begin
  879. old_current_structdef:=current_structdef;
  880. old_current_genericdef:=current_genericdef;
  881. old_current_specializedef:=current_specializedef;
  882. current_structdef:=nil;
  883. current_genericdef:=nil;
  884. current_specializedef:=nil;
  885. { objects and class types can't be declared local }
  886. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable]) and
  887. not assigned(genericlist) then
  888. Message(parser_e_no_local_objects);
  889. { reuse forward objectdef? }
  890. if assigned(fd) then
  891. begin
  892. if fd.objecttype<>objecttype then
  893. begin
  894. Message(parser_e_forward_mismatch);
  895. { recover }
  896. current_structdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  897. include(current_structdef.objectoptions,oo_is_forward);
  898. end
  899. else
  900. current_structdef:=fd
  901. end
  902. else
  903. begin
  904. { anonym objects aren't allow (o : object a : longint; end;) }
  905. if n='' then
  906. Message(parser_f_no_anonym_objects);
  907. { create new class }
  908. current_structdef:=tobjectdef.create(objecttype,n,nil);
  909. { include always the forward flag, it'll be removed after the parent class have been
  910. added. This is to prevent circular childof loops }
  911. include(current_structdef.objectoptions,oo_is_forward);
  912. if (cs_compilesystem in current_settings.moduleswitches) then
  913. begin
  914. case current_objectdef.objecttype of
  915. odt_interfacecom :
  916. if (current_structdef.objname^='IUNKNOWN') then
  917. interface_iunknown:=current_objectdef;
  918. odt_class :
  919. if (current_structdef.objname^='TOBJECT') then
  920. class_tobject:=current_objectdef;
  921. end;
  922. end;
  923. if (current_module.modulename^='OBJCBASE') then
  924. begin
  925. case current_objectdef.objecttype of
  926. odt_objcclass:
  927. if (current_objectdef.objname^='Protocol') then
  928. objc_protocoltype:=current_objectdef;
  929. end;
  930. end;
  931. end;
  932. { usage of specialized type inside its generic template }
  933. if assigned(genericdef) then
  934. current_specializedef:=current_structdef
  935. { reject declaration of generic class inside generic class }
  936. else if assigned(genericlist) then
  937. current_genericdef:=current_structdef;
  938. { set published flag in $M+ mode, it can also be inherited and will
  939. be added when the parent class set with tobjectdef.set_parent (PFV) }
  940. if (cs_generate_rtti in current_settings.localswitches) and
  941. (current_objectdef.objecttype in [odt_interfacecom,odt_class]) then
  942. include(current_structdef.objectoptions,oo_can_have_published);
  943. { forward def? }
  944. if not assigned(fd) and
  945. (token=_SEMICOLON) then
  946. begin
  947. { add to the list of definitions to check that the forward
  948. is resolved. this is required for delphi mode }
  949. current_module.checkforwarddefs.add(current_structdef);
  950. end
  951. else
  952. begin
  953. { change objccategories into objcclass helpers }
  954. if (objecttype=odt_objccategory) then
  955. begin
  956. current_objectdef.objecttype:=odt_objcclass;
  957. include(current_structdef.objectoptions,oo_is_classhelper);
  958. end;
  959. { parse list of options (abstract / sealed) }
  960. parse_object_options;
  961. { parse list of parent classes }
  962. parse_parent_classes;
  963. { parse optional GUID for interfaces }
  964. parse_guid;
  965. symtablestack.push(current_structdef.symtable);
  966. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  967. { parse and insert object members }
  968. parse_object_members;
  969. symtablestack.pop(current_structdef.symtable);
  970. end;
  971. { generate vmt space if needed }
  972. if not(oo_has_vmt in current_structdef.objectoptions) and
  973. not(oo_is_forward in current_structdef.objectoptions) and
  974. (
  975. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  976. (current_objectdef.objecttype in [odt_class])
  977. ) then
  978. current_objectdef.insertvmt;
  979. { for implemented classes with a vmt check if there is a constructor }
  980. if (oo_has_vmt in current_structdef.objectoptions) and
  981. not(oo_is_forward in current_structdef.objectoptions) and
  982. not(oo_has_constructor in current_structdef.objectoptions) and
  983. not is_objc_class_or_protocol(current_structdef) then
  984. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  985. if is_interface(current_structdef) or
  986. is_objcprotocol(current_structdef) then
  987. setinterfacemethodoptions
  988. else if is_objcclass(current_structdef) then
  989. setobjcclassmethodoptions;
  990. { return defined objectdef }
  991. result:=current_objectdef;
  992. { restore old state }
  993. current_structdef:=old_current_structdef;
  994. current_genericdef:=old_current_genericdef;
  995. current_specializedef:=old_current_specializedef;
  996. end;
  997. end.