pdecobj.pas 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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;helpertype:thelpertype) : tobjectdef;
  25. { parses a (class) method declaration }
  26. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  27. function class_constructor_head:tprocdef;
  28. function class_destructor_head:tprocdef;
  29. function constructor_head:tprocdef;
  30. function destructor_head:tprocdef;
  31. procedure struct_property_dec(is_classproperty:boolean);
  32. implementation
  33. uses
  34. sysutils,cutils,
  35. globals,verbose,systems,tokens,
  36. symbase,symsym,symtable,symcreat,defcmp,
  37. node,nld,nmem,ncon,ncnv,ncal,
  38. fmodule,scanner,
  39. pbase,pexpr,pdecsub,pdecvar,ptype,pdecl,ppu,
  40. pjvm,
  41. parabase
  42. ;
  43. const
  44. { Please leave this here, this module should NOT use
  45. these variables.
  46. Declaring it as string here results in an error when compiling (PFV) }
  47. current_procinfo = 'error';
  48. var
  49. current_objectdef : tobjectdef absolute current_structdef;
  50. function class_constructor_head:tprocdef;
  51. var
  52. pd : tprocdef;
  53. begin
  54. result:=nil;
  55. consume(_CONSTRUCTOR);
  56. { must be at same level as in implementation }
  57. parse_proc_head(current_structdef,potype_class_constructor,pd);
  58. if not assigned(pd) then
  59. begin
  60. consume(_SEMICOLON);
  61. exit;
  62. end;
  63. pd.calcparas;
  64. if (pd.maxparacount>0) then
  65. Message(parser_e_no_paras_for_class_constructor);
  66. consume(_SEMICOLON);
  67. include(current_structdef.objectoptions,oo_has_class_constructor);
  68. current_module.flags:=current_module.flags or uf_classinits;
  69. { no return value }
  70. pd.returndef:=voidtype;
  71. result:=pd;
  72. end;
  73. function constructor_head:tprocdef;
  74. var
  75. pd : tprocdef;
  76. begin
  77. result:=nil;
  78. consume(_CONSTRUCTOR);
  79. { must be at same level as in implementation }
  80. parse_proc_head(current_structdef,potype_constructor,pd);
  81. if not assigned(pd) then
  82. begin
  83. consume(_SEMICOLON);
  84. exit;
  85. end;
  86. if (cs_constructor_name in current_settings.globalswitches) and
  87. (pd.procsym.name<>'INIT') then
  88. Message(parser_e_constructorname_must_be_init);
  89. consume(_SEMICOLON);
  90. include(current_structdef.objectoptions,oo_has_constructor);
  91. { Set return type, class and record constructors return the
  92. created instance, object constructors return boolean }
  93. if is_class(pd.struct) or
  94. is_record(pd.struct) or
  95. is_javaclass(pd.struct) then
  96. pd.returndef:=pd.struct
  97. else
  98. {$ifdef CPU64bitaddr}
  99. pd.returndef:=bool64type;
  100. {$else CPU64bitaddr}
  101. pd.returndef:=bool32type;
  102. {$endif CPU64bitaddr}
  103. result:=pd;
  104. end;
  105. procedure struct_property_dec(is_classproperty:boolean);
  106. var
  107. p : tpropertysym;
  108. begin
  109. { check for a class, record or helper }
  110. if not((is_class_or_interface_or_dispinterface(current_structdef) or is_record(current_structdef) or is_objectpascal_helper(current_structdef)) or
  111. (not(m_tp7 in current_settings.modeswitches) and (is_object(current_structdef)))) then
  112. Message(parser_e_syntax_error);
  113. consume(_PROPERTY);
  114. p:=read_property_dec(is_classproperty,current_structdef);
  115. consume(_SEMICOLON);
  116. if try_to_consume(_DEFAULT) then
  117. begin
  118. if oo_has_default_property in current_structdef.objectoptions then
  119. message(parser_e_only_one_default_property);
  120. include(current_structdef.objectoptions,oo_has_default_property);
  121. include(p.propoptions,ppo_defaultproperty);
  122. if not(ppo_hasparameters in p.propoptions) then
  123. message(parser_e_property_need_paras);
  124. if (token=_COLON) then
  125. begin
  126. Message(parser_e_field_not_allowed_here);
  127. consume_all_until(_SEMICOLON);
  128. end;
  129. consume(_SEMICOLON);
  130. end;
  131. { parse possible enumerator modifier }
  132. if try_to_consume(_ENUMERATOR) then
  133. begin
  134. if (token = _ID) then
  135. begin
  136. if pattern='CURRENT' then
  137. begin
  138. if oo_has_enumerator_current in current_structdef.objectoptions then
  139. message(parser_e_only_one_enumerator_current);
  140. if not p.propaccesslist[palt_read].empty then
  141. begin
  142. include(current_structdef.objectoptions,oo_has_enumerator_current);
  143. include(p.propoptions,ppo_enumerator_current);
  144. end
  145. else
  146. Message(parser_e_enumerator_current_is_not_valid) // property has no reader
  147. end
  148. else
  149. Message1(parser_e_invalid_enumerator_identifier, pattern);
  150. consume(token);
  151. end
  152. else
  153. Message(parser_e_enumerator_identifier_required);
  154. consume(_SEMICOLON);
  155. end;
  156. { hint directives, these can be separated by semicolons here,
  157. that needs to be handled here with a loop (PFV) }
  158. while try_consume_hintdirective(p.symoptions,p.deprecatedmsg) do
  159. Consume(_SEMICOLON);
  160. end;
  161. function class_destructor_head:tprocdef;
  162. var
  163. pd : tprocdef;
  164. begin
  165. result:=nil;
  166. consume(_DESTRUCTOR);
  167. parse_proc_head(current_structdef,potype_class_destructor,pd);
  168. if not assigned(pd) then
  169. begin
  170. consume(_SEMICOLON);
  171. exit;
  172. end;
  173. pd.calcparas;
  174. if (pd.maxparacount>0) then
  175. Message(parser_e_no_paras_for_class_destructor);
  176. consume(_SEMICOLON);
  177. include(current_structdef.objectoptions,oo_has_class_destructor);
  178. current_module.flags:=current_module.flags or uf_classinits;
  179. { no return value }
  180. pd.returndef:=voidtype;
  181. result:=pd;
  182. end;
  183. function destructor_head:tprocdef;
  184. var
  185. pd : tprocdef;
  186. begin
  187. result:=nil;
  188. consume(_DESTRUCTOR);
  189. parse_proc_head(current_structdef,potype_destructor,pd);
  190. if not assigned(pd) then
  191. begin
  192. consume(_SEMICOLON);
  193. exit;
  194. end;
  195. if (cs_constructor_name in current_settings.globalswitches) and
  196. (pd.procsym.name<>'DONE') then
  197. Message(parser_e_destructorname_must_be_done);
  198. pd.calcparas;
  199. if not(pd.maxparacount=0) and
  200. (m_fpc in current_settings.modeswitches) then
  201. Message(parser_e_no_paras_for_destructor);
  202. consume(_SEMICOLON);
  203. include(current_structdef.objectoptions,oo_has_destructor);
  204. { no return value }
  205. pd.returndef:=voidtype;
  206. result:=pd;
  207. end;
  208. procedure setinterfacemethodoptions;
  209. var
  210. i : longint;
  211. def : tdef;
  212. begin
  213. include(current_structdef.objectoptions,oo_has_virtual);
  214. for i:=0 to current_structdef.symtable.DefList.count-1 do
  215. begin
  216. def:=tdef(current_structdef.symtable.DefList[i]);
  217. if assigned(def) and
  218. (def.typ=procdef) then
  219. begin
  220. include(tprocdef(def).procoptions,po_virtualmethod);
  221. tprocdef(def).forwarddef:=false;
  222. end;
  223. end;
  224. end;
  225. procedure setobjcclassmethodoptions;
  226. var
  227. i : longint;
  228. def : tdef;
  229. begin
  230. for i:=0 to current_structdef.symtable.DefList.count-1 do
  231. begin
  232. def:=tdef(current_structdef.symtable.DefList[i]);
  233. if assigned(def) and
  234. (def.typ=procdef) then
  235. begin
  236. include(tprocdef(def).procoptions,po_virtualmethod);
  237. end;
  238. end;
  239. end;
  240. procedure handleImplementedInterface(intfdef : tobjectdef);
  241. begin
  242. if not is_interface(intfdef) then
  243. begin
  244. Message1(type_e_interface_type_expected,intfdef.typename);
  245. exit;
  246. end;
  247. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  248. Message1(sym_e_duplicate_id,intfdef.objname^)
  249. else
  250. begin
  251. { allocate and prepare the GUID only if the class
  252. implements some interfaces. }
  253. if current_objectdef.ImplementedInterfaces.count = 0 then
  254. current_objectdef.prepareguid;
  255. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  256. end;
  257. end;
  258. procedure handleImplementedProtocolOrJavaIntf(intfdef : tobjectdef);
  259. begin
  260. intfdef:=find_real_class_definition(intfdef,false);
  261. case current_objectdef.objecttype of
  262. odt_objcclass,
  263. odt_objccategory,
  264. odt_objcprotocol:
  265. if not is_objcprotocol(intfdef) then
  266. begin
  267. Message1(type_e_protocol_type_expected,intfdef.typename);
  268. exit;
  269. end;
  270. odt_javaclass,
  271. odt_interfacejava:
  272. if not is_javainterface(intfdef) then
  273. begin
  274. Message1(type_e_interface_type_expected,intfdef.typename);
  275. exit
  276. end;
  277. else
  278. internalerror(2011010807);
  279. end;
  280. if ([oo_is_forward,oo_is_formal] * intfdef.objectoptions <> []) then
  281. begin
  282. Message1(parser_e_forward_intf_declaration_must_be_resolved,intfdef.objrealname^);
  283. exit;
  284. end;
  285. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  286. Message1(sym_e_duplicate_id,intfdef.objname^)
  287. else
  288. begin
  289. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  290. end;
  291. end;
  292. procedure readImplementedInterfacesAndProtocols(intf: boolean);
  293. var
  294. hdef : tdef;
  295. begin
  296. while try_to_consume(_COMMA) do
  297. begin
  298. { use single_type instead of id_type for specialize support }
  299. single_type(hdef,[stoAllowSpecialization,stoParseClassParent]);
  300. if (hdef.typ<>objectdef) then
  301. begin
  302. if intf then
  303. Message1(type_e_interface_type_expected,hdef.typename)
  304. else
  305. Message1(type_e_protocol_type_expected,hdef.typename);
  306. continue;
  307. end;
  308. if intf then
  309. handleImplementedInterface(tobjectdef(hdef))
  310. else
  311. handleImplementedProtocolOrJavaIntf(tobjectdef(hdef));
  312. end;
  313. end;
  314. procedure readinterfaceiid;
  315. var
  316. p : tnode;
  317. valid : boolean;
  318. begin
  319. p:=comp_expr(true,false);
  320. if p.nodetype=stringconstn then
  321. begin
  322. stringdispose(current_objectdef.iidstr);
  323. current_objectdef.iidstr:=stringdup(strpas(tstringconstnode(p).value_str));
  324. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  325. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  326. not valid then
  327. Message(parser_e_improper_guid_syntax);
  328. include(current_structdef.objectoptions,oo_has_valid_guid);
  329. end
  330. else
  331. Message(parser_e_illegal_expression);
  332. p.free;
  333. end;
  334. procedure get_cpp_or_java_class_external_status(od: tobjectdef);
  335. var
  336. hs: string;
  337. begin
  338. { C++ classes can be external -> all methods inside are external
  339. (defined at the class level instead of per method, so that you cannot
  340. define some methods as external and some not)
  341. }
  342. if try_to_consume(_EXTERNAL) then
  343. begin
  344. hs:='';
  345. if token in [_CSTRING,_CWSTRING,_CCHAR,_CWCHAR] then
  346. begin
  347. { Always add library prefix and suffix to create an uniform name }
  348. hs:=get_stringconst;
  349. if ExtractFileExt(hs)='' then
  350. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  351. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  352. hs:=target_info.sharedlibprefix+hs;
  353. end
  354. else if assigned(current_module.namespace) then
  355. begin
  356. { import_lib is used to specify the package name for the JVM
  357. target (= namespace) }
  358. if (target_info.system=system_jvm_java32) and
  359. assigned(current_module.namespace) then
  360. hs:=current_module.namespace^;
  361. { not sure how to deal with cppclass here, since namespaces
  362. mean something different there }
  363. end;
  364. if hs<>'' then
  365. begin
  366. { the JVM expects java/lang/Object rather than java.lang.Object }
  367. if target_info.system=system_jvm_java32 then
  368. Replace(hs,'.','/');
  369. od.import_lib:=stringdup(hs);
  370. end;
  371. include(od.objectoptions, oo_is_external);
  372. { check if we shall use another name for the class }
  373. if try_to_consume(_NAME) then
  374. od.objextname:=stringdup(get_stringconst)
  375. else
  376. od.objextname:=stringdup(od.objrealname^);
  377. include(od.objectoptions,oo_is_external);
  378. end
  379. else
  380. begin
  381. od.objextname:=stringdup(od.objrealname^);
  382. { ToDo for cpp: read/set the namespace of the class (influences the mangled name)
  383. (notice that for the JVM target, there is no difference between
  384. the namespace and import_lib) }
  385. if (target_info.system=system_jvm_java32) and
  386. assigned(current_module.namespace) then
  387. begin
  388. od.import_lib:=stringdup(current_module.namespace^);
  389. Replace(od.import_lib^,'.','/');
  390. end;
  391. end;
  392. end;
  393. procedure get_objc_class_or_protocol_external_status(od: tobjectdef);
  394. begin
  395. { Objective-C classes can be external -> all messages inside are
  396. external (defined at the class level instead of per method, so
  397. that you cannot define some methods as external and some not)
  398. }
  399. if try_to_consume(_EXTERNAL) then
  400. begin
  401. if try_to_consume(_NAME) then
  402. od.objextname:=stringdup(get_stringconst)
  403. else
  404. { the external name doesn't matter for formally declared
  405. classes, and allowing to specify one would mean that we would
  406. have to check it for consistency with the actual definition
  407. later on }
  408. od.objextname:=stringdup(od.objrealname^);
  409. include(od.objectoptions,oo_is_external);
  410. end
  411. else
  412. od.objextname:=stringdup(od.objrealname^);
  413. end;
  414. procedure parse_object_options;
  415. var
  416. gotexternal: boolean;
  417. begin
  418. case current_objectdef.objecttype of
  419. odt_object,odt_class,
  420. odt_javaclass:
  421. begin
  422. gotexternal:=false;
  423. while true do
  424. begin
  425. if try_to_consume(_ABSTRACT) then
  426. include(current_structdef.objectoptions,oo_is_abstract)
  427. else
  428. if try_to_consume(_SEALED) then
  429. include(current_structdef.objectoptions,oo_is_sealed)
  430. else if (current_objectdef.objecttype=odt_javaclass) and
  431. (token=_ID) and
  432. (idtoken=_EXTERNAL) then
  433. begin
  434. get_cpp_or_java_class_external_status(current_objectdef);
  435. gotexternal:=true;
  436. end
  437. else
  438. break;
  439. end;
  440. if [oo_is_abstract, oo_is_sealed] <= current_structdef.objectoptions then
  441. Message(parser_e_abstract_and_sealed_conflict);
  442. { set default external name in case of no external directive }
  443. if (current_objectdef.objecttype=odt_javaclass) and
  444. not gotexternal then
  445. get_cpp_or_java_class_external_status(current_objectdef)
  446. end;
  447. odt_cppclass,
  448. odt_interfacejava:
  449. get_cpp_or_java_class_external_status(current_objectdef);
  450. odt_objcclass,odt_objcprotocol,odt_objccategory:
  451. get_objc_class_or_protocol_external_status(current_objectdef);
  452. odt_helper: ; // nothing
  453. end;
  454. end;
  455. procedure parse_parent_classes;
  456. var
  457. intfchildof,
  458. childof : tobjectdef;
  459. hdef : tdef;
  460. hasparentdefined : boolean;
  461. begin
  462. childof:=nil;
  463. intfchildof:=nil;
  464. hasparentdefined:=false;
  465. { reads the parent class }
  466. if (token=_LKLAMMER) or
  467. is_objccategory(current_structdef) then
  468. begin
  469. consume(_LKLAMMER);
  470. { use single_type instead of id_type for specialize support }
  471. single_type(hdef,[stoAllowSpecialization, stoParseClassParent]);
  472. if (not assigned(hdef)) or
  473. (hdef.typ<>objectdef) then
  474. begin
  475. if assigned(hdef) then
  476. Message1(type_e_class_type_expected,hdef.typename)
  477. else if is_objccategory(current_structdef) then
  478. { a category must specify the class to extend }
  479. Message(type_e_objcclass_type_expected);
  480. end
  481. else
  482. begin
  483. childof:=tobjectdef(hdef);
  484. { a mix of class, interfaces, objects and cppclasses
  485. isn't allowed }
  486. case current_objectdef.objecttype of
  487. odt_class,
  488. odt_javaclass:
  489. if (childof.objecttype<>current_objectdef.objecttype) then
  490. begin
  491. if (is_interface(childof) and
  492. is_class(current_objectdef)) or
  493. (is_javainterface(childof) and
  494. is_javaclass(current_objectdef)) then
  495. begin
  496. { we insert the interface after the child
  497. is set, see below
  498. }
  499. intfchildof:=childof;
  500. childof:=class_tobject;
  501. end
  502. else
  503. Message(parser_e_mix_of_classes_and_objects);
  504. end
  505. else
  506. if oo_is_sealed in childof.objectoptions then
  507. Message1(parser_e_sealed_descendant,childof.typename)
  508. else
  509. childof:=find_real_class_definition(childof,true);
  510. odt_interfacecorba,
  511. odt_interfacecom:
  512. begin
  513. if not(is_interface(childof)) then
  514. Message(parser_e_mix_of_classes_and_objects);
  515. current_objectdef.objecttype:=childof.objecttype;
  516. end;
  517. odt_cppclass:
  518. if not(is_cppclass(childof)) then
  519. Message(parser_e_mix_of_classes_and_objects);
  520. odt_objcclass:
  521. if not(is_objcclass(childof) or
  522. is_objccategory(childof)) then
  523. begin
  524. if is_objcprotocol(childof) then
  525. begin
  526. if not(oo_is_classhelper in current_structdef.objectoptions) then
  527. begin
  528. intfchildof:=childof;
  529. childof:=nil;
  530. CGMessage(parser_h_no_objc_parent);
  531. end
  532. else
  533. { a category must specify the class to extend }
  534. CGMessage(type_e_objcclass_type_expected);
  535. end
  536. else
  537. Message(parser_e_mix_of_classes_and_objects);
  538. end
  539. else
  540. childof:=find_real_class_definition(childof,true);
  541. odt_objcprotocol:
  542. begin
  543. if not(is_objcprotocol(childof)) then
  544. Message(parser_e_mix_of_classes_and_objects);
  545. intfchildof:=childof;
  546. childof:=nil;
  547. end;
  548. odt_interfacejava:
  549. begin
  550. if not(is_javainterface(childof)) then
  551. Message(parser_e_mix_of_classes_and_objects);
  552. intfchildof:=find_real_class_definition(childof,true);
  553. childof:=nil;
  554. end;
  555. odt_object:
  556. if not(is_object(childof)) then
  557. Message(parser_e_mix_of_classes_and_objects)
  558. else
  559. if oo_is_sealed in childof.objectoptions then
  560. Message1(parser_e_sealed_descendant,childof.typename);
  561. odt_dispinterface:
  562. Message(parser_e_dispinterface_cant_have_parent);
  563. odt_helper:
  564. if not is_objectpascal_helper(childof) then
  565. begin
  566. Message(type_e_helper_type_expected);
  567. childof:=nil;
  568. end;
  569. end;
  570. end;
  571. hasparentdefined:=true;
  572. end;
  573. { if no parent class, then a class get tobject as parent }
  574. if not assigned(childof) then
  575. begin
  576. case current_objectdef.objecttype of
  577. odt_class:
  578. if current_objectdef<>class_tobject then
  579. childof:=class_tobject;
  580. odt_interfacecom:
  581. if current_objectdef<>interface_iunknown then
  582. childof:=interface_iunknown;
  583. odt_dispinterface:
  584. childof:=interface_idispatch;
  585. odt_objcclass:
  586. CGMessage(parser_h_no_objc_parent);
  587. odt_javaclass:
  588. if current_objectdef<>java_jlobject then
  589. childof:=java_jlobject;
  590. end;
  591. end;
  592. if assigned(childof) then
  593. begin
  594. { Forbid not completly defined objects to be used as parents. This will
  595. also prevent circular loops of classes, because we set the forward flag
  596. at the start of the new definition and will reset it below after the
  597. parent has been set }
  598. if (oo_is_forward in childof.objectoptions) then
  599. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  600. else if not(oo_is_formal in childof.objectoptions) then
  601. current_objectdef.set_parent(childof)
  602. else
  603. Message1(sym_e_formal_class_not_resolved,childof.objrealname^);
  604. end;
  605. { remove forward flag, is resolved }
  606. exclude(current_structdef.objectoptions,oo_is_forward);
  607. if hasparentdefined then
  608. begin
  609. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  610. begin
  611. if assigned(intfchildof) then
  612. if current_objectdef.objecttype=odt_class then
  613. handleImplementedInterface(intfchildof)
  614. else
  615. handleImplementedProtocolOrJavaIntf(intfchildof);
  616. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  617. end;
  618. consume(_RKLAMMER);
  619. end;
  620. end;
  621. procedure parse_extended_type(helpertype:thelpertype);
  622. var
  623. hdef: tdef;
  624. begin
  625. if not is_objectpascal_helper(current_structdef) then
  626. Internalerror(2011021103);
  627. if helpertype=ht_none then
  628. Internalerror(2011021001);
  629. consume(_FOR);
  630. single_type(hdef,[stoParseClassParent]);
  631. if (not assigned(hdef)) or
  632. not (hdef.typ in [objectdef,recorddef]) then
  633. begin
  634. if helpertype=ht_class then
  635. Message1(type_e_class_type_expected,hdef.typename)
  636. else
  637. if helpertype=ht_record then
  638. Message1(type_e_record_type_expected,hdef.typename);
  639. end
  640. else
  641. begin
  642. case helpertype of
  643. ht_class:
  644. begin
  645. if not is_class(hdef) then
  646. Message1(type_e_class_type_expected,hdef.typename);
  647. { a class helper must extend the same class or a subclass
  648. of the class extended by the parent class helper }
  649. if assigned(current_objectdef.childof) then
  650. begin
  651. if not is_class(current_objectdef.childof.extendeddef) then
  652. Internalerror(2011021101);
  653. if not hdef.is_related(current_objectdef.childof.extendeddef) then
  654. Message1(type_e_class_helper_must_extend_subclass,current_objectdef.childof.extendeddef.typename);
  655. end;
  656. end;
  657. ht_record:
  658. begin
  659. if not is_record(hdef) then
  660. Message1(type_e_record_type_expected,hdef.typename);
  661. { a record helper must extend the same record as the
  662. parent helper }
  663. if assigned(current_objectdef.childof) then
  664. begin
  665. if not is_record(current_objectdef.childof.extendeddef) then
  666. Internalerror(2011021102);
  667. if hdef<>current_objectdef.childof.extendeddef then
  668. Message1(type_e_record_helper_must_extend_same_record,current_objectdef.childof.extendeddef.typename);
  669. end;
  670. end;
  671. end;
  672. current_objectdef.extendeddef:=tabstractrecorddef(hdef);
  673. end;
  674. end;
  675. procedure parse_guid;
  676. begin
  677. { read GUID }
  678. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  679. try_to_consume(_LECKKLAMMER) then
  680. begin
  681. readinterfaceiid;
  682. consume(_RECKKLAMMER);
  683. end
  684. else if (current_objectdef.objecttype=odt_dispinterface) then
  685. message(parser_e_dispinterface_needs_a_guid);
  686. end;
  687. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  688. procedure chkobjc(pd: tprocdef);
  689. begin
  690. if is_objc_class_or_protocol(pd.struct) then
  691. begin
  692. include(pd.procoptions,po_objc);
  693. end;
  694. end;
  695. procedure chkjava(pd: tprocdef);
  696. begin
  697. if is_java_class_or_interface(pd.struct) then
  698. begin
  699. include(pd.procoptions,po_java);
  700. { In java, all methods are either regular virtual methods,
  701. or static class methods }
  702. if [po_staticmethod,po_classmethod]*pd.procoptions=[po_classmethod] then
  703. messagepos(pd.fileinfo,type_e_java_class_method_not_static);
  704. end;
  705. end;
  706. procedure chkcpp(pd:tprocdef);
  707. begin
  708. { nothing currently }
  709. end;
  710. procedure maybe_parse_hint_directives(pd:tprocdef);
  711. var
  712. dummysymoptions : tsymoptions;
  713. deprecatedmsg : pshortstring;
  714. begin
  715. dummysymoptions:=[];
  716. deprecatedmsg:=nil;
  717. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  718. Consume(_SEMICOLON);
  719. if assigned(pd) then
  720. begin
  721. pd.symoptions:=pd.symoptions+dummysymoptions;
  722. pd.deprecatedmsg:=deprecatedmsg;
  723. end
  724. else
  725. stringdispose(deprecatedmsg);
  726. end;
  727. var
  728. oldparse_only: boolean;
  729. begin
  730. case token of
  731. _PROCEDURE,
  732. _FUNCTION:
  733. begin
  734. if (astruct.symtable.currentvisibility=vis_published) and
  735. not(oo_can_have_published in astruct.objectoptions) then
  736. Message(parser_e_cant_have_published);
  737. oldparse_only:=parse_only;
  738. parse_only:=true;
  739. result:=parse_proc_dec(is_classdef,astruct);
  740. { this is for error recovery as well as forward }
  741. { interface mappings, i.e. mapping to a method }
  742. { which isn't declared yet }
  743. if assigned(result) then
  744. begin
  745. parse_object_proc_directives(result);
  746. { check if dispid is set }
  747. if is_dispinterface(result.struct) and not (po_dispid in result.procoptions) then
  748. begin
  749. result.dispid:=tobjectdef(result.struct).get_next_dispid;
  750. include(result.procoptions, po_dispid);
  751. end;
  752. { all Macintosh Object Pascal methods are virtual. }
  753. { this can't be a class method, because macpas mode }
  754. { has no m_class }
  755. if (m_mac in current_settings.modeswitches) then
  756. include(result.procoptions,po_virtualmethod);
  757. { for record helpers only static class methods are allowed }
  758. if is_objectpascal_helper(astruct) and
  759. is_record(tobjectdef(astruct).extendeddef) and
  760. is_classdef and not (po_staticmethod in result.procoptions) then
  761. MessagePos(result.fileinfo,parser_e_class_methods_only_static_in_records);
  762. handle_calling_convention(result);
  763. { add definition to procsym }
  764. proc_add_definition(result);
  765. { add procdef options to objectdef options }
  766. if (po_msgint in result.procoptions) then
  767. include(astruct.objectoptions,oo_has_msgint);
  768. if (po_msgstr in result.procoptions) then
  769. include(astruct.objectoptions,oo_has_msgstr);
  770. if (po_virtualmethod in result.procoptions) then
  771. include(astruct.objectoptions,oo_has_virtual);
  772. chkcpp(result);
  773. chkobjc(result);
  774. chkjava(result);
  775. end;
  776. maybe_parse_hint_directives(result);
  777. parse_only:=oldparse_only;
  778. end;
  779. _CONSTRUCTOR :
  780. begin
  781. if (astruct.symtable.currentvisibility=vis_published) and
  782. not(oo_can_have_published in astruct.objectoptions) then
  783. Message(parser_e_cant_have_published);
  784. if not is_classdef and not(astruct.symtable.currentvisibility in [vis_public,vis_published]) then
  785. Message(parser_w_constructor_should_be_public);
  786. if is_interface(astruct) then
  787. Message(parser_e_no_con_des_in_interfaces);
  788. { Objective-C does not know the concept of a constructor }
  789. if is_objc_class_or_protocol(astruct) then
  790. Message(parser_e_objc_no_constructor_destructor);
  791. if is_objectpascal_helper(astruct) then
  792. if is_classdef then
  793. { class constructors are not allowed in class helpers }
  794. Message(parser_e_no_class_constructor_in_helpers)
  795. else if is_record(tobjectdef(astruct).extendeddef) then
  796. { as long as constructors aren't allowed in records they
  797. aren't allowed in helpers either }
  798. Message(parser_e_no_constructor_in_records);
  799. { only 1 class constructor is allowed }
  800. if is_classdef and (oo_has_class_constructor in astruct.objectoptions) then
  801. Message1(parser_e_only_one_class_constructor_allowed, astruct.objrealname^);
  802. oldparse_only:=parse_only;
  803. parse_only:=true;
  804. if is_classdef then
  805. result:=class_constructor_head
  806. else
  807. result:=constructor_head;
  808. parse_object_proc_directives(result);
  809. handle_calling_convention(result);
  810. { add definition to procsym }
  811. proc_add_definition(result);
  812. { add procdef options to objectdef options }
  813. if (po_virtualmethod in result.procoptions) then
  814. include(astruct.objectoptions,oo_has_virtual);
  815. chkcpp(result);
  816. maybe_parse_hint_directives(result);
  817. parse_only:=oldparse_only;
  818. end;
  819. _DESTRUCTOR :
  820. begin
  821. if (astruct.symtable.currentvisibility=vis_published) and
  822. not(oo_can_have_published in astruct.objectoptions) then
  823. Message(parser_e_cant_have_published);
  824. if not is_classdef then
  825. if (oo_has_destructor in astruct.objectoptions) then
  826. Message(parser_n_only_one_destructor);
  827. if is_interface(astruct) then
  828. Message(parser_e_no_con_des_in_interfaces);
  829. { (class) destructors are not allowed in class helpers }
  830. if is_objectpascal_helper(astruct) then
  831. Message(parser_e_no_destructor_in_records);
  832. if not is_classdef and (astruct.symtable.currentvisibility<>vis_public) then
  833. Message(parser_w_destructor_should_be_public);
  834. { Objective-C does not know the concept of a destructor }
  835. if is_objc_class_or_protocol(astruct) then
  836. Message(parser_e_objc_no_constructor_destructor);
  837. { only 1 class destructor is allowed }
  838. if is_classdef and (oo_has_class_destructor in astruct.objectoptions) then
  839. Message1(parser_e_only_one_class_destructor_allowed, astruct.objrealname^);
  840. oldparse_only:=parse_only;
  841. parse_only:=true;
  842. if is_classdef then
  843. result:=class_destructor_head
  844. else
  845. result:=destructor_head;
  846. parse_object_proc_directives(result);
  847. handle_calling_convention(result);
  848. { add definition to procsym }
  849. proc_add_definition(result);
  850. { add procdef options to objectdef options }
  851. if (po_virtualmethod in result.procoptions) then
  852. include(astruct.objectoptions,oo_has_virtual);
  853. chkcpp(result);
  854. maybe_parse_hint_directives(result);
  855. parse_only:=oldparse_only;
  856. end;
  857. else
  858. internalerror(2011032102);
  859. end;
  860. end;
  861. procedure parse_object_members;
  862. var
  863. typedconstswritable: boolean;
  864. object_member_blocktype : tblock_type;
  865. fields_allowed, is_classdef, class_fields, is_final, final_fields: boolean;
  866. vdoptions: tvar_dec_options;
  867. procedure parse_const;
  868. begin
  869. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  870. Message(parser_e_type_var_const_only_in_records_and_classes);
  871. consume(_CONST);
  872. object_member_blocktype:=bt_const;
  873. final_fields:=is_final;
  874. is_final:=false;
  875. end;
  876. procedure parse_var;
  877. begin
  878. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass]) and
  879. { Java interfaces can contain static final class vars }
  880. not((current_objectdef.objecttype=odt_interfacejava) and
  881. is_final and is_classdef) then
  882. Message(parser_e_type_var_const_only_in_records_and_classes);
  883. consume(_VAR);
  884. fields_allowed:=true;
  885. object_member_blocktype:=bt_general;
  886. class_fields:=is_classdef;
  887. final_fields:=is_final;
  888. is_classdef:=false;
  889. is_final:=false;
  890. end;
  891. procedure parse_class;
  892. begin
  893. is_classdef:=false;
  894. { read class method/field/property }
  895. consume(_CLASS);
  896. { class modifier is only allowed for procedures, functions, }
  897. { constructors, destructors, fields and properties }
  898. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  899. Message(parser_e_procedure_or_function_expected);
  900. { Java interfaces can contain final class vars }
  901. if is_interface(current_structdef) or
  902. (is_javainterface(current_structdef) and
  903. (not(is_final) or
  904. (token<>_VAR))) then
  905. Message(parser_e_no_static_method_in_interfaces)
  906. else
  907. { class methods are also allowed for Objective-C protocols }
  908. is_classdef:=true;
  909. end;
  910. procedure parse_visibility(vis: tvisibility; oo: tobjectoption);
  911. begin
  912. { Objective-C and Java classes do not support "published",
  913. as basically everything is published. }
  914. if (vis=vis_published) and
  915. (is_objc_class_or_protocol(current_structdef) or
  916. is_java_class_or_interface(current_structdef)) then
  917. Message(parser_e_no_objc_published)
  918. else if is_interface(current_structdef) or
  919. is_objc_protocol_or_category(current_structdef) or
  920. is_javainterface(current_structdef) then
  921. Message(parser_e_no_access_specifier_in_interfaces);
  922. current_structdef.symtable.currentvisibility:=vis;
  923. consume(token);
  924. if (oo<>oo_none) then
  925. include(current_structdef.objectoptions,oo);
  926. fields_allowed:=true;
  927. is_classdef:=false;
  928. class_fields:=false;
  929. is_final:=false;
  930. object_member_blocktype:=bt_general;
  931. end;
  932. begin
  933. { empty class declaration ? }
  934. if (current_objectdef.objecttype in [odt_class,odt_objcclass,odt_javaclass]) and
  935. (token=_SEMICOLON) then
  936. exit;
  937. { in "publishable" classes the default access type is published }
  938. if (oo_can_have_published in current_structdef.objectoptions) then
  939. current_structdef.symtable.currentvisibility:=vis_published
  940. else
  941. current_structdef.symtable.currentvisibility:=vis_public;
  942. fields_allowed:=true;
  943. is_classdef:=false;
  944. class_fields:=false;
  945. is_final:=false;
  946. final_fields:=false;
  947. object_member_blocktype:=bt_general;
  948. repeat
  949. case token of
  950. _TYPE :
  951. begin
  952. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  953. Message(parser_e_type_var_const_only_in_records_and_classes);
  954. consume(_TYPE);
  955. object_member_blocktype:=bt_type;
  956. end;
  957. _VAR :
  958. begin
  959. parse_var;
  960. end;
  961. _CONST:
  962. begin
  963. parse_const
  964. end;
  965. _ID :
  966. begin
  967. if is_objcprotocol(current_structdef) and
  968. ((idtoken=_REQUIRED) or
  969. (idtoken=_OPTIONAL)) then
  970. begin
  971. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  972. consume(idtoken)
  973. end
  974. else case idtoken of
  975. _PRIVATE :
  976. begin
  977. parse_visibility(vis_private,oo_has_private);
  978. end;
  979. _PROTECTED :
  980. begin
  981. parse_visibility(vis_protected,oo_has_protected);
  982. end;
  983. _PUBLIC :
  984. begin
  985. parse_visibility(vis_public,oo_none);
  986. end;
  987. _PUBLISHED :
  988. begin
  989. parse_visibility(vis_published,oo_none);
  990. end;
  991. _STRICT :
  992. begin
  993. if is_interface(current_structdef) or
  994. is_objc_protocol_or_category(current_structdef) or
  995. is_javainterface(current_structdef) then
  996. Message(parser_e_no_access_specifier_in_interfaces);
  997. consume(_STRICT);
  998. if token=_ID then
  999. begin
  1000. case idtoken of
  1001. _PRIVATE:
  1002. begin
  1003. consume(_PRIVATE);
  1004. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  1005. include(current_structdef.objectoptions,oo_has_strictprivate);
  1006. end;
  1007. _PROTECTED:
  1008. begin
  1009. consume(_PROTECTED);
  1010. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  1011. include(current_structdef.objectoptions,oo_has_strictprotected);
  1012. end;
  1013. else
  1014. message(parser_e_protected_or_private_expected);
  1015. end;
  1016. end
  1017. else
  1018. message(parser_e_protected_or_private_expected);
  1019. fields_allowed:=true;
  1020. is_classdef:=false;
  1021. class_fields:=false;
  1022. is_final:=false;
  1023. final_fields:=false;
  1024. object_member_blocktype:=bt_general;
  1025. end
  1026. else if (m_final_fields in current_settings.modeswitches) and
  1027. (token=_ID) and
  1028. (idtoken=_FINAL) then
  1029. begin
  1030. { currently only supported for external classes, because
  1031. requires fully working DFA otherwise }
  1032. if (current_structdef.typ<>objectdef) or
  1033. not(oo_is_external in tobjectdef(current_structdef).objectoptions) then
  1034. Message(parser_e_final_only_external);
  1035. consume(_final);
  1036. is_final:=true;
  1037. if token=_CLASS then
  1038. parse_class;
  1039. if not(token in [_CONST,_VAR]) then
  1040. message(parser_e_final_only_const_var);
  1041. end
  1042. else
  1043. begin
  1044. if object_member_blocktype=bt_general then
  1045. begin
  1046. if is_interface(current_structdef) or
  1047. is_objc_protocol_or_category(current_structdef) or
  1048. is_objectpascal_helper(current_structdef) or
  1049. (is_javainterface(current_structdef) and
  1050. not(class_fields and final_fields)) then
  1051. Message(parser_e_no_vars_in_interfaces);
  1052. if (current_structdef.symtable.currentvisibility=vis_published) and
  1053. not(oo_can_have_published in current_structdef.objectoptions) then
  1054. Message(parser_e_cant_have_published);
  1055. if (not fields_allowed) then
  1056. Message(parser_e_field_not_allowed_here);
  1057. vdoptions:=[vd_object];
  1058. if class_fields then
  1059. include(vdoptions,vd_class);
  1060. if final_fields then
  1061. include(vdoptions,vd_final);
  1062. read_record_fields(vdoptions);
  1063. end
  1064. else if object_member_blocktype=bt_type then
  1065. types_dec(true)
  1066. else if object_member_blocktype=bt_const then
  1067. begin
  1068. if final_fields then
  1069. begin
  1070. { the value of final fields cannot be changed
  1071. once they've been assigned a value }
  1072. typedconstswritable:=cs_typed_const_writable in current_settings.localswitches;
  1073. exclude(current_settings.localswitches,cs_typed_const_writable);
  1074. end;
  1075. consts_dec(true,not is_javainterface(current_structdef));
  1076. if final_fields and
  1077. typedconstswritable then
  1078. include(current_settings.localswitches,cs_typed_const_writable);
  1079. end
  1080. else
  1081. internalerror(201001110);
  1082. end;
  1083. end;
  1084. end;
  1085. _PROPERTY :
  1086. begin
  1087. struct_property_dec(is_classdef);
  1088. fields_allowed:=false;
  1089. is_classdef:=false;
  1090. end;
  1091. _CLASS:
  1092. begin
  1093. parse_class;
  1094. end;
  1095. _PROCEDURE,
  1096. _FUNCTION,
  1097. _CONSTRUCTOR,
  1098. _DESTRUCTOR :
  1099. begin
  1100. method_dec(current_structdef,is_classdef);
  1101. fields_allowed:=false;
  1102. is_classdef:=false;
  1103. end;
  1104. _END :
  1105. begin
  1106. consume(_END);
  1107. break;
  1108. end;
  1109. else
  1110. consume(_ID); { Give a ident expected message, like tp7 }
  1111. end;
  1112. until false;
  1113. end;
  1114. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  1115. var
  1116. old_current_structdef: tabstractrecorddef;
  1117. old_current_genericdef,
  1118. old_current_specializedef: tstoreddef;
  1119. old_parse_generic: boolean;
  1120. list: TFPObjectList;
  1121. s: String;
  1122. st: TSymtable;
  1123. begin
  1124. old_current_structdef:=current_structdef;
  1125. old_current_genericdef:=current_genericdef;
  1126. old_current_specializedef:=current_specializedef;
  1127. old_parse_generic:=parse_generic;
  1128. current_structdef:=nil;
  1129. current_genericdef:=nil;
  1130. current_specializedef:=nil;
  1131. { objects and class types can't be declared local }
  1132. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1133. not assigned(genericlist) then
  1134. Message(parser_e_no_local_objects);
  1135. { reuse forward objectdef? }
  1136. if assigned(fd) then
  1137. begin
  1138. if fd.objecttype<>objecttype then
  1139. begin
  1140. Message(parser_e_forward_mismatch);
  1141. { recover }
  1142. current_structdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  1143. include(current_structdef.objectoptions,oo_is_forward);
  1144. end
  1145. else
  1146. current_structdef:=fd
  1147. end
  1148. else
  1149. begin
  1150. { anonym objects aren't allow (o : object a : longint; end;) }
  1151. if n='' then
  1152. Message(parser_f_no_anonym_objects);
  1153. { create new class }
  1154. current_structdef:=tobjectdef.create(objecttype,n,nil);
  1155. { include always the forward flag, it'll be removed after the parent class have been
  1156. added. This is to prevent circular childof loops }
  1157. include(current_structdef.objectoptions,oo_is_forward);
  1158. if (cs_compilesystem in current_settings.moduleswitches) then
  1159. begin
  1160. case current_objectdef.objecttype of
  1161. odt_interfacecom :
  1162. if (current_structdef.objname^='IUNKNOWN') then
  1163. interface_iunknown:=current_objectdef
  1164. else
  1165. if (current_structdef.objname^='IDISPATCH') then
  1166. interface_idispatch:=current_objectdef;
  1167. odt_class :
  1168. if (current_structdef.objname^='TOBJECT') then
  1169. class_tobject:=current_objectdef;
  1170. odt_javaclass:
  1171. begin
  1172. if (current_objectdef.objname^='JLOBJECT') then
  1173. java_jlobject:=current_objectdef;
  1174. if (current_objectdef.objname^='JLTHROWABLE') then
  1175. java_jlthrowable:=current_objectdef;
  1176. if (current_objectdef.objname^='FPCBASERECORDTYPE') then
  1177. java_fpcbaserecordtype:=current_objectdef;
  1178. end;
  1179. end;
  1180. end;
  1181. if (current_module.modulename^='OBJCBASE') then
  1182. begin
  1183. case current_objectdef.objecttype of
  1184. odt_objcclass:
  1185. if (current_objectdef.objname^='Protocol') then
  1186. objc_protocoltype:=current_objectdef;
  1187. end;
  1188. end;
  1189. end;
  1190. { usage of specialized type inside its generic template }
  1191. if assigned(genericdef) then
  1192. current_specializedef:=current_structdef
  1193. { reject declaration of generic class inside generic class }
  1194. else if assigned(genericlist) then
  1195. current_genericdef:=current_structdef;
  1196. { set published flag in $M+ mode, it can also be inherited and will
  1197. be added when the parent class set with tobjectdef.set_parent (PFV) }
  1198. if (cs_generate_rtti in current_settings.localswitches) and
  1199. (current_objectdef.objecttype in [odt_interfacecom,odt_class,odt_helper]) then
  1200. include(current_structdef.objectoptions,oo_can_have_published);
  1201. { Objective-C/Java objectdefs can be "formal definitions", in which case
  1202. the syntax is "type tc = objcclass external;" -> we have to parse
  1203. its object options (external) already here, to make sure that such
  1204. definitions are recognised as formal defs }
  1205. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  1206. parse_object_options;
  1207. { forward def? }
  1208. if not assigned(fd) and
  1209. (token=_SEMICOLON) then
  1210. begin
  1211. { add to the list of definitions to check that the forward
  1212. is resolved. this is required for delphi mode }
  1213. current_module.checkforwarddefs.add(current_structdef);
  1214. end
  1215. else
  1216. begin
  1217. { change objccategories into objcclass helpers }
  1218. if (objecttype=odt_objccategory) then
  1219. begin
  1220. current_objectdef.objecttype:=odt_objcclass;
  1221. include(current_structdef.objectoptions,oo_is_classhelper);
  1222. end;
  1223. { include the class helper flag for Object Pascal helpers }
  1224. if (objecttype=odt_helper) then
  1225. include(current_objectdef.objectoptions,oo_is_classhelper);
  1226. { parse list of options (abstract / sealed) }
  1227. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava]) then
  1228. parse_object_options;
  1229. symtablestack.push(current_structdef.symtable);
  1230. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  1231. parse_generic:=(df_generic in current_structdef.defoptions);
  1232. { parse list of parent classes }
  1233. { for record helpers in mode Delphi this is not allowed }
  1234. if not (is_objectpascal_helper(current_objectdef) and
  1235. (m_delphi in current_settings.modeswitches) and
  1236. (helpertype=ht_record)) then
  1237. parse_parent_classes
  1238. else
  1239. { remove forward flag, is resolved (this is normally done inside
  1240. parse_parent_classes) }
  1241. exclude(current_structdef.objectoptions,oo_is_forward);
  1242. { parse extended type for helpers }
  1243. if is_objectpascal_helper(current_structdef) then
  1244. parse_extended_type(helpertype);
  1245. { parse optional GUID for interfaces }
  1246. parse_guid;
  1247. { parse and insert object members }
  1248. parse_object_members;
  1249. { In Java, constructors are not automatically inherited (so you can
  1250. hide them). Emulate the Pascal behaviour for classes implemented
  1251. in Pascal (we cannot do it for classes implemented in Java, since
  1252. we obviously cannot add constructors to those) }
  1253. if is_javaclass(current_structdef) and
  1254. not(oo_is_external in current_structdef.objectoptions) then
  1255. maybe_add_public_default_java_constructor(tobjectdef(current_structdef));
  1256. symtablestack.pop(current_structdef.symtable);
  1257. end;
  1258. { generate vmt space if needed }
  1259. if not(oo_has_vmt in current_structdef.objectoptions) and
  1260. not(oo_is_forward in current_structdef.objectoptions) and
  1261. (
  1262. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1263. (current_objectdef.objecttype in [odt_class])
  1264. ) then
  1265. current_objectdef.insertvmt;
  1266. { for implemented classes with a vmt check if there is a constructor }
  1267. if (oo_has_vmt in current_structdef.objectoptions) and
  1268. not(oo_is_forward in current_structdef.objectoptions) and
  1269. not(oo_has_constructor in current_structdef.objectoptions) and
  1270. not is_objc_class_or_protocol(current_structdef) and
  1271. not is_java_class_or_interface(current_structdef) then
  1272. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1273. if is_interface(current_structdef) or
  1274. is_objcprotocol(current_structdef) or
  1275. is_javainterface(current_structdef) then
  1276. setinterfacemethodoptions
  1277. else if is_objcclass(current_structdef) then
  1278. setobjcclassmethodoptions;
  1279. { if this helper is defined in the implementation section of the unit
  1280. or inside the main project file, the extendeddefs list of the current
  1281. module must be updated (it will be removed when poping the symtable) }
  1282. if is_objectpascal_helper(current_structdef) then
  1283. begin
  1284. { the topmost symtable must be a static symtable }
  1285. st:=current_structdef.owner;
  1286. while st.symtabletype in [objectsymtable,recordsymtable] do
  1287. st:=st.defowner.owner;
  1288. if st.symtabletype=staticsymtable then
  1289. begin
  1290. s:=make_mangledname('',current_objectdef.extendeddef.symtable,'');
  1291. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1292. if not assigned(list) then
  1293. begin
  1294. list:=TFPObjectList.Create(false);
  1295. current_module.extendeddefs.Add(s, list);
  1296. end;
  1297. list.add(current_structdef);
  1298. end;
  1299. end;
  1300. tabstractrecordsymtable(current_objectdef.symtable).addalignmentpadding;
  1301. { return defined objectdef }
  1302. result:=current_objectdef;
  1303. { restore old state }
  1304. current_structdef:=old_current_structdef;
  1305. current_genericdef:=old_current_genericdef;
  1306. current_specializedef:=old_current_specializedef;
  1307. parse_generic:=old_parse_generic;
  1308. end;
  1309. end.