pdecobj.pas 45 KB

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