pdecobj.pas 47 KB

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