pdecobj.pas 69 KB

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