pdecobj.pas 68 KB

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