pdecobj.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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. procedure validate_extendeddef_typehelper(var def:tdef);
  645. begin
  646. if def.typ in [undefineddef,procvardef,procdef,objectdef,recorddef,
  647. filedef,classrefdef,abstractdef,forwarddef,formaldef] then
  648. begin
  649. Message1(type_e_type_not_allowed_for_type_helper,def.typename);
  650. def:=generrordef;
  651. end;
  652. end;
  653. procedure check_inheritance_record_type_helper(var def:tdef);
  654. begin
  655. if (def.typ<>errordef) and assigned(current_objectdef.childof) then
  656. begin
  657. if def<>current_objectdef.childof.extendeddef then
  658. begin
  659. Message1(type_e_record_helper_must_extend_same_record,current_objectdef.childof.extendeddef.typename);
  660. def:=generrordef;
  661. end;
  662. end;
  663. end;
  664. var
  665. hdef: tdef;
  666. begin
  667. if not is_objectpascal_helper(current_structdef) then
  668. Internalerror(2011021103);
  669. if helpertype=ht_none then
  670. Internalerror(2011021001);
  671. consume(_FOR);
  672. single_type(hdef,[stoParseClassParent]);
  673. if not assigned(hdef) or (hdef.typ=errordef) then
  674. begin
  675. case helpertype of
  676. ht_class:
  677. Message1(type_e_class_type_expected,hdef.typename);
  678. ht_record:
  679. Message(type_e_record_type_expected);
  680. ht_type:
  681. Message1(type_e_type_id_expected,hdef.typename);
  682. end;
  683. end
  684. else
  685. begin
  686. case helpertype of
  687. ht_class:
  688. if (hdef.typ<>objectdef) or
  689. not is_class(hdef) then
  690. Message1(type_e_class_type_expected,hdef.typename)
  691. else
  692. begin
  693. { a class helper must extend the same class or a subclass
  694. of the class extended by the parent class helper }
  695. if assigned(current_objectdef.childof) then
  696. begin
  697. if not is_class(current_objectdef.childof.extendeddef) then
  698. Internalerror(2011021101);
  699. if not hdef.is_related(current_objectdef.childof.extendeddef) then
  700. Message1(type_e_class_helper_must_extend_subclass,current_objectdef.childof.extendeddef.typename);
  701. end;
  702. end;
  703. ht_record:
  704. if (hdef.typ=objectdef) or
  705. (
  706. { primitive types are allowed for record helpers in mode
  707. delphi }
  708. (hdef.typ<>recorddef) and
  709. not (m_delphi in current_settings.modeswitches)
  710. ) then
  711. Message1(type_e_record_type_expected,hdef.typename)
  712. else
  713. begin
  714. if hdef.typ<>recorddef then
  715. { this is a primitive type in mode delphi, so validate
  716. the def }
  717. validate_extendeddef_typehelper(hdef);
  718. { a record helper must extend the same record as the
  719. parent helper }
  720. check_inheritance_record_type_helper(hdef);
  721. end;
  722. ht_type:
  723. begin
  724. validate_extendeddef_typehelper(hdef);
  725. { a type helper must extend the same type as the
  726. parent helper }
  727. check_inheritance_record_type_helper(hdef);
  728. end;
  729. end;
  730. end;
  731. if assigned(hdef) then
  732. current_objectdef.extendeddef:=hdef
  733. else
  734. current_objectdef.extendeddef:=generrordef;
  735. end;
  736. procedure parse_guid;
  737. begin
  738. { read GUID }
  739. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  740. try_to_consume(_LECKKLAMMER) then
  741. begin
  742. readinterfaceiid;
  743. consume(_RECKKLAMMER);
  744. end
  745. else if (current_objectdef.objecttype=odt_dispinterface) then
  746. message(parser_e_dispinterface_needs_a_guid);
  747. end;
  748. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  749. procedure chkobjc(pd: tprocdef);
  750. begin
  751. if is_objc_class_or_protocol(pd.struct) then
  752. begin
  753. include(pd.procoptions,po_objc);
  754. end;
  755. end;
  756. procedure chkjava(pd: tprocdef);
  757. begin
  758. {$ifdef jvm}
  759. if is_java_class_or_interface(pd.struct) then
  760. begin
  761. { mark all non-virtual instance methods as "virtual; final;",
  762. because
  763. a) that's the only way to guarantee "non-virtual" behaviour
  764. (other than making them class methods with an explicit self
  765. pointer, but that causes problems with interface mappings
  766. and procvars)
  767. b) if we don't mark them virtual, they don't get added to the
  768. vmt and we can't check whether child classes try to override
  769. them
  770. }
  771. if is_javaclass(pd.struct) then
  772. begin
  773. if not(po_virtualmethod in pd.procoptions) and
  774. not(po_classmethod in pd.procoptions) then
  775. begin
  776. include(pd.procoptions,po_virtualmethod);
  777. include(pd.procoptions,po_finalmethod);
  778. include(pd.procoptions,po_java_nonvirtual);
  779. end
  780. else if [po_virtualmethod,po_classmethod]<=pd.procoptions then
  781. begin
  782. if po_staticmethod in pd.procoptions then
  783. Message(type_e_java_class_method_not_static_virtual);
  784. end;
  785. end;
  786. end;
  787. {$endif}
  788. end;
  789. procedure chkcpp(pd:tprocdef);
  790. begin
  791. { nothing currently }
  792. end;
  793. var
  794. oldparse_only: boolean;
  795. begin
  796. case token of
  797. _PROCEDURE,
  798. _FUNCTION:
  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. oldparse_only:=parse_only;
  804. parse_only:=true;
  805. result:=parse_proc_dec(is_classdef,astruct);
  806. { this is for error recovery as well as forward }
  807. { interface mappings, i.e. mapping to a method }
  808. { which isn't declared yet }
  809. if assigned(result) then
  810. begin
  811. parse_object_proc_directives(result);
  812. { check if dispid is set }
  813. if is_dispinterface(result.struct) and not (po_dispid in result.procoptions) then
  814. begin
  815. result.dispid:=tobjectdef(result.struct).get_next_dispid;
  816. include(result.procoptions, po_dispid);
  817. end;
  818. { all Macintosh Object Pascal methods are virtual. }
  819. { this can't be a class method, because macpas mode }
  820. { has no m_class }
  821. if (m_mac in current_settings.modeswitches) then
  822. include(result.procoptions,po_virtualmethod);
  823. { for record and type helpers only static class methods are
  824. allowed }
  825. if is_objectpascal_helper(astruct) and
  826. (tobjectdef(astruct).extendeddef.typ<>objectdef) and
  827. is_classdef and not (po_staticmethod in result.procoptions) then
  828. MessagePos(result.fileinfo,parser_e_class_methods_only_static_in_records);
  829. handle_calling_convention(result);
  830. { add definition to procsym }
  831. proc_add_definition(result);
  832. { add procdef options to objectdef options }
  833. if (po_msgint in result.procoptions) then
  834. include(astruct.objectoptions,oo_has_msgint);
  835. if (po_msgstr in result.procoptions) then
  836. include(astruct.objectoptions,oo_has_msgstr);
  837. if (po_virtualmethod in result.procoptions) then
  838. include(astruct.objectoptions,oo_has_virtual);
  839. chkcpp(result);
  840. chkobjc(result);
  841. chkjava(result);
  842. end;
  843. maybe_parse_hint_directives(result);
  844. parse_only:=oldparse_only;
  845. end;
  846. _CONSTRUCTOR :
  847. begin
  848. if (astruct.symtable.currentvisibility=vis_published) and
  849. not(oo_can_have_published in astruct.objectoptions) then
  850. Message(parser_e_cant_have_published);
  851. if not is_classdef and not(astruct.symtable.currentvisibility in [vis_public,vis_published]) then
  852. Message(parser_w_constructor_should_be_public);
  853. if is_interface(astruct) then
  854. Message(parser_e_no_con_des_in_interfaces);
  855. { Objective-C does not know the concept of a constructor }
  856. if is_objc_class_or_protocol(astruct) then
  857. Message(parser_e_objc_no_constructor_destructor);
  858. if is_objectpascal_helper(astruct) then
  859. if is_classdef then
  860. { class constructors are not allowed in class helpers }
  861. Message(parser_e_no_class_constructor_in_helpers);
  862. { only 1 class constructor is allowed }
  863. if is_classdef and (oo_has_class_constructor in astruct.objectoptions) then
  864. Message1(parser_e_only_one_class_constructor_allowed, astruct.objrealname^);
  865. oldparse_only:=parse_only;
  866. parse_only:=true;
  867. if is_classdef then
  868. result:=class_constructor_head(current_structdef)
  869. else
  870. begin
  871. result:=constructor_head;
  872. if is_objectpascal_helper(astruct) and
  873. (tobjectdef(astruct).extendeddef.typ<>objectdef) and
  874. (result.minparacount=0) then
  875. { as long as parameterless constructors aren't allowed in records they
  876. aren't allowed in record/type helpers either }
  877. MessagePos(result.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  878. end;
  879. chkcpp(result);
  880. parse_only:=oldparse_only;
  881. end;
  882. _DESTRUCTOR :
  883. begin
  884. if (astruct.symtable.currentvisibility=vis_published) and
  885. not(oo_can_have_published in astruct.objectoptions) then
  886. Message(parser_e_cant_have_published);
  887. if not is_classdef then
  888. if (oo_has_new_destructor in astruct.objectoptions) then
  889. Message(parser_n_only_one_destructor);
  890. if is_interface(astruct) then
  891. Message(parser_e_no_con_des_in_interfaces);
  892. { (class) destructors are not allowed in class helpers }
  893. if is_objectpascal_helper(astruct) then
  894. Message(parser_e_no_destructor_in_records);
  895. if not is_classdef and (astruct.symtable.currentvisibility<>vis_public) then
  896. Message(parser_w_destructor_should_be_public);
  897. { Objective-C does not know the concept of a destructor }
  898. if is_objc_class_or_protocol(astruct) then
  899. Message(parser_e_objc_no_constructor_destructor);
  900. { only 1 class destructor is allowed }
  901. if is_classdef and (oo_has_class_destructor in astruct.objectoptions) then
  902. Message1(parser_e_only_one_class_destructor_allowed, astruct.objrealname^);
  903. oldparse_only:=parse_only;
  904. parse_only:=true;
  905. if is_classdef then
  906. result:=class_destructor_head(current_structdef)
  907. else
  908. result:=destructor_head;
  909. chkcpp(result);
  910. parse_only:=oldparse_only;
  911. end;
  912. else
  913. internalerror(2011032102);
  914. end;
  915. end;
  916. procedure parse_object_members;
  917. var
  918. typedconstswritable: boolean;
  919. object_member_blocktype : tblock_type;
  920. fields_allowed, is_classdef, class_fields, is_final, final_fields: boolean;
  921. vdoptions: tvar_dec_options;
  922. fieldlist: tfpobjectlist;
  923. procedure parse_const;
  924. begin
  925. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  926. Message(parser_e_type_var_const_only_in_records_and_classes);
  927. consume(_CONST);
  928. object_member_blocktype:=bt_const;
  929. final_fields:=is_final;
  930. is_final:=false;
  931. end;
  932. procedure parse_var;
  933. begin
  934. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass]) and
  935. { Java interfaces can contain static final class vars }
  936. not((current_objectdef.objecttype=odt_interfacejava) and
  937. is_final and is_classdef) then
  938. Message(parser_e_type_var_const_only_in_records_and_classes);
  939. consume(_VAR);
  940. fields_allowed:=true;
  941. object_member_blocktype:=bt_general;
  942. class_fields:=is_classdef;
  943. final_fields:=is_final;
  944. is_classdef:=false;
  945. is_final:=false;
  946. end;
  947. procedure parse_class;
  948. begin
  949. is_classdef:=false;
  950. { read class method/field/property }
  951. consume(_CLASS);
  952. { class modifier is only allowed for procedures, functions, }
  953. { constructors, destructors, fields and properties }
  954. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  955. Message(parser_e_procedure_or_function_expected);
  956. { Java interfaces can contain final class vars }
  957. if is_interface(current_structdef) or
  958. (is_javainterface(current_structdef) and
  959. (not(is_final) or
  960. (token<>_VAR))) then
  961. Message(parser_e_no_static_method_in_interfaces)
  962. else
  963. { class methods are also allowed for Objective-C protocols }
  964. is_classdef:=true;
  965. end;
  966. procedure parse_visibility(vis: tvisibility; oo: tobjectoption);
  967. begin
  968. { Objective-C and Java classes do not support "published",
  969. as basically everything is published. }
  970. if (vis=vis_published) and
  971. (is_objc_class_or_protocol(current_structdef) or
  972. is_java_class_or_interface(current_structdef)) then
  973. Message(parser_e_no_objc_published)
  974. else if is_interface(current_structdef) or
  975. is_objc_protocol_or_category(current_structdef) or
  976. is_javainterface(current_structdef) then
  977. Message(parser_e_no_access_specifier_in_interfaces);
  978. current_structdef.symtable.currentvisibility:=vis;
  979. consume(token);
  980. if (oo<>oo_none) then
  981. include(current_structdef.objectoptions,oo);
  982. fields_allowed:=true;
  983. is_classdef:=false;
  984. class_fields:=false;
  985. is_final:=false;
  986. object_member_blocktype:=bt_general;
  987. end;
  988. begin
  989. { empty class declaration ? }
  990. if (current_objectdef.objecttype in [odt_class,odt_objcclass,odt_javaclass]) and
  991. (token=_SEMICOLON) then
  992. exit;
  993. { in "publishable" classes the default access type is published }
  994. if (oo_can_have_published in current_structdef.objectoptions) then
  995. current_structdef.symtable.currentvisibility:=vis_published
  996. else
  997. current_structdef.symtable.currentvisibility:=vis_public;
  998. fields_allowed:=true;
  999. is_classdef:=false;
  1000. class_fields:=false;
  1001. is_final:=false;
  1002. final_fields:=false;
  1003. object_member_blocktype:=bt_general;
  1004. fieldlist:=tfpobjectlist.create(false);
  1005. repeat
  1006. case token of
  1007. _TYPE :
  1008. begin
  1009. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  1010. Message(parser_e_type_var_const_only_in_records_and_classes);
  1011. consume(_TYPE);
  1012. object_member_blocktype:=bt_type;
  1013. end;
  1014. _VAR :
  1015. begin
  1016. parse_var;
  1017. end;
  1018. _CONST:
  1019. begin
  1020. parse_const
  1021. end;
  1022. _ID :
  1023. begin
  1024. if is_objcprotocol(current_structdef) and
  1025. ((idtoken=_REQUIRED) or
  1026. (idtoken=_OPTIONAL)) then
  1027. begin
  1028. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  1029. consume(idtoken)
  1030. end
  1031. else case idtoken of
  1032. _PRIVATE :
  1033. begin
  1034. parse_visibility(vis_private,oo_has_private);
  1035. end;
  1036. _PROTECTED :
  1037. begin
  1038. parse_visibility(vis_protected,oo_has_protected);
  1039. end;
  1040. _PUBLIC :
  1041. begin
  1042. parse_visibility(vis_public,oo_none);
  1043. end;
  1044. _PUBLISHED :
  1045. begin
  1046. parse_visibility(vis_published,oo_none);
  1047. end;
  1048. _STRICT :
  1049. begin
  1050. if is_interface(current_structdef) or
  1051. is_objc_protocol_or_category(current_structdef) or
  1052. is_javainterface(current_structdef) then
  1053. Message(parser_e_no_access_specifier_in_interfaces);
  1054. consume(_STRICT);
  1055. if token=_ID then
  1056. begin
  1057. case idtoken of
  1058. _PRIVATE:
  1059. begin
  1060. consume(_PRIVATE);
  1061. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  1062. include(current_structdef.objectoptions,oo_has_strictprivate);
  1063. end;
  1064. _PROTECTED:
  1065. begin
  1066. consume(_PROTECTED);
  1067. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  1068. include(current_structdef.objectoptions,oo_has_strictprotected);
  1069. end;
  1070. else
  1071. message(parser_e_protected_or_private_expected);
  1072. end;
  1073. end
  1074. else
  1075. message(parser_e_protected_or_private_expected);
  1076. fields_allowed:=true;
  1077. is_classdef:=false;
  1078. class_fields:=false;
  1079. is_final:=false;
  1080. final_fields:=false;
  1081. object_member_blocktype:=bt_general;
  1082. end
  1083. else if (m_final_fields in current_settings.modeswitches) and
  1084. (token=_ID) and
  1085. (idtoken=_FINAL) then
  1086. begin
  1087. { currently only supported for external classes, because
  1088. requires fully working DFA otherwise }
  1089. if (current_structdef.typ<>objectdef) or
  1090. not(oo_is_external in tobjectdef(current_structdef).objectoptions) then
  1091. Message(parser_e_final_only_external);
  1092. consume(_final);
  1093. is_final:=true;
  1094. if token=_CLASS then
  1095. parse_class;
  1096. if not(token in [_CONST,_VAR]) then
  1097. message(parser_e_final_only_const_var);
  1098. end
  1099. else
  1100. begin
  1101. if object_member_blocktype=bt_general then
  1102. begin
  1103. if is_interface(current_structdef) or
  1104. is_objc_protocol_or_category(current_structdef) or
  1105. is_objectpascal_helper(current_structdef) or
  1106. (is_javainterface(current_structdef) and
  1107. not(class_fields and final_fields)) then
  1108. Message(parser_e_no_vars_in_interfaces);
  1109. if (current_structdef.symtable.currentvisibility=vis_published) and
  1110. not(oo_can_have_published in current_structdef.objectoptions) then
  1111. Message(parser_e_cant_have_published);
  1112. if (not fields_allowed) then
  1113. Message(parser_e_field_not_allowed_here);
  1114. vdoptions:=[vd_object];
  1115. if class_fields then
  1116. include(vdoptions,vd_class);
  1117. if is_class(current_structdef) then
  1118. include(vdoptions,vd_canreorder);
  1119. if final_fields then
  1120. include(vdoptions,vd_final);
  1121. read_record_fields(vdoptions,fieldlist);
  1122. end
  1123. else if object_member_blocktype=bt_type then
  1124. types_dec(true)
  1125. else if object_member_blocktype=bt_const then
  1126. begin
  1127. if final_fields then
  1128. begin
  1129. { the value of final fields cannot be changed
  1130. once they've been assigned a value }
  1131. typedconstswritable:=cs_typed_const_writable in current_settings.localswitches;
  1132. exclude(current_settings.localswitches,cs_typed_const_writable);
  1133. end;
  1134. consts_dec(true,not is_javainterface(current_structdef));
  1135. if final_fields and
  1136. typedconstswritable then
  1137. include(current_settings.localswitches,cs_typed_const_writable);
  1138. end
  1139. else
  1140. internalerror(201001110);
  1141. end;
  1142. end;
  1143. end;
  1144. _PROPERTY :
  1145. begin
  1146. struct_property_dec(is_classdef);
  1147. fields_allowed:=false;
  1148. is_classdef:=false;
  1149. end;
  1150. _CLASS:
  1151. begin
  1152. parse_class;
  1153. end;
  1154. _PROCEDURE,
  1155. _FUNCTION,
  1156. _CONSTRUCTOR,
  1157. _DESTRUCTOR :
  1158. begin
  1159. method_dec(current_structdef,is_classdef);
  1160. fields_allowed:=false;
  1161. is_classdef:=false;
  1162. end;
  1163. _END :
  1164. begin
  1165. consume(_END);
  1166. break;
  1167. end;
  1168. else
  1169. consume(_ID); { Give a ident expected message, like tp7 }
  1170. end;
  1171. until false;
  1172. if is_class(current_structdef) then
  1173. tabstractrecordsymtable(current_structdef.symtable).addfieldlist(fieldlist,true);
  1174. fieldlist.free;
  1175. end;
  1176. function object_dec(objecttype:tobjecttyp;const n:tidstring;objsym:tsym;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  1177. var
  1178. old_current_structdef: tabstractrecorddef;
  1179. old_current_genericdef,
  1180. old_current_specializedef: tstoreddef;
  1181. hrecst: trecordsymtable;
  1182. fsym: tfieldvarsym;
  1183. old_parse_generic: boolean;
  1184. list: TFPObjectList;
  1185. s: String;
  1186. st: TSymtable;
  1187. olddef: tdef;
  1188. begin
  1189. old_current_structdef:=current_structdef;
  1190. old_current_genericdef:=current_genericdef;
  1191. old_current_specializedef:=current_specializedef;
  1192. old_parse_generic:=parse_generic;
  1193. current_structdef:=nil;
  1194. current_genericdef:=nil;
  1195. current_specializedef:=nil;
  1196. { objects and class types can't be declared local }
  1197. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1198. not assigned(genericlist) then
  1199. Message(parser_e_no_local_objects);
  1200. { reuse forward objectdef? }
  1201. if assigned(fd) then
  1202. begin
  1203. if fd.objecttype<>objecttype then
  1204. begin
  1205. Message(parser_e_forward_mismatch);
  1206. { recover }
  1207. current_structdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  1208. include(current_structdef.objectoptions,oo_is_forward);
  1209. end
  1210. else
  1211. current_structdef:=fd
  1212. end
  1213. else
  1214. begin
  1215. { anonym objects aren't allow (o : object a : longint; end;) }
  1216. if n='' then
  1217. Message(parser_f_no_anonym_objects);
  1218. { create new class }
  1219. current_structdef:=tobjectdef.create(objecttype,n,nil);
  1220. { include always the forward flag, it'll be removed after the parent class have been
  1221. added. This is to prevent circular childof loops }
  1222. include(current_structdef.objectoptions,oo_is_forward);
  1223. if (cs_compilesystem in current_settings.moduleswitches) then
  1224. begin
  1225. case current_objectdef.objecttype of
  1226. odt_interfacecom :
  1227. if (current_structdef.objname^='IUNKNOWN') then
  1228. interface_iunknown:=current_objectdef
  1229. else
  1230. if (current_structdef.objname^='IDISPATCH') then
  1231. interface_idispatch:=current_objectdef;
  1232. odt_class :
  1233. if (current_structdef.objname^='TOBJECT') then
  1234. class_tobject:=current_objectdef;
  1235. odt_javaclass:
  1236. begin
  1237. if (current_structdef.objname^='TOBJECT') then
  1238. class_tobject:=current_objectdef
  1239. else if (current_objectdef.objname^='JLOBJECT') then
  1240. begin
  1241. java_jlobject:=current_objectdef;
  1242. { the methodpointer type is normally created in
  1243. psystem, but java_jlobject is not yet available
  1244. there... }
  1245. hrecst:=trecordsymtable.create('',1);
  1246. fsym:=tfieldvarsym.create('$proc',vs_value,java_jlobject,[]);
  1247. hrecst.insert(fsym);
  1248. hrecst.addfield(fsym,vis_hidden);
  1249. fsym:=tfieldvarsym.create('$data',vs_value,java_jlobject,[]);
  1250. hrecst.insert(fsym);
  1251. hrecst.addfield(fsym,vis_hidden);
  1252. methodpointertype:=trecorddef.create('',hrecst);
  1253. systemunit.insert(ttypesym.create('$methodpointer',methodpointertype));
  1254. end
  1255. else if (current_objectdef.objname^='JLTHROWABLE') then
  1256. java_jlthrowable:=current_objectdef
  1257. else if (current_objectdef.objname^='FPCBASERECORDTYPE') then
  1258. java_fpcbaserecordtype:=current_objectdef
  1259. else if (current_objectdef.objname^='JLSTRING') then
  1260. java_jlstring:=current_objectdef
  1261. else if (current_objectdef.objname^='ANSISTRINGCLASS') then
  1262. java_ansistring:=current_objectdef
  1263. else if (current_objectdef.objname^='SHORTSTRINGCLASS') then
  1264. java_shortstring:=current_objectdef
  1265. else if (current_objectdef.objname^='JLENUM') then
  1266. java_jlenum:=current_objectdef
  1267. else if (current_objectdef.objname^='JUENUMSET') then
  1268. java_juenumset:=current_objectdef
  1269. else if (current_objectdef.objname^='FPCBITSET') then
  1270. java_jubitset:=current_objectdef
  1271. else if (current_objectdef.objname^='FPCBASEPROCVARTYPE') then
  1272. java_procvarbase:=current_objectdef;
  1273. end;
  1274. end;
  1275. end;
  1276. if (current_module.modulename^='OBJCBASE') then
  1277. begin
  1278. case current_objectdef.objecttype of
  1279. odt_objcclass:
  1280. if (current_objectdef.objname^='Protocol') then
  1281. objc_protocoltype:=current_objectdef;
  1282. end;
  1283. end;
  1284. end;
  1285. { usage of specialized type inside its generic template }
  1286. if assigned(genericdef) then
  1287. current_specializedef:=current_structdef
  1288. { reject declaration of generic class inside generic class }
  1289. else if assigned(genericlist) then
  1290. current_genericdef:=current_structdef;
  1291. { nested types of specializations are specializations as well }
  1292. if assigned(old_current_structdef) and
  1293. (df_specialization in old_current_structdef.defoptions) then
  1294. include(current_structdef.defoptions,df_specialization);
  1295. if assigned(old_current_structdef) and
  1296. (df_generic in old_current_structdef.defoptions) then
  1297. begin
  1298. include(current_structdef.defoptions,df_generic);
  1299. current_genericdef:=current_structdef;
  1300. end;
  1301. { set published flag in $M+ mode, it can also be inherited and will
  1302. be added when the parent class set with tobjectdef.set_parent (PFV) }
  1303. if (cs_generate_rtti in current_settings.localswitches) and
  1304. (current_objectdef.objecttype in [odt_interfacecom,odt_class,odt_helper]) then
  1305. include(current_structdef.objectoptions,oo_can_have_published);
  1306. { Objective-C/Java objectdefs can be "formal definitions", in which case
  1307. the syntax is "type tc = objcclass external;" -> we have to parse
  1308. its object options (external) already here, to make sure that such
  1309. definitions are recognised as formal defs }
  1310. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  1311. parse_object_options;
  1312. { forward def? }
  1313. if not assigned(fd) and
  1314. (token=_SEMICOLON) then
  1315. begin
  1316. { add to the list of definitions to check that the forward
  1317. is resolved. this is required for delphi mode }
  1318. current_module.checkforwarddefs.add(current_structdef);
  1319. end
  1320. else
  1321. begin
  1322. { change objccategories into objcclass helpers }
  1323. if (objecttype=odt_objccategory) then
  1324. begin
  1325. current_objectdef.objecttype:=odt_objcclass;
  1326. include(current_structdef.objectoptions,oo_is_classhelper);
  1327. end;
  1328. { include the class helper flag for Object Pascal helpers }
  1329. if (objecttype=odt_helper) then
  1330. include(current_objectdef.objectoptions,oo_is_classhelper);
  1331. { parse list of options (abstract / sealed) }
  1332. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava]) then
  1333. parse_object_options;
  1334. symtablestack.push(current_structdef.symtable);
  1335. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  1336. { when we are parsing a generic already then this is a generic as
  1337. well }
  1338. if old_parse_generic then
  1339. include(current_structdef.defoptions, df_generic);
  1340. parse_generic:=(df_generic in current_structdef.defoptions);
  1341. { in non-Delphi modes we need a strict private symbol without type
  1342. count and type parameters in the name to simply resolving }
  1343. maybe_insert_generic_rename_symbol(n,genericlist);
  1344. { parse list of parent classes }
  1345. { for record helpers in mode Delphi this is not allowed }
  1346. if not (is_objectpascal_helper(current_objectdef) and
  1347. (m_delphi in current_settings.modeswitches) and
  1348. (helpertype=ht_record)) then
  1349. parse_parent_classes
  1350. else
  1351. { remove forward flag, is resolved (this is normally done inside
  1352. parse_parent_classes) }
  1353. exclude(current_structdef.objectoptions,oo_is_forward);
  1354. { parse extended type for helpers }
  1355. if is_objectpascal_helper(current_structdef) then
  1356. parse_extended_type(helpertype);
  1357. { parse optional GUID for interfaces }
  1358. parse_guid;
  1359. { classes can handle links to themself not only inside type blocks
  1360. but in const blocks too. to make this possible we need to set
  1361. their symbols to real defs instead of errordef }
  1362. if assigned(objsym) and (objecttype in [odt_class,odt_javaclass,odt_interfacejava]) then
  1363. begin
  1364. olddef:=ttypesym(objsym).typedef;
  1365. ttypesym(objsym).typedef:=current_structdef;
  1366. current_structdef.typesym:=objsym;
  1367. end
  1368. else
  1369. olddef:=nil;
  1370. { parse and insert object members }
  1371. parse_object_members;
  1372. if assigned(olddef) then
  1373. begin
  1374. ttypesym(objsym).typedef:=olddef;
  1375. current_structdef.typesym:=nil;
  1376. end;
  1377. if not(oo_is_external in current_structdef.objectoptions) then
  1378. begin
  1379. { In Java, constructors are not automatically inherited (so you can
  1380. hide them). Emulate the Pascal behaviour for classes implemented
  1381. in Pascal (we cannot do it for classes implemented in Java, since
  1382. we obviously cannot add constructors to those) }
  1383. if is_javaclass(current_structdef) then
  1384. begin
  1385. add_missing_parent_constructors_intf(tobjectdef(current_structdef),true,vis_none);
  1386. {$ifdef jvm}
  1387. maybe_add_public_default_java_constructor(tobjectdef(current_structdef));
  1388. jvm_wrap_virtual_class_methods(tobjectdef(current_structdef));
  1389. {$endif}
  1390. end;
  1391. { need method to hold the initialization code for typed constants? }
  1392. if (target_info.system in systems_typed_constants_node_init) and
  1393. not is_any_interface_kind(current_structdef) then
  1394. add_typedconst_init_routine(current_structdef);
  1395. end;
  1396. symtablestack.pop(current_structdef.symtable);
  1397. end;
  1398. { generate vmt space if needed }
  1399. if not(oo_has_vmt in current_structdef.objectoptions) and
  1400. not(oo_is_forward in current_structdef.objectoptions) and
  1401. { no vmt for helpers ever }
  1402. not is_objectpascal_helper(current_structdef) and
  1403. (
  1404. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1405. (current_objectdef.objecttype in [odt_class])
  1406. ) then
  1407. current_objectdef.insertvmt;
  1408. { for implemented classes with a vmt check if there is a constructor }
  1409. if (oo_has_vmt in current_structdef.objectoptions) and
  1410. not(oo_is_forward in current_structdef.objectoptions) and
  1411. not(oo_has_constructor in current_structdef.objectoptions) and
  1412. not is_objc_class_or_protocol(current_structdef) and
  1413. not is_java_class_or_interface(current_structdef) then
  1414. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1415. if is_interface(current_structdef) or
  1416. is_objcprotocol(current_structdef) or
  1417. is_javainterface(current_structdef) then
  1418. setinterfacemethodoptions
  1419. else if is_objcclass(current_structdef) then
  1420. setobjcclassmethodoptions;
  1421. { if this helper is defined in the implementation section of the unit
  1422. or inside the main project file, the extendeddefs list of the current
  1423. module must be updated (it will be removed when poping the symtable) }
  1424. if is_objectpascal_helper(current_structdef) and
  1425. (current_objectdef.extendeddef.typ<>errordef) then
  1426. begin
  1427. { the topmost symtable must be a static symtable }
  1428. st:=current_structdef.owner;
  1429. while st.symtabletype in [objectsymtable,recordsymtable] do
  1430. st:=st.defowner.owner;
  1431. if st.symtabletype=staticsymtable then
  1432. begin
  1433. if current_objectdef.extendeddef.typ in [recorddef,objectdef] then
  1434. s:=make_mangledname('',tabstractrecorddef(current_objectdef.extendeddef).symtable,'')
  1435. else
  1436. s:=make_mangledname('',current_objectdef.extendeddef.owner,current_objectdef.extendeddef.typesym.name);
  1437. Message1(sym_d_adding_helper_for,s);
  1438. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1439. if not assigned(list) then
  1440. begin
  1441. list:=TFPObjectList.Create(false);
  1442. current_module.extendeddefs.Add(s, list);
  1443. end;
  1444. list.add(current_structdef);
  1445. end;
  1446. end;
  1447. tabstractrecordsymtable(current_objectdef.symtable).addalignmentpadding;
  1448. { return defined objectdef }
  1449. result:=current_objectdef;
  1450. { restore old state }
  1451. current_structdef:=old_current_structdef;
  1452. current_genericdef:=old_current_genericdef;
  1453. current_specializedef:=old_current_specializedef;
  1454. parse_generic:=old_parse_generic;
  1455. end;
  1456. end.