pdecobj.pas 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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. else
  481. ;
  482. end;
  483. end;
  484. procedure parse_parent_classes;
  485. var
  486. intfchildof,
  487. childof : tobjectdef;
  488. hdef : tdef;
  489. hasparentdefined : boolean;
  490. begin
  491. childof:=nil;
  492. intfchildof:=nil;
  493. hasparentdefined:=false;
  494. { reads the parent class }
  495. if (token=_LKLAMMER) or
  496. is_objccategory(current_structdef) then
  497. begin
  498. consume(_LKLAMMER);
  499. { use single_type instead of id_type for specialize support }
  500. single_type(hdef,[stoAllowSpecialization, stoParseClassParent]);
  501. if (not assigned(hdef)) or
  502. (hdef.typ<>objectdef) then
  503. begin
  504. if assigned(hdef) then
  505. Message1(type_e_class_type_expected,hdef.typename)
  506. else if is_objccategory(current_structdef) then
  507. { a category must specify the class to extend }
  508. Message(type_e_objcclass_type_expected);
  509. end
  510. else
  511. begin
  512. childof:=tobjectdef(hdef);
  513. { a mix of class, interfaces, objects and cppclasses
  514. isn't allowed }
  515. case current_objectdef.objecttype of
  516. odt_class,
  517. odt_javaclass:
  518. if (childof.objecttype<>current_objectdef.objecttype) then
  519. begin
  520. if (is_interface(childof) and
  521. is_class(current_objectdef)) or
  522. (is_javainterface(childof) and
  523. is_javaclass(current_objectdef)) then
  524. begin
  525. { we insert the interface after the child
  526. is set, see below
  527. }
  528. intfchildof:=childof;
  529. childof:=class_tobject;
  530. end
  531. else
  532. Message(parser_e_mix_of_classes_and_objects);
  533. end
  534. else
  535. if oo_is_sealed in childof.objectoptions then
  536. Message1(parser_e_sealed_descendant,childof.typename)
  537. else
  538. childof:=find_real_class_definition(childof,true);
  539. odt_interfacecorba,
  540. odt_interfacecom:
  541. begin
  542. if not(is_interface(childof)) then
  543. Message(parser_e_mix_of_classes_and_objects);
  544. current_objectdef.objecttype:=childof.objecttype;
  545. end;
  546. odt_cppclass:
  547. if not(is_cppclass(childof)) then
  548. Message(parser_e_mix_of_classes_and_objects);
  549. odt_objcclass:
  550. if not(is_objcclass(childof) or
  551. is_objccategory(childof)) then
  552. begin
  553. if is_objcprotocol(childof) then
  554. begin
  555. if not(oo_is_classhelper in current_structdef.objectoptions) then
  556. begin
  557. intfchildof:=childof;
  558. childof:=nil;
  559. CGMessage(parser_h_no_objc_parent);
  560. end
  561. else
  562. { a category must specify the class to extend }
  563. CGMessage(type_e_objcclass_type_expected);
  564. end
  565. else
  566. Message(parser_e_mix_of_classes_and_objects);
  567. end
  568. else
  569. childof:=find_real_class_definition(childof,true);
  570. odt_objcprotocol:
  571. begin
  572. if not(is_objcprotocol(childof)) then
  573. Message(parser_e_mix_of_classes_and_objects);
  574. intfchildof:=childof;
  575. childof:=nil;
  576. end;
  577. odt_interfacejava:
  578. begin
  579. if not(is_javainterface(childof)) then
  580. Message(parser_e_mix_of_classes_and_objects);
  581. intfchildof:=find_real_class_definition(childof,true);
  582. childof:=nil;
  583. end;
  584. odt_object:
  585. if not(is_object(childof)) then
  586. Message(parser_e_mix_of_classes_and_objects)
  587. else
  588. if oo_is_sealed in childof.objectoptions then
  589. Message1(parser_e_sealed_descendant,childof.typename);
  590. odt_dispinterface:
  591. Message(parser_e_dispinterface_cant_have_parent);
  592. odt_helper:
  593. if not is_objectpascal_helper(childof) then
  594. begin
  595. Message(type_e_helper_type_expected);
  596. childof:=nil;
  597. end;
  598. else
  599. ;
  600. end;
  601. end;
  602. hasparentdefined:=true;
  603. end;
  604. { if no parent class, then a class get tobject as parent }
  605. if not assigned(childof) then
  606. begin
  607. case current_objectdef.objecttype of
  608. odt_class:
  609. if current_objectdef<>class_tobject then
  610. childof:=class_tobject;
  611. odt_interfacecom:
  612. if current_objectdef<>interface_iunknown then
  613. childof:=interface_iunknown;
  614. odt_dispinterface:
  615. childof:=interface_idispatch;
  616. odt_objcclass:
  617. CGMessage(parser_h_no_objc_parent);
  618. odt_javaclass:
  619. { inherit from TObject by default for compatibility }
  620. if current_objectdef<>java_jlobject then
  621. childof:=class_tobject;
  622. else
  623. ;
  624. end;
  625. end;
  626. if assigned(childof) then
  627. begin
  628. { Forbid not completly defined objects to be used as parents. This will
  629. also prevent circular loops of classes, because we set the forward flag
  630. at the start of the new definition and will reset it below after the
  631. parent has been set }
  632. if (oo_is_forward in childof.objectoptions) then
  633. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  634. else if not(oo_is_formal in childof.objectoptions) then
  635. current_objectdef.set_parent(childof)
  636. else
  637. Message1(sym_e_formal_class_not_resolved,childof.objrealname^);
  638. end;
  639. if hasparentdefined then
  640. begin
  641. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  642. begin
  643. if assigned(intfchildof) then
  644. if current_objectdef.objecttype=odt_class then
  645. handleImplementedInterface(intfchildof)
  646. else
  647. handleImplementedProtocolOrJavaIntf(intfchildof);
  648. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  649. end;
  650. consume(_RKLAMMER);
  651. end;
  652. { remove forward flag, is resolved }
  653. exclude(current_structdef.objectoptions,oo_is_forward);
  654. end;
  655. procedure parse_extended_type(helpertype:thelpertype);
  656. procedure validate_extendeddef_typehelper(var def:tdef);
  657. begin
  658. if (def.typ in [undefineddef,procvardef,procdef,
  659. filedef,classrefdef,abstractdef,forwarddef,formaldef]) or
  660. (
  661. (def.typ=objectdef) and
  662. not (tobjectdef(def).objecttype in objecttypes_with_helpers)
  663. ) then
  664. begin
  665. Message1(type_e_type_not_allowed_for_type_helper,def.typename);
  666. def:=generrordef;
  667. end;
  668. end;
  669. procedure check_inheritance_record_type_helper(var def:tdef);
  670. begin
  671. if (def.typ<>errordef) and assigned(current_objectdef.childof) then
  672. begin
  673. if def<>current_objectdef.childof.extendeddef then
  674. begin
  675. Message1(type_e_record_helper_must_extend_same_record,current_objectdef.childof.extendeddef.typename);
  676. def:=generrordef;
  677. end;
  678. end;
  679. end;
  680. procedure check_inheritance_class_helper(var def:tdef);
  681. begin
  682. if (def.typ<>errordef) and assigned(current_objectdef.childof) then
  683. begin
  684. if (current_objectdef.childof.extendeddef.typ<>objectdef) or
  685. not (tobjectdef(current_objectdef.childof.extendeddef).objecttype in objecttypes_with_helpers) then
  686. Internalerror(2011021101);
  687. if not def_is_related(def,current_objectdef.childof.extendeddef) then
  688. begin
  689. Message1(type_e_class_helper_must_extend_subclass,current_objectdef.childof.extendeddef.typename);
  690. def:=generrordef;
  691. end;
  692. end;
  693. end;
  694. var
  695. hdef: tdef;
  696. begin
  697. if not is_objectpascal_helper(current_structdef) then
  698. Internalerror(2011021103);
  699. consume(_FOR);
  700. single_type(hdef,[stoParseClassParent]);
  701. if not assigned(hdef) or (hdef.typ=errordef) then
  702. begin
  703. case helpertype of
  704. ht_class:
  705. Message1(type_e_class_type_expected,hdef.typename);
  706. ht_record:
  707. Message(type_e_record_type_expected);
  708. ht_type:
  709. Message1(type_e_type_id_expected,hdef.typename);
  710. else
  711. internalerror(2019050532);
  712. end;
  713. end
  714. else
  715. begin
  716. case helpertype of
  717. ht_class:
  718. if (hdef.typ<>objectdef) or
  719. not is_class(hdef) then
  720. Message1(type_e_class_type_expected,hdef.typename)
  721. else
  722. begin
  723. { a class helper must extend the same class or a subclass
  724. of the class extended by the parent class helper }
  725. check_inheritance_class_helper(hdef);
  726. end;
  727. ht_record:
  728. if (hdef.typ=objectdef) or
  729. (
  730. { primitive types are allowed for record helpers in mode
  731. delphi }
  732. (hdef.typ<>recorddef) and
  733. not (m_delphi in current_settings.modeswitches)
  734. ) then
  735. Message1(type_e_record_type_expected,hdef.typename)
  736. else
  737. begin
  738. if hdef.typ<>recorddef then
  739. { this is a primitive type in mode delphi, so validate
  740. the def }
  741. validate_extendeddef_typehelper(hdef);
  742. { a record helper must extend the same record as the
  743. parent helper }
  744. check_inheritance_record_type_helper(hdef);
  745. end;
  746. ht_type:
  747. begin
  748. validate_extendeddef_typehelper(hdef);
  749. if (hdef.typ=objectdef) and
  750. (tobjectdef(hdef).objecttype in objecttypes_with_helpers) then
  751. check_inheritance_class_helper(hdef)
  752. else
  753. { a type helper must extend the same type as the
  754. parent helper }
  755. check_inheritance_record_type_helper(hdef);
  756. end;
  757. else
  758. internalerror(2019050531);
  759. end;
  760. end;
  761. if assigned(hdef) then
  762. current_objectdef.extendeddef:=hdef
  763. else
  764. current_objectdef.extendeddef:=generrordef;
  765. end;
  766. procedure parse_guid;
  767. begin
  768. { read GUID }
  769. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  770. try_to_consume(_LECKKLAMMER) then
  771. begin
  772. readinterfaceiid;
  773. consume(_RECKKLAMMER);
  774. end
  775. else if (current_objectdef.objecttype=odt_dispinterface) then
  776. message(parser_e_dispinterface_needs_a_guid);
  777. end;
  778. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  779. procedure chkobjc(pd: tprocdef);
  780. begin
  781. if is_objc_class_or_protocol(pd.struct) then
  782. begin
  783. include(pd.procoptions,po_objc);
  784. end;
  785. end;
  786. procedure chkjava(pd: tprocdef);
  787. begin
  788. {$ifdef jvm}
  789. if is_java_class_or_interface(pd.struct) then
  790. begin
  791. { mark all non-virtual instance methods as "virtual; final;",
  792. because
  793. a) that's the only way to guarantee "non-virtual" behaviour
  794. (other than making them class methods with an explicit self
  795. pointer, but that causes problems with interface mappings
  796. and procvars)
  797. b) if we don't mark them virtual, they don't get added to the
  798. vmt and we can't check whether child classes try to override
  799. them
  800. }
  801. if is_javaclass(pd.struct) then
  802. begin
  803. if not(po_virtualmethod in pd.procoptions) and
  804. not(po_classmethod in pd.procoptions) then
  805. begin
  806. include(pd.procoptions,po_virtualmethod);
  807. include(pd.procoptions,po_finalmethod);
  808. include(pd.procoptions,po_java_nonvirtual);
  809. end
  810. else if [po_virtualmethod,po_classmethod]<=pd.procoptions then
  811. begin
  812. if po_staticmethod in pd.procoptions then
  813. Message(type_e_java_class_method_not_static_virtual);
  814. end;
  815. end;
  816. end;
  817. {$endif}
  818. end;
  819. procedure chkcpp(pd:tprocdef);
  820. begin
  821. { nothing currently }
  822. end;
  823. var
  824. oldparse_only: boolean;
  825. begin
  826. case token of
  827. _PROCEDURE,
  828. _FUNCTION:
  829. begin
  830. if (astruct.symtable.currentvisibility=vis_published) and
  831. not(oo_can_have_published in astruct.objectoptions) then
  832. Message(parser_e_cant_have_published);
  833. oldparse_only:=parse_only;
  834. parse_only:=true;
  835. result:=parse_proc_dec(is_classdef,astruct,hadgeneric);
  836. { this is for error recovery as well as forward }
  837. { interface mappings, i.e. mapping to a method }
  838. { which isn't declared yet }
  839. if assigned(result) then
  840. begin
  841. parse_object_proc_directives(result);
  842. { check if dispid is set }
  843. if is_dispinterface(result.struct) and not (po_dispid in result.procoptions) then
  844. begin
  845. result.dispid:=tobjectdef(result.struct).get_next_dispid;
  846. include(result.procoptions, po_dispid);
  847. end;
  848. { all Macintosh Object Pascal methods are virtual. }
  849. { this can't be a class method, because macpas mode }
  850. { has no m_class }
  851. if (m_mac in current_settings.modeswitches) then
  852. include(result.procoptions,po_virtualmethod);
  853. { for record and type helpers only static class methods are
  854. allowed }
  855. if is_objectpascal_helper(astruct) and
  856. (
  857. (tobjectdef(astruct).extendeddef.typ<>objectdef) or
  858. (tobjectdef(tobjectdef(astruct).extendeddef).objecttype<>odt_class)
  859. ) and
  860. is_classdef and not (po_staticmethod in result.procoptions) then
  861. MessagePos(result.fileinfo,parser_e_class_methods_only_static_in_records);
  862. handle_calling_convention(result,hcc_default_actions_intf_struct);
  863. { add definition to procsym }
  864. proc_add_definition(result);
  865. { add procdef options to objectdef options }
  866. if (po_msgint in result.procoptions) then
  867. include(astruct.objectoptions,oo_has_msgint);
  868. if (po_msgstr in result.procoptions) then
  869. include(astruct.objectoptions,oo_has_msgstr);
  870. if (po_virtualmethod in result.procoptions) then
  871. include(astruct.objectoptions,oo_has_virtual);
  872. if result.is_generic then
  873. astruct.symtable.includeoption(sto_has_generic);
  874. chkcpp(result);
  875. chkobjc(result);
  876. chkjava(result);
  877. end;
  878. maybe_parse_hint_directives(result);
  879. parse_only:=oldparse_only;
  880. end;
  881. _CONSTRUCTOR :
  882. begin
  883. if (astruct.symtable.currentvisibility=vis_published) and
  884. not(oo_can_have_published in astruct.objectoptions) then
  885. Message(parser_e_cant_have_published);
  886. if not is_classdef and not(astruct.symtable.currentvisibility in [vis_public,vis_published]) then
  887. Message(parser_w_constructor_should_be_public);
  888. if is_interface(astruct) then
  889. Message(parser_e_no_con_des_in_interfaces);
  890. { Objective-C does not know the concept of a constructor }
  891. if is_objc_class_or_protocol(astruct) then
  892. Message(parser_e_objc_no_constructor_destructor);
  893. if is_objectpascal_helper(astruct) then
  894. if is_classdef then
  895. { class constructors are not allowed in class helpers }
  896. Message(parser_e_no_class_constructor_in_helpers);
  897. { only 1 class constructor is allowed }
  898. if is_classdef and (oo_has_class_constructor in astruct.objectoptions) then
  899. Message1(parser_e_only_one_class_constructor_allowed, astruct.objrealname^);
  900. oldparse_only:=parse_only;
  901. parse_only:=true;
  902. if is_classdef then
  903. result:=class_constructor_head(current_structdef)
  904. else
  905. begin
  906. result:=constructor_head;
  907. if is_objectpascal_helper(astruct) and
  908. (tobjectdef(astruct).extendeddef.typ<>objectdef) and
  909. (result.minparacount=0) then
  910. { as long as parameterless constructors aren't allowed in records they
  911. aren't allowed in record/type helpers either }
  912. MessagePos(result.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  913. end;
  914. chkcpp(result);
  915. parse_only:=oldparse_only;
  916. end;
  917. _DESTRUCTOR :
  918. begin
  919. if (astruct.symtable.currentvisibility=vis_published) and
  920. not(oo_can_have_published in astruct.objectoptions) then
  921. Message(parser_e_cant_have_published);
  922. if not is_classdef then
  923. if (oo_has_new_destructor in astruct.objectoptions) then
  924. Message(parser_n_only_one_destructor);
  925. if is_interface(astruct) then
  926. Message(parser_e_no_con_des_in_interfaces);
  927. { (class) destructors are not allowed in class helpers }
  928. if is_objectpascal_helper(astruct) then
  929. Message(parser_e_no_destructor_in_records);
  930. if not is_classdef and (astruct.symtable.currentvisibility<>vis_public) then
  931. Message(parser_w_destructor_should_be_public);
  932. { Objective-C does not know the concept of a destructor }
  933. if is_objc_class_or_protocol(astruct) then
  934. Message(parser_e_objc_no_constructor_destructor);
  935. { only 1 class destructor is allowed }
  936. if is_classdef and (oo_has_class_destructor in astruct.objectoptions) then
  937. Message1(parser_e_only_one_class_destructor_allowed, astruct.objrealname^);
  938. oldparse_only:=parse_only;
  939. parse_only:=true;
  940. if is_classdef then
  941. result:=class_destructor_head(current_structdef)
  942. else
  943. result:=destructor_head;
  944. chkcpp(result);
  945. parse_only:=oldparse_only;
  946. end;
  947. else
  948. internalerror(2011032102);
  949. end;
  950. end;
  951. procedure parse_object_members;
  952. var
  953. typedconstswritable: boolean;
  954. object_member_blocktype : tblock_type;
  955. hadgeneric,
  956. fields_allowed, is_classdef, class_fields, is_final, final_fields,
  957. threadvar_fields : boolean;
  958. vdoptions: tvar_dec_options;
  959. fieldlist: tfpobjectlist;
  960. procedure parse_const;
  961. begin
  962. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  963. Message(parser_e_type_var_const_only_in_records_and_classes);
  964. consume(_CONST);
  965. object_member_blocktype:=bt_const;
  966. final_fields:=is_final;
  967. is_final:=false;
  968. end;
  969. procedure parse_var(isthreadvar:boolean);
  970. begin
  971. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass]) and
  972. { Java interfaces can contain static final class vars }
  973. not((current_objectdef.objecttype=odt_interfacejava) and
  974. is_final and is_classdef) then
  975. Message(parser_e_type_var_const_only_in_records_and_classes);
  976. if isthreadvar then
  977. consume(_THREADVAR)
  978. else
  979. consume(_VAR);
  980. fields_allowed:=true;
  981. object_member_blocktype:=bt_general;
  982. class_fields:=is_classdef;
  983. final_fields:=is_final;
  984. threadvar_fields:=isthreadvar;
  985. is_classdef:=false;
  986. is_final:=false;
  987. end;
  988. procedure parse_class;
  989. begin
  990. is_classdef:=false;
  991. { read class method/field/property }
  992. consume(_CLASS);
  993. { class modifier is only allowed for procedures, functions, }
  994. { constructors, destructors, fields and properties }
  995. if not((token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_DESTRUCTOR,_THREADVAR]) or (token=_CONSTRUCTOR)) then
  996. Message(parser_e_procedure_or_function_expected);
  997. { Java interfaces can contain final class vars }
  998. if is_interface(current_structdef) or
  999. (is_javainterface(current_structdef) and
  1000. (not(is_final) or
  1001. (token<>_VAR))) then
  1002. Message(parser_e_no_static_method_in_interfaces)
  1003. else
  1004. { class methods are also allowed for Objective-C protocols }
  1005. is_classdef:=true;
  1006. end;
  1007. procedure parse_visibility(vis: tvisibility; oo: tobjectoption);
  1008. begin
  1009. { Objective-C and Java classes do not support "published",
  1010. as basically everything is published. }
  1011. if (vis=vis_published) and
  1012. (is_objc_class_or_protocol(current_structdef) or
  1013. is_java_class_or_interface(current_structdef)) then
  1014. Message(parser_e_no_objc_published)
  1015. else if is_interface(current_structdef) or
  1016. is_objc_protocol_or_category(current_structdef) or
  1017. is_javainterface(current_structdef) then
  1018. Message(parser_e_no_access_specifier_in_interfaces);
  1019. current_structdef.symtable.currentvisibility:=vis;
  1020. consume(token);
  1021. if (oo<>oo_none) then
  1022. include(current_structdef.objectoptions,oo);
  1023. fields_allowed:=true;
  1024. is_classdef:=false;
  1025. class_fields:=false;
  1026. threadvar_fields:=false;
  1027. is_final:=false;
  1028. object_member_blocktype:=bt_general;
  1029. end;
  1030. begin
  1031. { empty class declaration ? }
  1032. if (current_objectdef.objecttype in [odt_class,odt_objcclass,odt_javaclass]) and
  1033. (token=_SEMICOLON) then
  1034. exit;
  1035. { in "publishable" classes the default access type is published }
  1036. if (oo_can_have_published in current_structdef.objectoptions) then
  1037. current_structdef.symtable.currentvisibility:=vis_published
  1038. else
  1039. current_structdef.symtable.currentvisibility:=vis_public;
  1040. fields_allowed:=true;
  1041. is_classdef:=false;
  1042. class_fields:=false;
  1043. is_final:=false;
  1044. final_fields:=false;
  1045. hadgeneric:=false;
  1046. threadvar_fields:=false;
  1047. object_member_blocktype:=bt_general;
  1048. fieldlist:=tfpobjectlist.create(false);
  1049. repeat
  1050. case token of
  1051. _TYPE :
  1052. begin
  1053. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  1054. Message(parser_e_type_var_const_only_in_records_and_classes);
  1055. consume(_TYPE);
  1056. object_member_blocktype:=bt_type;
  1057. end;
  1058. _VAR :
  1059. begin
  1060. parse_var(false);
  1061. end;
  1062. _CONST:
  1063. begin
  1064. parse_const
  1065. end;
  1066. _THREADVAR :
  1067. begin
  1068. if not is_classdef then
  1069. begin
  1070. Message(parser_e_threadvar_must_be_class);
  1071. { for error recovery we enforce class fields }
  1072. is_classdef:=true;
  1073. end;
  1074. parse_var(true);
  1075. end;
  1076. _ID :
  1077. begin
  1078. if is_objcprotocol(current_structdef) and
  1079. ((idtoken=_REQUIRED) or
  1080. (idtoken=_OPTIONAL)) then
  1081. begin
  1082. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  1083. consume(idtoken)
  1084. end
  1085. else case idtoken of
  1086. _PRIVATE :
  1087. begin
  1088. parse_visibility(vis_private,oo_has_private);
  1089. end;
  1090. _PROTECTED :
  1091. begin
  1092. parse_visibility(vis_protected,oo_has_protected);
  1093. end;
  1094. _PUBLIC :
  1095. begin
  1096. parse_visibility(vis_public,oo_none);
  1097. end;
  1098. _PUBLISHED :
  1099. begin
  1100. parse_visibility(vis_published,oo_none);
  1101. end;
  1102. _STRICT :
  1103. begin
  1104. if is_interface(current_structdef) or
  1105. is_objc_protocol_or_category(current_structdef) or
  1106. is_javainterface(current_structdef) then
  1107. Message(parser_e_no_access_specifier_in_interfaces);
  1108. consume(_STRICT);
  1109. if token=_ID then
  1110. begin
  1111. case idtoken of
  1112. _PRIVATE:
  1113. begin
  1114. consume(_PRIVATE);
  1115. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  1116. include(current_structdef.objectoptions,oo_has_strictprivate);
  1117. end;
  1118. _PROTECTED:
  1119. begin
  1120. consume(_PROTECTED);
  1121. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  1122. include(current_structdef.objectoptions,oo_has_strictprotected);
  1123. end;
  1124. else
  1125. message(parser_e_protected_or_private_expected);
  1126. end;
  1127. end
  1128. else
  1129. message(parser_e_protected_or_private_expected);
  1130. fields_allowed:=true;
  1131. is_classdef:=false;
  1132. class_fields:=false;
  1133. threadvar_fields:=false;
  1134. is_final:=false;
  1135. final_fields:=false;
  1136. object_member_blocktype:=bt_general;
  1137. end
  1138. else if (m_final_fields in current_settings.modeswitches) and
  1139. (token=_ID) and
  1140. (idtoken=_FINAL) then
  1141. begin
  1142. { currently only supported for external classes, because
  1143. requires fully working DFA otherwise }
  1144. if (current_structdef.typ<>objectdef) or
  1145. not(oo_is_external in tobjectdef(current_structdef).objectoptions) then
  1146. Message(parser_e_final_only_external);
  1147. consume(_final);
  1148. is_final:=true;
  1149. if token=_CLASS then
  1150. parse_class;
  1151. if not(token in [_CONST,_VAR]) then
  1152. message(parser_e_final_only_const_var);
  1153. end
  1154. else
  1155. begin
  1156. if object_member_blocktype=bt_general then
  1157. begin
  1158. if (idtoken=_GENERIC) and
  1159. not (m_delphi in current_settings.modeswitches) and
  1160. not fields_allowed then
  1161. begin
  1162. if hadgeneric then
  1163. Message(parser_e_procedure_or_function_expected);
  1164. consume(_ID);
  1165. hadgeneric:=true;
  1166. if not (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  1167. Message(parser_e_procedure_or_function_expected);
  1168. end
  1169. else
  1170. begin
  1171. if is_interface(current_structdef) or
  1172. is_objc_protocol_or_category(current_structdef) or
  1173. (
  1174. is_objectpascal_helper(current_structdef) and
  1175. not class_fields
  1176. ) or
  1177. (is_javainterface(current_structdef) and
  1178. not(class_fields and final_fields)) then
  1179. Message(parser_e_no_vars_in_interfaces);
  1180. if (current_structdef.symtable.currentvisibility=vis_published) and
  1181. not(oo_can_have_published in current_structdef.objectoptions) then
  1182. Message(parser_e_cant_have_published);
  1183. if (not fields_allowed) then
  1184. Message(parser_e_field_not_allowed_here);
  1185. vdoptions:=[vd_object];
  1186. if not (m_delphi in current_settings.modeswitches) then
  1187. include(vdoptions,vd_check_generic);
  1188. if class_fields then
  1189. include(vdoptions,vd_class);
  1190. if is_class(current_structdef) then
  1191. include(vdoptions,vd_canreorder);
  1192. if final_fields then
  1193. include(vdoptions,vd_final);
  1194. if threadvar_fields then
  1195. include(vdoptions,vd_threadvar);
  1196. read_record_fields(vdoptions,fieldlist,nil,hadgeneric);
  1197. end;
  1198. end
  1199. else if object_member_blocktype=bt_type then
  1200. types_dec(true,hadgeneric)
  1201. else if object_member_blocktype=bt_const then
  1202. begin
  1203. typedconstswritable:=false;
  1204. if final_fields then
  1205. begin
  1206. { the value of final fields cannot be changed
  1207. once they've been assigned a value }
  1208. typedconstswritable:=cs_typed_const_writable in current_settings.localswitches;
  1209. exclude(current_settings.localswitches,cs_typed_const_writable);
  1210. end;
  1211. consts_dec(true,not is_javainterface(current_structdef),hadgeneric);
  1212. if final_fields and
  1213. typedconstswritable then
  1214. include(current_settings.localswitches,cs_typed_const_writable);
  1215. end
  1216. else
  1217. internalerror(201001110);
  1218. end;
  1219. end;
  1220. end;
  1221. _PROPERTY :
  1222. begin
  1223. struct_property_dec(is_classdef);
  1224. fields_allowed:=false;
  1225. is_classdef:=false;
  1226. end;
  1227. _CLASS:
  1228. begin
  1229. parse_class;
  1230. end;
  1231. _PROCEDURE,
  1232. _FUNCTION,
  1233. _CONSTRUCTOR,
  1234. _DESTRUCTOR :
  1235. begin
  1236. method_dec(current_structdef,is_classdef,hadgeneric);
  1237. fields_allowed:=false;
  1238. is_classdef:=false;
  1239. hadgeneric:=false;
  1240. end;
  1241. _END :
  1242. begin
  1243. consume(_END);
  1244. break;
  1245. end;
  1246. else
  1247. consume(_ID); { Give a ident expected message, like tp7 }
  1248. end;
  1249. until false;
  1250. if is_class(current_structdef) then
  1251. tabstractrecordsymtable(current_structdef.symtable).addfieldlist(fieldlist,true);
  1252. fieldlist.free;
  1253. end;
  1254. function object_dec(objecttype:tobjecttyp;const n:tidstring;objsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  1255. var
  1256. old_current_structdef: tabstractrecorddef;
  1257. old_current_genericdef,
  1258. old_current_specializedef: tstoreddef;
  1259. old_parse_generic: boolean;
  1260. list: TFPObjectList;
  1261. s: String;
  1262. st: TSymtable;
  1263. olddef: tdef;
  1264. begin
  1265. old_current_structdef:=current_structdef;
  1266. old_current_genericdef:=current_genericdef;
  1267. old_current_specializedef:=current_specializedef;
  1268. old_parse_generic:=parse_generic;
  1269. current_structdef:=nil;
  1270. current_genericdef:=nil;
  1271. current_specializedef:=nil;
  1272. { objects and class types can't be declared local }
  1273. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1274. not assigned(genericlist) then
  1275. Message(parser_e_no_local_objects);
  1276. { reuse forward objectdef? }
  1277. if assigned(fd) then
  1278. begin
  1279. if fd.objecttype<>objecttype then
  1280. begin
  1281. Message(parser_e_forward_mismatch);
  1282. { recover }
  1283. current_structdef:=cobjectdef.create(objecttype,n,nil,true);
  1284. include(current_structdef.objectoptions,oo_is_forward);
  1285. end
  1286. else
  1287. current_structdef:=fd
  1288. end
  1289. else
  1290. begin
  1291. { anonym objects aren't allow (o : object a : longint; end;) }
  1292. if n='' then
  1293. Message(parser_f_no_anonym_objects);
  1294. { create new class }
  1295. current_structdef:=cobjectdef.create(objecttype,n,nil,true);
  1296. tobjectdef(current_structdef).helpertype:=helpertype;
  1297. { include always the forward flag, it'll be removed after the parent class have been
  1298. added. This is to prevent circular childof loops }
  1299. include(current_structdef.objectoptions,oo_is_forward);
  1300. if (cs_compilesystem in current_settings.moduleswitches) then
  1301. begin
  1302. case current_objectdef.objecttype of
  1303. odt_interfacecom :
  1304. if (current_structdef.objname^='IUNKNOWN') then
  1305. interface_iunknown:=current_objectdef
  1306. else
  1307. if (current_structdef.objname^='IDISPATCH') then
  1308. interface_idispatch:=current_objectdef;
  1309. odt_class :
  1310. if (current_structdef.objname^='TOBJECT') then
  1311. class_tobject:=current_objectdef;
  1312. odt_javaclass:
  1313. begin
  1314. if (current_structdef.objname^='TOBJECT') then
  1315. class_tobject:=current_objectdef
  1316. else if (current_objectdef.objname^='JLOBJECT') then
  1317. java_jlobject:=current_objectdef
  1318. else if (current_objectdef.objname^='JLTHROWABLE') then
  1319. java_jlthrowable:=current_objectdef
  1320. else if (current_objectdef.objname^='FPCBASERECORDTYPE') then
  1321. java_fpcbaserecordtype:=current_objectdef
  1322. else if (current_objectdef.objname^='JLSTRING') then
  1323. java_jlstring:=current_objectdef
  1324. else if (current_objectdef.objname^='ANSISTRINGCLASS') then
  1325. java_ansistring:=current_objectdef
  1326. else if (current_objectdef.objname^='SHORTSTRINGCLASS') then
  1327. java_shortstring:=current_objectdef
  1328. else if (current_objectdef.objname^='JLENUM') then
  1329. java_jlenum:=current_objectdef
  1330. else if (current_objectdef.objname^='JUENUMSET') then
  1331. java_juenumset:=current_objectdef
  1332. else if (current_objectdef.objname^='FPCBITSET') then
  1333. java_jubitset:=current_objectdef
  1334. else if (current_objectdef.objname^='FPCBASEPROCVARTYPE') then
  1335. java_procvarbase:=current_objectdef;
  1336. end;
  1337. else
  1338. ;
  1339. end;
  1340. end;
  1341. if (current_module.modulename^='OBJCBASE') then
  1342. begin
  1343. case current_objectdef.objecttype of
  1344. odt_objcclass:
  1345. if (current_objectdef.objname^='Protocol') then
  1346. objc_protocoltype:=current_objectdef;
  1347. else
  1348. ;
  1349. end;
  1350. end;
  1351. end;
  1352. { usage of specialized type inside its generic template }
  1353. if assigned(genericdef) then
  1354. current_specializedef:=current_structdef;
  1355. { reject declaration of generic class inside generic class }
  1356. if assigned(genericlist) then
  1357. current_genericdef:=current_structdef;
  1358. { nested types of specializations are specializations as well }
  1359. if assigned(old_current_structdef) and
  1360. (df_specialization in old_current_structdef.defoptions) then
  1361. include(current_structdef.defoptions,df_specialization);
  1362. if assigned(old_current_structdef) and
  1363. (df_generic in old_current_structdef.defoptions) then
  1364. begin
  1365. include(current_structdef.defoptions,df_generic);
  1366. current_genericdef:=current_structdef;
  1367. end;
  1368. { set published flag in $M+ mode, it can also be inherited and will
  1369. be added when the parent class set with tobjectdef.set_parent (PFV) }
  1370. if (cs_generate_rtti in current_settings.localswitches) and
  1371. (current_objectdef.objecttype in [odt_interfacecom,odt_class,odt_helper]) then
  1372. include(current_structdef.objectoptions,oo_can_have_published);
  1373. { Objective-C/Java objectdefs can be "formal definitions", in which case
  1374. the syntax is "type tc = objcclass external;" -> we have to parse
  1375. its object options (external) already here, to make sure that such
  1376. definitions are recognised as formal defs }
  1377. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  1378. parse_object_options;
  1379. { forward def? }
  1380. if not assigned(fd) and
  1381. (token=_SEMICOLON) then
  1382. begin
  1383. if is_objectpascal_helper(current_structdef) then
  1384. consume(_FOR);
  1385. { add to the list of definitions to check that the forward
  1386. is resolved. this is required for delphi mode }
  1387. current_module.checkforwarddefs.add(current_structdef);
  1388. end
  1389. else
  1390. begin
  1391. { change objccategories into objcclass helpers }
  1392. if (objecttype=odt_objccategory) then
  1393. begin
  1394. current_objectdef.objecttype:=odt_objcclass;
  1395. include(current_structdef.objectoptions,oo_is_classhelper);
  1396. end;
  1397. { include the class helper flag for Object Pascal helpers }
  1398. if (objecttype=odt_helper) then
  1399. include(current_objectdef.objectoptions,oo_is_classhelper);
  1400. { parse list of options (abstract / sealed) }
  1401. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava]) then
  1402. parse_object_options;
  1403. symtablestack.push(current_structdef.symtable);
  1404. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  1405. { when we are parsing a generic already then this is a generic as
  1406. well }
  1407. if old_parse_generic then
  1408. include(current_structdef.defoptions, df_generic);
  1409. parse_generic:=(df_generic in current_structdef.defoptions);
  1410. { in non-Delphi modes we need a strict private symbol without type
  1411. count and type parameters in the name to simply resolving }
  1412. maybe_insert_generic_rename_symbol(n,genericlist);
  1413. { parse list of parent classes }
  1414. { for record helpers in mode Delphi this is not allowed }
  1415. if not (is_objectpascal_helper(current_objectdef) and
  1416. (m_delphi in current_settings.modeswitches) and
  1417. (helpertype=ht_record)) then
  1418. parse_parent_classes
  1419. else
  1420. { remove forward flag, is resolved (this is normally done inside
  1421. parse_parent_classes) }
  1422. exclude(current_structdef.objectoptions,oo_is_forward);
  1423. { parse extended type for helpers }
  1424. if is_objectpascal_helper(current_structdef) then
  1425. parse_extended_type(helpertype);
  1426. { parse optional GUID for interfaces }
  1427. parse_guid;
  1428. { classes can handle links to themself not only inside type blocks
  1429. but in const blocks too. Additionally this is needed to parse parameters that are
  1430. specializations of the currently parsed type in basically everything except C++ and
  1431. ObjC classes. To make this possible we need to set their symbols to real defs instead
  1432. of errordef }
  1433. if assigned(objsym) and not (objecttype in [odt_cppclass,odt_objccategory,odt_objcclass,odt_objcprotocol]) then
  1434. begin
  1435. olddef:=ttypesym(objsym).typedef;
  1436. ttypesym(objsym).typedef:=current_structdef;
  1437. current_structdef.typesym:=objsym;
  1438. end
  1439. else
  1440. olddef:=nil;
  1441. { parse and insert object members }
  1442. parse_object_members;
  1443. if assigned(olddef) then
  1444. begin
  1445. ttypesym(objsym).typedef:=olddef;
  1446. current_structdef.typesym:=nil;
  1447. end;
  1448. if not(oo_is_external in current_structdef.objectoptions) then
  1449. begin
  1450. { In Java, constructors are not automatically inherited (so you can
  1451. hide them). Emulate the Pascal behaviour for classes implemented
  1452. in Pascal (we cannot do it for classes implemented in Java, since
  1453. we obviously cannot add constructors to those) }
  1454. if is_javaclass(current_structdef) then
  1455. begin
  1456. add_missing_parent_constructors_intf(tobjectdef(current_structdef),true,vis_none);
  1457. {$ifdef jvm}
  1458. maybe_add_public_default_java_constructor(tobjectdef(current_structdef));
  1459. jvm_wrap_virtual_class_methods(tobjectdef(current_structdef));
  1460. {$endif}
  1461. end;
  1462. { need method to hold the initialization code for typed constants? }
  1463. if (target_info.system in systems_typed_constants_node_init) and
  1464. not is_any_interface_kind(current_structdef) then
  1465. add_typedconst_init_routine(current_structdef);
  1466. end;
  1467. symtablestack.pop(current_structdef.symtable);
  1468. end;
  1469. { generate vmt space if needed }
  1470. if not(oo_has_vmt in current_structdef.objectoptions) and
  1471. not(oo_is_forward in current_structdef.objectoptions) and
  1472. not(parse_generic) and
  1473. { no vmt for helpers ever }
  1474. not is_objectpascal_helper(current_structdef) and
  1475. (
  1476. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1477. (current_objectdef.objecttype in [odt_class])
  1478. ) then
  1479. current_objectdef.insertvmt;
  1480. { for implemented classes with a vmt check if there is a constructor }
  1481. if (oo_has_vmt in current_structdef.objectoptions) and
  1482. not(oo_is_forward in current_structdef.objectoptions) and
  1483. not(oo_has_constructor in current_structdef.objectoptions) and
  1484. not is_objc_class_or_protocol(current_structdef) and
  1485. not is_java_class_or_interface(current_structdef) then
  1486. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1487. if is_interface(current_structdef) or
  1488. is_objcprotocol(current_structdef) or
  1489. is_javainterface(current_structdef) then
  1490. setinterfacemethodoptions
  1491. else if is_objcclass(current_structdef) then
  1492. setobjcclassmethodoptions;
  1493. { we need to add this helper to the extendeddefs of the current module,
  1494. as the global and static symtable are not pushed onto the symtable
  1495. stack again (it will be removed when poping the symtable) }
  1496. if is_objectpascal_helper(current_structdef) and
  1497. (current_objectdef.extendeddef.typ<>errordef) then
  1498. begin
  1499. { the topmost symtable must be a static symtable }
  1500. st:=current_structdef.owner;
  1501. while st.symtabletype in [objectsymtable,recordsymtable] do
  1502. st:=st.defowner.owner;
  1503. if st.symtabletype in [staticsymtable,globalsymtable] then
  1504. begin
  1505. if current_objectdef.extendeddef.typ in [recorddef,objectdef] then
  1506. s:=make_mangledname('',tabstractrecorddef(current_objectdef.extendeddef).symtable,'')
  1507. else
  1508. s:=make_mangledname('',current_objectdef.extendeddef.owner,current_objectdef.extendeddef.typesym.name);
  1509. Message1(sym_d_adding_helper_for,s);
  1510. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1511. if not assigned(list) then
  1512. begin
  1513. list:=TFPObjectList.Create(false);
  1514. current_module.extendeddefs.Add(s, list);
  1515. end;
  1516. list.add(current_structdef);
  1517. end;
  1518. end;
  1519. tabstractrecordsymtable(current_objectdef.symtable).addalignmentpadding;
  1520. { return defined objectdef }
  1521. result:=current_objectdef;
  1522. { restore old state }
  1523. current_structdef:=old_current_structdef;
  1524. current_genericdef:=old_current_genericdef;
  1525. current_specializedef:=old_current_specializedef;
  1526. parse_generic:=old_parse_generic;
  1527. end;
  1528. end.