pdecobj.pas 62 KB

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