pdecobj.pas 66 KB

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