pdecobj.pas 62 KB

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