pdecobj.pas 69 KB

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