pdecobj.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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;objsym:tsym;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. { check if we shall use another name for the class }
  389. if try_to_consume(_NAME) then
  390. od.objextname:=stringdup(get_stringconst)
  391. else
  392. od.objextname:=stringdup(od.objrealname^);
  393. include(od.objectoptions,oo_is_external);
  394. end
  395. else
  396. begin
  397. od.objextname:=stringdup(od.objrealname^);
  398. end;
  399. end;
  400. procedure get_objc_class_or_protocol_external_status(od: tobjectdef);
  401. begin
  402. { Objective-C classes can be external -> all messages inside are
  403. external (defined at the class level instead of per method, so
  404. that you cannot define some methods as external and some not)
  405. }
  406. if try_to_consume(_EXTERNAL) then
  407. begin
  408. if try_to_consume(_NAME) then
  409. od.objextname:=stringdup(get_stringconst)
  410. else
  411. { the external name doesn't matter for formally declared
  412. classes, and allowing to specify one would mean that we would
  413. have to check it for consistency with the actual definition
  414. later on }
  415. od.objextname:=stringdup(od.objrealname^);
  416. include(od.objectoptions,oo_is_external);
  417. end
  418. else
  419. od.objextname:=stringdup(od.objrealname^);
  420. end;
  421. procedure parse_object_options;
  422. var
  423. gotexternal: boolean;
  424. begin
  425. case current_objectdef.objecttype of
  426. odt_object,odt_class,
  427. odt_javaclass:
  428. begin
  429. gotexternal:=false;
  430. while true do
  431. begin
  432. if try_to_consume(_ABSTRACT) then
  433. include(current_structdef.objectoptions,oo_is_abstract)
  434. else
  435. if try_to_consume(_SEALED) then
  436. include(current_structdef.objectoptions,oo_is_sealed)
  437. else if (current_objectdef.objecttype=odt_javaclass) and
  438. (token=_ID) and
  439. (idtoken=_EXTERNAL) then
  440. begin
  441. get_cpp_or_java_class_external_status(current_objectdef);
  442. gotexternal:=true;
  443. end
  444. else
  445. break;
  446. end;
  447. { don't use <=, because there's a bug in the 2.6.0 SPARC code
  448. generator regarding handling this expression }
  449. if ([oo_is_abstract, oo_is_sealed] * current_structdef.objectoptions) = [oo_is_abstract, oo_is_sealed] then
  450. Message(parser_e_abstract_and_sealed_conflict);
  451. { set default external name in case of no external directive }
  452. if (current_objectdef.objecttype=odt_javaclass) and
  453. not gotexternal then
  454. get_cpp_or_java_class_external_status(current_objectdef)
  455. end;
  456. odt_cppclass,
  457. odt_interfacejava:
  458. get_cpp_or_java_class_external_status(current_objectdef);
  459. odt_objcclass,odt_objcprotocol,odt_objccategory:
  460. get_objc_class_or_protocol_external_status(current_objectdef);
  461. odt_helper: ; // nothing
  462. end;
  463. end;
  464. procedure parse_parent_classes;
  465. var
  466. intfchildof,
  467. childof : tobjectdef;
  468. hdef : tdef;
  469. hasparentdefined : boolean;
  470. begin
  471. childof:=nil;
  472. intfchildof:=nil;
  473. hasparentdefined:=false;
  474. { reads the parent class }
  475. if (token=_LKLAMMER) or
  476. is_objccategory(current_structdef) then
  477. begin
  478. consume(_LKLAMMER);
  479. { use single_type instead of id_type for specialize support }
  480. single_type(hdef,[stoAllowSpecialization, stoParseClassParent]);
  481. if (not assigned(hdef)) or
  482. (hdef.typ<>objectdef) then
  483. begin
  484. if assigned(hdef) then
  485. Message1(type_e_class_type_expected,hdef.typename)
  486. else if is_objccategory(current_structdef) then
  487. { a category must specify the class to extend }
  488. Message(type_e_objcclass_type_expected);
  489. end
  490. else
  491. begin
  492. childof:=tobjectdef(hdef);
  493. { a mix of class, interfaces, objects and cppclasses
  494. isn't allowed }
  495. case current_objectdef.objecttype of
  496. odt_class,
  497. odt_javaclass:
  498. if (childof.objecttype<>current_objectdef.objecttype) then
  499. begin
  500. if (is_interface(childof) and
  501. is_class(current_objectdef)) or
  502. (is_javainterface(childof) and
  503. is_javaclass(current_objectdef)) then
  504. begin
  505. { we insert the interface after the child
  506. is set, see below
  507. }
  508. intfchildof:=childof;
  509. childof:=class_tobject;
  510. end
  511. else
  512. Message(parser_e_mix_of_classes_and_objects);
  513. end
  514. else
  515. if oo_is_sealed in childof.objectoptions then
  516. Message1(parser_e_sealed_descendant,childof.typename)
  517. else
  518. childof:=find_real_class_definition(childof,true);
  519. odt_interfacecorba,
  520. odt_interfacecom:
  521. begin
  522. if not(is_interface(childof)) then
  523. Message(parser_e_mix_of_classes_and_objects);
  524. current_objectdef.objecttype:=childof.objecttype;
  525. end;
  526. odt_cppclass:
  527. if not(is_cppclass(childof)) then
  528. Message(parser_e_mix_of_classes_and_objects);
  529. odt_objcclass:
  530. if not(is_objcclass(childof) or
  531. is_objccategory(childof)) then
  532. begin
  533. if is_objcprotocol(childof) then
  534. begin
  535. if not(oo_is_classhelper in current_structdef.objectoptions) then
  536. begin
  537. intfchildof:=childof;
  538. childof:=nil;
  539. CGMessage(parser_h_no_objc_parent);
  540. end
  541. else
  542. { a category must specify the class to extend }
  543. CGMessage(type_e_objcclass_type_expected);
  544. end
  545. else
  546. Message(parser_e_mix_of_classes_and_objects);
  547. end
  548. else
  549. childof:=find_real_class_definition(childof,true);
  550. odt_objcprotocol:
  551. begin
  552. if not(is_objcprotocol(childof)) then
  553. Message(parser_e_mix_of_classes_and_objects);
  554. intfchildof:=childof;
  555. childof:=nil;
  556. end;
  557. odt_interfacejava:
  558. begin
  559. if not(is_javainterface(childof)) then
  560. Message(parser_e_mix_of_classes_and_objects);
  561. intfchildof:=find_real_class_definition(childof,true);
  562. childof:=nil;
  563. end;
  564. odt_object:
  565. if not(is_object(childof)) then
  566. Message(parser_e_mix_of_classes_and_objects)
  567. else
  568. if oo_is_sealed in childof.objectoptions then
  569. Message1(parser_e_sealed_descendant,childof.typename);
  570. odt_dispinterface:
  571. Message(parser_e_dispinterface_cant_have_parent);
  572. odt_helper:
  573. if not is_objectpascal_helper(childof) then
  574. begin
  575. Message(type_e_helper_type_expected);
  576. childof:=nil;
  577. end;
  578. end;
  579. end;
  580. hasparentdefined:=true;
  581. end;
  582. { if no parent class, then a class get tobject as parent }
  583. if not assigned(childof) then
  584. begin
  585. case current_objectdef.objecttype of
  586. odt_class:
  587. if current_objectdef<>class_tobject then
  588. childof:=class_tobject;
  589. odt_interfacecom:
  590. if current_objectdef<>interface_iunknown then
  591. childof:=interface_iunknown;
  592. odt_dispinterface:
  593. childof:=interface_idispatch;
  594. odt_objcclass:
  595. CGMessage(parser_h_no_objc_parent);
  596. odt_javaclass:
  597. { inherit from TObject by default for compatibility }
  598. if current_objectdef<>java_jlobject then
  599. childof:=class_tobject;
  600. end;
  601. end;
  602. if assigned(childof) then
  603. begin
  604. { Forbid not completly defined objects to be used as parents. This will
  605. also prevent circular loops of classes, because we set the forward flag
  606. at the start of the new definition and will reset it below after the
  607. parent has been set }
  608. if (oo_is_forward in childof.objectoptions) then
  609. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  610. else if not(oo_is_formal in childof.objectoptions) then
  611. current_objectdef.set_parent(childof)
  612. else
  613. Message1(sym_e_formal_class_not_resolved,childof.objrealname^);
  614. end;
  615. { remove forward flag, is resolved }
  616. exclude(current_structdef.objectoptions,oo_is_forward);
  617. if hasparentdefined then
  618. begin
  619. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  620. begin
  621. if assigned(intfchildof) then
  622. if current_objectdef.objecttype=odt_class then
  623. handleImplementedInterface(intfchildof)
  624. else
  625. handleImplementedProtocolOrJavaIntf(intfchildof);
  626. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  627. end;
  628. consume(_RKLAMMER);
  629. end;
  630. end;
  631. procedure parse_extended_type(helpertype:thelpertype);
  632. var
  633. hdef: tdef;
  634. begin
  635. if not is_objectpascal_helper(current_structdef) then
  636. Internalerror(2011021103);
  637. if helpertype=ht_none then
  638. Internalerror(2011021001);
  639. consume(_FOR);
  640. single_type(hdef,[stoParseClassParent]);
  641. if (not assigned(hdef)) or
  642. not (hdef.typ in [objectdef,recorddef]) then
  643. begin
  644. if helpertype=ht_class then
  645. Message1(type_e_class_type_expected,hdef.typename)
  646. else
  647. if helpertype=ht_record then
  648. Message1(type_e_record_type_expected,hdef.typename);
  649. end
  650. else
  651. begin
  652. case helpertype of
  653. ht_class:
  654. begin
  655. if not is_class(hdef) then
  656. Message1(type_e_class_type_expected,hdef.typename);
  657. { a class helper must extend the same class or a subclass
  658. of the class extended by the parent class helper }
  659. if assigned(current_objectdef.childof) then
  660. begin
  661. if not is_class(current_objectdef.childof.extendeddef) then
  662. Internalerror(2011021101);
  663. if not hdef.is_related(current_objectdef.childof.extendeddef) then
  664. Message1(type_e_class_helper_must_extend_subclass,current_objectdef.childof.extendeddef.typename);
  665. end;
  666. end;
  667. ht_record:
  668. begin
  669. if not is_record(hdef) then
  670. Message1(type_e_record_type_expected,hdef.typename);
  671. { a record helper must extend the same record as the
  672. parent helper }
  673. if assigned(current_objectdef.childof) then
  674. begin
  675. if not is_record(current_objectdef.childof.extendeddef) then
  676. Internalerror(2011021102);
  677. if hdef<>current_objectdef.childof.extendeddef then
  678. Message1(type_e_record_helper_must_extend_same_record,current_objectdef.childof.extendeddef.typename);
  679. end;
  680. end;
  681. else
  682. hdef:=nil;
  683. end;
  684. end;
  685. if assigned(hdef) then
  686. current_objectdef.extendeddef:=hdef
  687. else
  688. current_objectdef.extendeddef:=generrordef;
  689. end;
  690. procedure parse_guid;
  691. begin
  692. { read GUID }
  693. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  694. try_to_consume(_LECKKLAMMER) then
  695. begin
  696. readinterfaceiid;
  697. consume(_RECKKLAMMER);
  698. end
  699. else if (current_objectdef.objecttype=odt_dispinterface) then
  700. message(parser_e_dispinterface_needs_a_guid);
  701. end;
  702. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  703. procedure chkobjc(pd: tprocdef);
  704. begin
  705. if is_objc_class_or_protocol(pd.struct) then
  706. begin
  707. include(pd.procoptions,po_objc);
  708. end;
  709. end;
  710. procedure chkjava(pd: tprocdef);
  711. begin
  712. {$ifdef jvm}
  713. if is_java_class_or_interface(pd.struct) then
  714. begin
  715. { mark all non-virtual instance methods as "virtual; final;",
  716. because
  717. a) that's the only way to guarantee "non-virtual" behaviour
  718. (other than making them class methods with an explicit self
  719. pointer, but that causes problems with interface mappings
  720. and procvars)
  721. b) if we don't mark them virtual, they don't get added to the
  722. vmt and we can't check whether child classes try to override
  723. them
  724. }
  725. if is_javaclass(pd.struct) then
  726. begin
  727. if not(po_virtualmethod in pd.procoptions) and
  728. not(po_classmethod in pd.procoptions) then
  729. begin
  730. include(pd.procoptions,po_virtualmethod);
  731. include(pd.procoptions,po_finalmethod);
  732. include(pd.procoptions,po_java_nonvirtual);
  733. end
  734. else if [po_virtualmethod,po_classmethod]<=pd.procoptions then
  735. begin
  736. if po_staticmethod in pd.procoptions then
  737. Message(type_e_java_class_method_not_static_virtual);
  738. end;
  739. end;
  740. end;
  741. {$endif}
  742. end;
  743. procedure chkcpp(pd:tprocdef);
  744. begin
  745. { nothing currently }
  746. end;
  747. var
  748. oldparse_only: boolean;
  749. begin
  750. case token of
  751. _PROCEDURE,
  752. _FUNCTION:
  753. begin
  754. if (astruct.symtable.currentvisibility=vis_published) and
  755. not(oo_can_have_published in astruct.objectoptions) then
  756. Message(parser_e_cant_have_published);
  757. oldparse_only:=parse_only;
  758. parse_only:=true;
  759. result:=parse_proc_dec(is_classdef,astruct);
  760. { this is for error recovery as well as forward }
  761. { interface mappings, i.e. mapping to a method }
  762. { which isn't declared yet }
  763. if assigned(result) then
  764. begin
  765. parse_object_proc_directives(result);
  766. { check if dispid is set }
  767. if is_dispinterface(result.struct) and not (po_dispid in result.procoptions) then
  768. begin
  769. result.dispid:=tobjectdef(result.struct).get_next_dispid;
  770. include(result.procoptions, po_dispid);
  771. end;
  772. { all Macintosh Object Pascal methods are virtual. }
  773. { this can't be a class method, because macpas mode }
  774. { has no m_class }
  775. if (m_mac in current_settings.modeswitches) then
  776. include(result.procoptions,po_virtualmethod);
  777. { for record helpers only static class methods are allowed }
  778. if is_objectpascal_helper(astruct) and
  779. is_record(tobjectdef(astruct).extendeddef) and
  780. is_classdef and not (po_staticmethod in result.procoptions) then
  781. MessagePos(result.fileinfo,parser_e_class_methods_only_static_in_records);
  782. handle_calling_convention(result);
  783. { add definition to procsym }
  784. proc_add_definition(result);
  785. { add procdef options to objectdef options }
  786. if (po_msgint in result.procoptions) then
  787. include(astruct.objectoptions,oo_has_msgint);
  788. if (po_msgstr in result.procoptions) then
  789. include(astruct.objectoptions,oo_has_msgstr);
  790. if (po_virtualmethod in result.procoptions) then
  791. include(astruct.objectoptions,oo_has_virtual);
  792. chkcpp(result);
  793. chkobjc(result);
  794. chkjava(result);
  795. end;
  796. maybe_parse_hint_directives(result);
  797. parse_only:=oldparse_only;
  798. end;
  799. _CONSTRUCTOR :
  800. begin
  801. if (astruct.symtable.currentvisibility=vis_published) and
  802. not(oo_can_have_published in astruct.objectoptions) then
  803. Message(parser_e_cant_have_published);
  804. if not is_classdef and not(astruct.symtable.currentvisibility in [vis_public,vis_published]) then
  805. Message(parser_w_constructor_should_be_public);
  806. if is_interface(astruct) then
  807. Message(parser_e_no_con_des_in_interfaces);
  808. { Objective-C does not know the concept of a constructor }
  809. if is_objc_class_or_protocol(astruct) then
  810. Message(parser_e_objc_no_constructor_destructor);
  811. if is_objectpascal_helper(astruct) then
  812. if is_classdef then
  813. { class constructors are not allowed in class helpers }
  814. Message(parser_e_no_class_constructor_in_helpers)
  815. else if is_record(tobjectdef(astruct).extendeddef) then
  816. { as long as constructors aren't allowed in records they
  817. aren't allowed in helpers either }
  818. Message(parser_e_no_constructor_in_records);
  819. { only 1 class constructor is allowed }
  820. if is_classdef and (oo_has_class_constructor in astruct.objectoptions) then
  821. Message1(parser_e_only_one_class_constructor_allowed, astruct.objrealname^);
  822. oldparse_only:=parse_only;
  823. parse_only:=true;
  824. if is_classdef then
  825. result:=class_constructor_head(current_structdef)
  826. else
  827. result:=constructor_head;
  828. chkcpp(result);
  829. parse_only:=oldparse_only;
  830. end;
  831. _DESTRUCTOR :
  832. begin
  833. if (astruct.symtable.currentvisibility=vis_published) and
  834. not(oo_can_have_published in astruct.objectoptions) then
  835. Message(parser_e_cant_have_published);
  836. if not is_classdef then
  837. if (oo_has_new_destructor in astruct.objectoptions) then
  838. Message(parser_n_only_one_destructor);
  839. if is_interface(astruct) then
  840. Message(parser_e_no_con_des_in_interfaces);
  841. { (class) destructors are not allowed in class helpers }
  842. if is_objectpascal_helper(astruct) then
  843. Message(parser_e_no_destructor_in_records);
  844. if not is_classdef and (astruct.symtable.currentvisibility<>vis_public) then
  845. Message(parser_w_destructor_should_be_public);
  846. { Objective-C does not know the concept of a destructor }
  847. if is_objc_class_or_protocol(astruct) then
  848. Message(parser_e_objc_no_constructor_destructor);
  849. { only 1 class destructor is allowed }
  850. if is_classdef and (oo_has_class_destructor in astruct.objectoptions) then
  851. Message1(parser_e_only_one_class_destructor_allowed, astruct.objrealname^);
  852. oldparse_only:=parse_only;
  853. parse_only:=true;
  854. if is_classdef then
  855. result:=class_destructor_head(current_structdef)
  856. else
  857. result:=destructor_head;
  858. chkcpp(result);
  859. parse_only:=oldparse_only;
  860. end;
  861. else
  862. internalerror(2011032102);
  863. end;
  864. end;
  865. procedure parse_object_members;
  866. var
  867. typedconstswritable: boolean;
  868. object_member_blocktype : tblock_type;
  869. fields_allowed, is_classdef, class_fields, is_final, final_fields: boolean;
  870. vdoptions: tvar_dec_options;
  871. procedure parse_const;
  872. begin
  873. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  874. Message(parser_e_type_var_const_only_in_records_and_classes);
  875. consume(_CONST);
  876. object_member_blocktype:=bt_const;
  877. final_fields:=is_final;
  878. is_final:=false;
  879. end;
  880. procedure parse_var;
  881. begin
  882. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass]) and
  883. { Java interfaces can contain static final class vars }
  884. not((current_objectdef.objecttype=odt_interfacejava) and
  885. is_final and is_classdef) then
  886. Message(parser_e_type_var_const_only_in_records_and_classes);
  887. consume(_VAR);
  888. fields_allowed:=true;
  889. object_member_blocktype:=bt_general;
  890. class_fields:=is_classdef;
  891. final_fields:=is_final;
  892. is_classdef:=false;
  893. is_final:=false;
  894. end;
  895. procedure parse_class;
  896. begin
  897. is_classdef:=false;
  898. { read class method/field/property }
  899. consume(_CLASS);
  900. { class modifier is only allowed for procedures, functions, }
  901. { constructors, destructors, fields and properties }
  902. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  903. Message(parser_e_procedure_or_function_expected);
  904. { Java interfaces can contain final class vars }
  905. if is_interface(current_structdef) or
  906. (is_javainterface(current_structdef) and
  907. (not(is_final) or
  908. (token<>_VAR))) then
  909. Message(parser_e_no_static_method_in_interfaces)
  910. else
  911. { class methods are also allowed for Objective-C protocols }
  912. is_classdef:=true;
  913. end;
  914. procedure parse_visibility(vis: tvisibility; oo: tobjectoption);
  915. begin
  916. { Objective-C and Java classes do not support "published",
  917. as basically everything is published. }
  918. if (vis=vis_published) and
  919. (is_objc_class_or_protocol(current_structdef) or
  920. is_java_class_or_interface(current_structdef)) then
  921. Message(parser_e_no_objc_published)
  922. else if is_interface(current_structdef) or
  923. is_objc_protocol_or_category(current_structdef) or
  924. is_javainterface(current_structdef) then
  925. Message(parser_e_no_access_specifier_in_interfaces);
  926. current_structdef.symtable.currentvisibility:=vis;
  927. consume(token);
  928. if (oo<>oo_none) then
  929. include(current_structdef.objectoptions,oo);
  930. fields_allowed:=true;
  931. is_classdef:=false;
  932. class_fields:=false;
  933. is_final:=false;
  934. object_member_blocktype:=bt_general;
  935. end;
  936. begin
  937. { empty class declaration ? }
  938. if (current_objectdef.objecttype in [odt_class,odt_objcclass,odt_javaclass]) and
  939. (token=_SEMICOLON) then
  940. exit;
  941. { in "publishable" classes the default access type is published }
  942. if (oo_can_have_published in current_structdef.objectoptions) then
  943. current_structdef.symtable.currentvisibility:=vis_published
  944. else
  945. current_structdef.symtable.currentvisibility:=vis_public;
  946. fields_allowed:=true;
  947. is_classdef:=false;
  948. class_fields:=false;
  949. is_final:=false;
  950. final_fields:=false;
  951. object_member_blocktype:=bt_general;
  952. repeat
  953. case token of
  954. _TYPE :
  955. begin
  956. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  957. Message(parser_e_type_var_const_only_in_records_and_classes);
  958. consume(_TYPE);
  959. object_member_blocktype:=bt_type;
  960. end;
  961. _VAR :
  962. begin
  963. parse_var;
  964. end;
  965. _CONST:
  966. begin
  967. parse_const
  968. end;
  969. _ID :
  970. begin
  971. if is_objcprotocol(current_structdef) and
  972. ((idtoken=_REQUIRED) or
  973. (idtoken=_OPTIONAL)) then
  974. begin
  975. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  976. consume(idtoken)
  977. end
  978. else case idtoken of
  979. _PRIVATE :
  980. begin
  981. parse_visibility(vis_private,oo_has_private);
  982. end;
  983. _PROTECTED :
  984. begin
  985. parse_visibility(vis_protected,oo_has_protected);
  986. end;
  987. _PUBLIC :
  988. begin
  989. parse_visibility(vis_public,oo_none);
  990. end;
  991. _PUBLISHED :
  992. begin
  993. parse_visibility(vis_published,oo_none);
  994. end;
  995. _STRICT :
  996. begin
  997. if is_interface(current_structdef) or
  998. is_objc_protocol_or_category(current_structdef) or
  999. is_javainterface(current_structdef) then
  1000. Message(parser_e_no_access_specifier_in_interfaces);
  1001. consume(_STRICT);
  1002. if token=_ID then
  1003. begin
  1004. case idtoken of
  1005. _PRIVATE:
  1006. begin
  1007. consume(_PRIVATE);
  1008. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  1009. include(current_structdef.objectoptions,oo_has_strictprivate);
  1010. end;
  1011. _PROTECTED:
  1012. begin
  1013. consume(_PROTECTED);
  1014. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  1015. include(current_structdef.objectoptions,oo_has_strictprotected);
  1016. end;
  1017. else
  1018. message(parser_e_protected_or_private_expected);
  1019. end;
  1020. end
  1021. else
  1022. message(parser_e_protected_or_private_expected);
  1023. fields_allowed:=true;
  1024. is_classdef:=false;
  1025. class_fields:=false;
  1026. is_final:=false;
  1027. final_fields:=false;
  1028. object_member_blocktype:=bt_general;
  1029. end
  1030. else if (m_final_fields in current_settings.modeswitches) and
  1031. (token=_ID) and
  1032. (idtoken=_FINAL) then
  1033. begin
  1034. { currently only supported for external classes, because
  1035. requires fully working DFA otherwise }
  1036. if (current_structdef.typ<>objectdef) or
  1037. not(oo_is_external in tobjectdef(current_structdef).objectoptions) then
  1038. Message(parser_e_final_only_external);
  1039. consume(_final);
  1040. is_final:=true;
  1041. if token=_CLASS then
  1042. parse_class;
  1043. if not(token in [_CONST,_VAR]) then
  1044. message(parser_e_final_only_const_var);
  1045. end
  1046. else
  1047. begin
  1048. if object_member_blocktype=bt_general then
  1049. begin
  1050. if is_interface(current_structdef) or
  1051. is_objc_protocol_or_category(current_structdef) or
  1052. is_objectpascal_helper(current_structdef) or
  1053. (is_javainterface(current_structdef) and
  1054. not(class_fields and final_fields)) then
  1055. Message(parser_e_no_vars_in_interfaces);
  1056. if (current_structdef.symtable.currentvisibility=vis_published) and
  1057. not(oo_can_have_published in current_structdef.objectoptions) then
  1058. Message(parser_e_cant_have_published);
  1059. if (not fields_allowed) then
  1060. Message(parser_e_field_not_allowed_here);
  1061. vdoptions:=[vd_object];
  1062. if class_fields then
  1063. include(vdoptions,vd_class);
  1064. if final_fields then
  1065. include(vdoptions,vd_final);
  1066. read_record_fields(vdoptions);
  1067. end
  1068. else if object_member_blocktype=bt_type then
  1069. types_dec(true)
  1070. else if object_member_blocktype=bt_const then
  1071. begin
  1072. if final_fields then
  1073. begin
  1074. { the value of final fields cannot be changed
  1075. once they've been assigned a value }
  1076. typedconstswritable:=cs_typed_const_writable in current_settings.localswitches;
  1077. exclude(current_settings.localswitches,cs_typed_const_writable);
  1078. end;
  1079. consts_dec(true,not is_javainterface(current_structdef));
  1080. if final_fields and
  1081. typedconstswritable then
  1082. include(current_settings.localswitches,cs_typed_const_writable);
  1083. end
  1084. else
  1085. internalerror(201001110);
  1086. end;
  1087. end;
  1088. end;
  1089. _PROPERTY :
  1090. begin
  1091. struct_property_dec(is_classdef);
  1092. fields_allowed:=false;
  1093. is_classdef:=false;
  1094. end;
  1095. _CLASS:
  1096. begin
  1097. parse_class;
  1098. end;
  1099. _PROCEDURE,
  1100. _FUNCTION,
  1101. _CONSTRUCTOR,
  1102. _DESTRUCTOR :
  1103. begin
  1104. method_dec(current_structdef,is_classdef);
  1105. fields_allowed:=false;
  1106. is_classdef:=false;
  1107. end;
  1108. _END :
  1109. begin
  1110. consume(_END);
  1111. break;
  1112. end;
  1113. else
  1114. consume(_ID); { Give a ident expected message, like tp7 }
  1115. end;
  1116. until false;
  1117. end;
  1118. function object_dec(objecttype:tobjecttyp;const n:tidstring;objsym:tsym;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  1119. var
  1120. old_current_structdef: tabstractrecorddef;
  1121. old_current_genericdef,
  1122. old_current_specializedef: tstoreddef;
  1123. hrecst: trecordsymtable;
  1124. fsym: tfieldvarsym;
  1125. old_parse_generic: boolean;
  1126. list: TFPObjectList;
  1127. s: String;
  1128. st: TSymtable;
  1129. olddef: tdef;
  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. { classes can handle links to themself not only inside type blocks
  1299. but in const blocks too. to make this possible we need to set
  1300. their symbols to real defs instead of errordef }
  1301. if assigned(objsym) and (objecttype in [odt_class,odt_javaclass,odt_interfacejava]) then
  1302. begin
  1303. olddef:=ttypesym(objsym).typedef;
  1304. ttypesym(objsym).typedef:=current_structdef;
  1305. end
  1306. else
  1307. olddef:=nil;
  1308. { parse and insert object members }
  1309. parse_object_members;
  1310. if assigned(olddef) then
  1311. ttypesym(objsym).typedef:=olddef;
  1312. if not(oo_is_external in current_structdef.objectoptions) then
  1313. begin
  1314. { In Java, constructors are not automatically inherited (so you can
  1315. hide them). Emulate the Pascal behaviour for classes implemented
  1316. in Pascal (we cannot do it for classes implemented in Java, since
  1317. we obviously cannot add constructors to those) }
  1318. if is_javaclass(current_structdef) then
  1319. begin
  1320. add_missing_parent_constructors_intf(tobjectdef(current_structdef),true,vis_none);
  1321. {$ifdef jvm}
  1322. maybe_add_public_default_java_constructor(tobjectdef(current_structdef));
  1323. jvm_wrap_virtual_class_methods(tobjectdef(current_structdef));
  1324. {$endif}
  1325. end;
  1326. { need method to hold the initialization code for typed constants? }
  1327. if (target_info.system in systems_typed_constants_node_init) and
  1328. not is_any_interface_kind(current_structdef) then
  1329. add_typedconst_init_routine(current_structdef);
  1330. end;
  1331. symtablestack.pop(current_structdef.symtable);
  1332. end;
  1333. { generate vmt space if needed }
  1334. if not(oo_has_vmt in current_structdef.objectoptions) and
  1335. not(oo_is_forward in current_structdef.objectoptions) and
  1336. (
  1337. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1338. (current_objectdef.objecttype in [odt_class])
  1339. ) then
  1340. current_objectdef.insertvmt;
  1341. { for implemented classes with a vmt check if there is a constructor }
  1342. if (oo_has_vmt in current_structdef.objectoptions) and
  1343. not(oo_is_forward in current_structdef.objectoptions) and
  1344. not(oo_has_constructor in current_structdef.objectoptions) and
  1345. not is_objc_class_or_protocol(current_structdef) and
  1346. not is_java_class_or_interface(current_structdef) then
  1347. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1348. if is_interface(current_structdef) or
  1349. is_objcprotocol(current_structdef) or
  1350. is_javainterface(current_structdef) then
  1351. setinterfacemethodoptions
  1352. else if is_objcclass(current_structdef) then
  1353. setobjcclassmethodoptions;
  1354. { if this helper is defined in the implementation section of the unit
  1355. or inside the main project file, the extendeddefs list of the current
  1356. module must be updated (it will be removed when poping the symtable) }
  1357. if is_objectpascal_helper(current_structdef) and
  1358. (current_objectdef.extendeddef.typ in [recorddef,objectdef]) then
  1359. begin
  1360. { the topmost symtable must be a static symtable }
  1361. st:=current_structdef.owner;
  1362. while st.symtabletype in [objectsymtable,recordsymtable] do
  1363. st:=st.defowner.owner;
  1364. if st.symtabletype=staticsymtable then
  1365. begin
  1366. s:=make_mangledname('',tabstractrecorddef(current_objectdef.extendeddef).symtable,'');
  1367. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1368. if not assigned(list) then
  1369. begin
  1370. list:=TFPObjectList.Create(false);
  1371. current_module.extendeddefs.Add(s, list);
  1372. end;
  1373. list.add(current_structdef);
  1374. end;
  1375. end;
  1376. tabstractrecordsymtable(current_objectdef.symtable).addalignmentpadding;
  1377. { return defined objectdef }
  1378. result:=current_objectdef;
  1379. { restore old state }
  1380. current_structdef:=old_current_structdef;
  1381. current_genericdef:=old_current_genericdef;
  1382. current_specializedef:=old_current_specializedef;
  1383. parse_generic:=old_parse_generic;
  1384. end;
  1385. end.