pdecobj.pas 60 KB

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