pdecobj.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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. { reads the parent class }
  401. if (token=_LKLAMMER) or
  402. is_objccategory(current_structdef) then
  403. begin
  404. consume(_LKLAMMER);
  405. { use single_type instead of id_type for specialize support }
  406. single_type(hdef,[stoAllowSpecialization, stoParseClassParent]);
  407. if (not assigned(hdef)) or
  408. (hdef.typ<>objectdef) then
  409. begin
  410. if assigned(hdef) then
  411. Message1(type_e_class_type_expected,hdef.typename)
  412. else if is_objccategory(current_structdef) then
  413. { a category must specify the class to extend }
  414. Message(type_e_objcclass_type_expected);
  415. end
  416. else
  417. begin
  418. childof:=tobjectdef(hdef);
  419. { a mix of class, interfaces, objects and cppclasses
  420. isn't allowed }
  421. case current_objectdef.objecttype of
  422. odt_class:
  423. if not(is_class(childof)) then
  424. begin
  425. if is_interface(childof) then
  426. begin
  427. { we insert the interface after the child
  428. is set, see below
  429. }
  430. intfchildof:=childof;
  431. childof:=class_tobject;
  432. end
  433. else
  434. Message(parser_e_mix_of_classes_and_objects);
  435. end
  436. else
  437. if oo_is_sealed in childof.objectoptions then
  438. Message1(parser_e_sealed_descendant,childof.typename);
  439. odt_interfacecorba,
  440. odt_interfacecom:
  441. begin
  442. if not(is_interface(childof)) then
  443. Message(parser_e_mix_of_classes_and_objects);
  444. current_objectdef.objecttype:=childof.objecttype;
  445. end;
  446. odt_cppclass:
  447. if not(is_cppclass(childof)) then
  448. Message(parser_e_mix_of_classes_and_objects);
  449. odt_objcclass:
  450. if not(is_objcclass(childof) or
  451. is_objccategory(childof)) then
  452. begin
  453. if is_objcprotocol(childof) then
  454. begin
  455. if not(oo_is_classhelper in current_structdef.objectoptions) then
  456. begin
  457. intfchildof:=childof;
  458. childof:=nil;
  459. CGMessage(parser_h_no_objc_parent);
  460. end
  461. else
  462. { a category must specify the class to extend }
  463. CGMessage(type_e_objcclass_type_expected);
  464. end
  465. else
  466. Message(parser_e_mix_of_classes_and_objects);
  467. end
  468. else
  469. childof:=find_real_objcclass_definition(childof,true);
  470. odt_objcprotocol:
  471. begin
  472. if not(is_objcprotocol(childof)) then
  473. Message(parser_e_mix_of_classes_and_objects);
  474. intfchildof:=childof;
  475. childof:=nil;
  476. end;
  477. odt_object:
  478. if not(is_object(childof)) then
  479. Message(parser_e_mix_of_classes_and_objects)
  480. else
  481. if oo_is_sealed in childof.objectoptions then
  482. Message1(parser_e_sealed_descendant,childof.typename);
  483. odt_dispinterface:
  484. Message(parser_e_dispinterface_cant_have_parent);
  485. end;
  486. end;
  487. hasparentdefined:=true;
  488. end;
  489. { if no parent class, then a class get tobject as parent }
  490. if not assigned(childof) then
  491. begin
  492. case current_objectdef.objecttype of
  493. odt_class:
  494. if current_objectdef<>class_tobject then
  495. childof:=class_tobject;
  496. odt_interfacecom:
  497. if current_objectdef<>interface_iunknown then
  498. childof:=interface_iunknown;
  499. odt_objcclass:
  500. CGMessage(parser_h_no_objc_parent);
  501. end;
  502. end;
  503. if assigned(childof) then
  504. begin
  505. { Forbid not completly defined objects to be used as parents. This will
  506. also prevent circular loops of classes, because we set the forward flag
  507. at the start of the new definition and will reset it below after the
  508. parent has been set }
  509. if (oo_is_forward in childof.objectoptions) then
  510. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  511. else if not(oo_is_formal in childof.objectoptions) then
  512. current_objectdef.set_parent(childof)
  513. else
  514. Message1(sym_e_objc_formal_class_not_resolved,childof.objrealname^);
  515. end;
  516. { remove forward flag, is resolved }
  517. exclude(current_structdef.objectoptions,oo_is_forward);
  518. if hasparentdefined then
  519. begin
  520. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  521. begin
  522. if assigned(intfchildof) then
  523. if current_objectdef.objecttype=odt_class then
  524. handleImplementedInterface(intfchildof)
  525. else
  526. handleImplementedProtocol(intfchildof);
  527. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  528. end;
  529. consume(_RKLAMMER);
  530. end;
  531. end;
  532. procedure parse_guid;
  533. begin
  534. { read GUID }
  535. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  536. try_to_consume(_LECKKLAMMER) then
  537. begin
  538. readinterfaceiid;
  539. consume(_RECKKLAMMER);
  540. end
  541. else if (current_objectdef.objecttype=odt_dispinterface) then
  542. message(parser_e_dispinterface_needs_a_guid);
  543. end;
  544. procedure parse_object_members;
  545. procedure chkobjc(pd: tprocdef);
  546. begin
  547. if is_objc_class_or_protocol(pd.struct) then
  548. begin
  549. include(pd.procoptions,po_objc);
  550. end;
  551. end;
  552. procedure chkcpp(pd:tprocdef);
  553. begin
  554. { nothing currently }
  555. end;
  556. procedure maybe_parse_hint_directives(pd:tprocdef);
  557. var
  558. dummysymoptions : tsymoptions;
  559. deprecatedmsg : pshortstring;
  560. begin
  561. dummysymoptions:=[];
  562. deprecatedmsg:=nil;
  563. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  564. Consume(_SEMICOLON);
  565. if assigned(pd) then
  566. begin
  567. pd.symoptions:=pd.symoptions+dummysymoptions;
  568. pd.deprecatedmsg:=deprecatedmsg;
  569. end
  570. else
  571. stringdispose(deprecatedmsg);
  572. end;
  573. var
  574. pd : tprocdef;
  575. has_destructor,
  576. oldparse_only: boolean;
  577. object_member_blocktype : tblock_type;
  578. fields_allowed, is_classdef, classfields: boolean;
  579. vdoptions: tvar_dec_options;
  580. begin
  581. { empty class declaration ? }
  582. if (current_objectdef.objecttype in [odt_class,odt_objcclass]) and
  583. (token=_SEMICOLON) then
  584. exit;
  585. { in "publishable" classes the default access type is published }
  586. if (oo_can_have_published in current_structdef.objectoptions) then
  587. current_structdef.symtable.currentvisibility:=vis_published
  588. else
  589. current_structdef.symtable.currentvisibility:=vis_public;
  590. has_destructor:=false;
  591. fields_allowed:=true;
  592. is_classdef:=false;
  593. classfields:=false;
  594. object_member_blocktype:=bt_general;
  595. repeat
  596. case token of
  597. _TYPE :
  598. begin
  599. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  600. Message(parser_e_type_var_const_only_in_records_and_classes);
  601. consume(_TYPE);
  602. object_member_blocktype:=bt_type;
  603. end;
  604. _VAR :
  605. begin
  606. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  607. Message(parser_e_type_var_const_only_in_records_and_classes);
  608. consume(_VAR);
  609. fields_allowed:=true;
  610. object_member_blocktype:=bt_general;
  611. classfields:=is_classdef;
  612. is_classdef:=false;
  613. end;
  614. _CONST:
  615. begin
  616. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  617. Message(parser_e_type_var_const_only_in_records_and_classes);
  618. consume(_CONST);
  619. object_member_blocktype:=bt_const;
  620. end;
  621. _ID :
  622. begin
  623. if is_objcprotocol(current_structdef) and
  624. ((idtoken=_REQUIRED) or
  625. (idtoken=_OPTIONAL)) then
  626. begin
  627. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  628. consume(idtoken)
  629. end
  630. else case idtoken of
  631. _PRIVATE :
  632. begin
  633. if is_interface(current_structdef) or
  634. is_objc_protocol_or_category(current_structdef) then
  635. Message(parser_e_no_access_specifier_in_interfaces);
  636. consume(_PRIVATE);
  637. current_structdef.symtable.currentvisibility:=vis_private;
  638. include(current_structdef.objectoptions,oo_has_private);
  639. fields_allowed:=true;
  640. is_classdef:=false;
  641. classfields:=false;
  642. object_member_blocktype:=bt_general;
  643. end;
  644. _PROTECTED :
  645. begin
  646. if is_interface(current_structdef) or
  647. is_objc_protocol_or_category(current_structdef) then
  648. Message(parser_e_no_access_specifier_in_interfaces);
  649. consume(_PROTECTED);
  650. current_structdef.symtable.currentvisibility:=vis_protected;
  651. include(current_structdef.objectoptions,oo_has_protected);
  652. fields_allowed:=true;
  653. is_classdef:=false;
  654. classfields:=false;
  655. object_member_blocktype:=bt_general;
  656. end;
  657. _PUBLIC :
  658. begin
  659. if is_interface(current_structdef) or
  660. is_objc_protocol_or_category(current_structdef) then
  661. Message(parser_e_no_access_specifier_in_interfaces);
  662. consume(_PUBLIC);
  663. current_structdef.symtable.currentvisibility:=vis_public;
  664. fields_allowed:=true;
  665. is_classdef:=false;
  666. classfields:=false;
  667. object_member_blocktype:=bt_general;
  668. end;
  669. _PUBLISHED :
  670. begin
  671. { we've to check for a pushlished section in non- }
  672. { publishable classes later, if a real declaration }
  673. { this is the way, delphi does it }
  674. if is_interface(current_structdef) then
  675. Message(parser_e_no_access_specifier_in_interfaces);
  676. { Objective-C classes do not support "published",
  677. as basically everything is published. }
  678. if is_objc_class_or_protocol(current_structdef) then
  679. Message(parser_e_no_objc_published);
  680. consume(_PUBLISHED);
  681. current_structdef.symtable.currentvisibility:=vis_published;
  682. fields_allowed:=true;
  683. is_classdef:=false;
  684. classfields:=false;
  685. object_member_blocktype:=bt_general;
  686. end;
  687. _STRICT :
  688. begin
  689. if is_interface(current_structdef) or
  690. is_objc_protocol_or_category(current_structdef) then
  691. Message(parser_e_no_access_specifier_in_interfaces);
  692. consume(_STRICT);
  693. if token=_ID then
  694. begin
  695. case idtoken of
  696. _PRIVATE:
  697. begin
  698. consume(_PRIVATE);
  699. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  700. include(current_structdef.objectoptions,oo_has_strictprivate);
  701. end;
  702. _PROTECTED:
  703. begin
  704. consume(_PROTECTED);
  705. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  706. include(current_structdef.objectoptions,oo_has_strictprotected);
  707. end;
  708. else
  709. message(parser_e_protected_or_private_expected);
  710. end;
  711. end
  712. else
  713. message(parser_e_protected_or_private_expected);
  714. fields_allowed:=true;
  715. is_classdef:=false;
  716. classfields:=false;
  717. object_member_blocktype:=bt_general;
  718. end
  719. else
  720. begin
  721. if object_member_blocktype=bt_general then
  722. begin
  723. if is_interface(current_structdef) or
  724. is_objc_protocol_or_category(current_structdef) then
  725. Message(parser_e_no_vars_in_interfaces);
  726. if (current_structdef.symtable.currentvisibility=vis_published) and
  727. not(oo_can_have_published in current_structdef.objectoptions) then
  728. Message(parser_e_cant_have_published);
  729. if (not fields_allowed) then
  730. Message(parser_e_field_not_allowed_here);
  731. vdoptions:=[vd_object];
  732. if classfields then
  733. include(vdoptions,vd_class);
  734. read_record_fields(vdoptions);
  735. end
  736. else if object_member_blocktype=bt_type then
  737. types_dec(true)
  738. else if object_member_blocktype=bt_const then
  739. consts_dec(true)
  740. else
  741. internalerror(201001110);
  742. end;
  743. end;
  744. end;
  745. _PROPERTY :
  746. begin
  747. struct_property_dec(is_classdef);
  748. fields_allowed:=false;
  749. is_classdef:=false;
  750. end;
  751. _CLASS:
  752. begin
  753. is_classdef:=false;
  754. { read class method/field/property }
  755. consume(_CLASS);
  756. { class modifier is only allowed for procedures, functions, }
  757. { constructors, destructors, fields and properties }
  758. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  759. Message(parser_e_procedure_or_function_expected);
  760. if is_interface(current_structdef) then
  761. Message(parser_e_no_static_method_in_interfaces)
  762. else
  763. { class methods are also allowed for Objective-C protocols }
  764. is_classdef:=true;
  765. end;
  766. _PROCEDURE,
  767. _FUNCTION:
  768. begin
  769. if (current_structdef.symtable.currentvisibility=vis_published) and
  770. not(oo_can_have_published in current_structdef.objectoptions) then
  771. Message(parser_e_cant_have_published);
  772. oldparse_only:=parse_only;
  773. parse_only:=true;
  774. pd:=parse_proc_dec(is_classdef,current_structdef);
  775. { this is for error recovery as well as forward }
  776. { interface mappings, i.e. mapping to a method }
  777. { which isn't declared yet }
  778. if assigned(pd) then
  779. begin
  780. parse_object_proc_directives(pd);
  781. { check if dispid is set }
  782. if is_dispinterface(pd.struct) and not (po_dispid in pd.procoptions) then
  783. begin
  784. pd.dispid:=tobjectdef(pd.struct).get_next_dispid;
  785. include(pd.procoptions, po_dispid);
  786. end;
  787. { all Macintosh Object Pascal methods are virtual. }
  788. { this can't be a class method, because macpas mode }
  789. { has no m_class }
  790. if (m_mac in current_settings.modeswitches) then
  791. include(pd.procoptions,po_virtualmethod);
  792. handle_calling_convention(pd);
  793. { add definition to procsym }
  794. proc_add_definition(pd);
  795. { add procdef options to objectdef options }
  796. if (po_msgint in pd.procoptions) then
  797. include(current_structdef.objectoptions,oo_has_msgint);
  798. if (po_msgstr in pd.procoptions) then
  799. include(current_structdef.objectoptions,oo_has_msgstr);
  800. if (po_virtualmethod in pd.procoptions) then
  801. include(current_structdef.objectoptions,oo_has_virtual);
  802. chkcpp(pd);
  803. chkobjc(pd);
  804. end;
  805. maybe_parse_hint_directives(pd);
  806. parse_only:=oldparse_only;
  807. fields_allowed:=false;
  808. is_classdef:=false;
  809. end;
  810. _CONSTRUCTOR :
  811. begin
  812. if (current_structdef.symtable.currentvisibility=vis_published) and
  813. not(oo_can_have_published in current_structdef.objectoptions) then
  814. Message(parser_e_cant_have_published);
  815. if not is_classdef and not(current_structdef.symtable.currentvisibility in [vis_public,vis_published]) then
  816. Message(parser_w_constructor_should_be_public);
  817. if is_interface(current_structdef) then
  818. Message(parser_e_no_con_des_in_interfaces);
  819. { Objective-C does not know the concept of a constructor }
  820. if is_objc_class_or_protocol(current_structdef) then
  821. Message(parser_e_objc_no_constructor_destructor);
  822. { only 1 class constructor is allowed }
  823. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  824. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  825. oldparse_only:=parse_only;
  826. parse_only:=true;
  827. if is_classdef then
  828. pd:=class_constructor_head
  829. else
  830. pd:=constructor_head;
  831. parse_object_proc_directives(pd);
  832. handle_calling_convention(pd);
  833. { add definition to procsym }
  834. proc_add_definition(pd);
  835. { add procdef options to objectdef options }
  836. if (po_virtualmethod in pd.procoptions) then
  837. include(current_structdef.objectoptions,oo_has_virtual);
  838. chkcpp(pd);
  839. maybe_parse_hint_directives(pd);
  840. parse_only:=oldparse_only;
  841. fields_allowed:=false;
  842. is_classdef:=false;
  843. end;
  844. _DESTRUCTOR :
  845. begin
  846. if (current_structdef.symtable.currentvisibility=vis_published) and
  847. not(oo_can_have_published in current_structdef.objectoptions) then
  848. Message(parser_e_cant_have_published);
  849. if not is_classdef then
  850. if has_destructor then
  851. Message(parser_n_only_one_destructor)
  852. else
  853. has_destructor:=true;
  854. if is_interface(current_structdef) then
  855. Message(parser_e_no_con_des_in_interfaces);
  856. if not is_classdef and (current_structdef.symtable.currentvisibility<>vis_public) then
  857. Message(parser_w_destructor_should_be_public);
  858. { Objective-C does not know the concept of a destructor }
  859. if is_objc_class_or_protocol(current_structdef) then
  860. Message(parser_e_objc_no_constructor_destructor);
  861. { only 1 class destructor is allowed }
  862. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  863. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  864. oldparse_only:=parse_only;
  865. parse_only:=true;
  866. if is_classdef then
  867. pd:=class_destructor_head
  868. else
  869. pd:=destructor_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. _END :
  884. begin
  885. consume(_END);
  886. break;
  887. end;
  888. else
  889. consume(_ID); { Give a ident expected message, like tp7 }
  890. end;
  891. until false;
  892. end;
  893. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  894. var
  895. old_current_structdef: tabstractrecorddef;
  896. old_current_genericdef,
  897. old_current_specializedef: tstoreddef;
  898. old_parse_generic: boolean;
  899. begin
  900. old_current_structdef:=current_structdef;
  901. old_current_genericdef:=current_genericdef;
  902. old_current_specializedef:=current_specializedef;
  903. old_parse_generic:=parse_generic;
  904. current_structdef:=nil;
  905. current_genericdef:=nil;
  906. current_specializedef:=nil;
  907. { objects and class types can't be declared local }
  908. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable]) and
  909. not assigned(genericlist) then
  910. Message(parser_e_no_local_objects);
  911. { reuse forward objectdef? }
  912. if assigned(fd) then
  913. begin
  914. if fd.objecttype<>objecttype then
  915. begin
  916. Message(parser_e_forward_mismatch);
  917. { recover }
  918. current_structdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  919. include(current_structdef.objectoptions,oo_is_forward);
  920. end
  921. else
  922. current_structdef:=fd
  923. end
  924. else
  925. begin
  926. { anonym objects aren't allow (o : object a : longint; end;) }
  927. if n='' then
  928. Message(parser_f_no_anonym_objects);
  929. { create new class }
  930. current_structdef:=tobjectdef.create(objecttype,n,nil);
  931. { include always the forward flag, it'll be removed after the parent class have been
  932. added. This is to prevent circular childof loops }
  933. include(current_structdef.objectoptions,oo_is_forward);
  934. if (cs_compilesystem in current_settings.moduleswitches) then
  935. begin
  936. case current_objectdef.objecttype of
  937. odt_interfacecom :
  938. if (current_structdef.objname^='IUNKNOWN') then
  939. interface_iunknown:=current_objectdef;
  940. odt_class :
  941. if (current_structdef.objname^='TOBJECT') then
  942. class_tobject:=current_objectdef;
  943. end;
  944. end;
  945. if (current_module.modulename^='OBJCBASE') then
  946. begin
  947. case current_objectdef.objecttype of
  948. odt_objcclass:
  949. if (current_objectdef.objname^='Protocol') then
  950. objc_protocoltype:=current_objectdef;
  951. end;
  952. end;
  953. end;
  954. { usage of specialized type inside its generic template }
  955. if assigned(genericdef) then
  956. current_specializedef:=current_structdef
  957. { reject declaration of generic class inside generic class }
  958. else if assigned(genericlist) then
  959. current_genericdef:=current_structdef;
  960. { set published flag in $M+ mode, it can also be inherited and will
  961. be added when the parent class set with tobjectdef.set_parent (PFV) }
  962. if (cs_generate_rtti in current_settings.localswitches) and
  963. (current_objectdef.objecttype in [odt_interfacecom,odt_class]) then
  964. include(current_structdef.objectoptions,oo_can_have_published);
  965. { Objective-C objectdefs can be "formal definitions", in which case
  966. the syntax is "type tc = objcclass external;" -> we have to parse
  967. its object options (external) already here, to make sure that such
  968. definitions are recognised as formal defs }
  969. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory] then
  970. parse_object_options;
  971. { forward def? }
  972. if not assigned(fd) and
  973. (token=_SEMICOLON) then
  974. begin
  975. { add to the list of definitions to check that the forward
  976. is resolved. this is required for delphi mode }
  977. current_module.checkforwarddefs.add(current_structdef);
  978. end
  979. else
  980. begin
  981. { change objccategories into objcclass helpers }
  982. if (objecttype=odt_objccategory) then
  983. begin
  984. current_objectdef.objecttype:=odt_objcclass;
  985. include(current_structdef.objectoptions,oo_is_classhelper);
  986. end;
  987. { parse list of options (abstract / sealed) }
  988. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory]) then
  989. parse_object_options;
  990. symtablestack.push(current_structdef.symtable);
  991. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  992. parse_generic:=(df_generic in current_structdef.defoptions);
  993. { parse list of parent classes }
  994. parse_parent_classes;
  995. { parse optional GUID for interfaces }
  996. parse_guid;
  997. { parse and insert object members }
  998. parse_object_members;
  999. symtablestack.pop(current_structdef.symtable);
  1000. end;
  1001. { generate vmt space if needed }
  1002. if not(oo_has_vmt in current_structdef.objectoptions) and
  1003. not(oo_is_forward in current_structdef.objectoptions) and
  1004. (
  1005. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1006. (current_objectdef.objecttype in [odt_class])
  1007. ) then
  1008. current_objectdef.insertvmt;
  1009. { for implemented classes with a vmt check if there is a constructor }
  1010. if (oo_has_vmt in current_structdef.objectoptions) and
  1011. not(oo_is_forward in current_structdef.objectoptions) and
  1012. not(oo_has_constructor in current_structdef.objectoptions) and
  1013. not is_objc_class_or_protocol(current_structdef) then
  1014. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1015. if is_interface(current_structdef) or
  1016. is_objcprotocol(current_structdef) then
  1017. setinterfacemethodoptions
  1018. else if is_objcclass(current_structdef) then
  1019. setobjcclassmethodoptions;
  1020. { return defined objectdef }
  1021. result:=current_objectdef;
  1022. { restore old state }
  1023. current_structdef:=old_current_structdef;
  1024. current_genericdef:=old_current_genericdef;
  1025. current_specializedef:=old_current_specializedef;
  1026. parse_generic:=old_parse_generic;
  1027. end;
  1028. end.