pdecobj.pas 60 KB

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