pdecobj.pas 67 KB

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