pdecobj.pas 60 KB

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