pdecobj.pas 58 KB

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