ptype.pas 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing 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 ptype;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,cclasses,
  22. symtype,symdef,symbase;
  23. type
  24. TSingleTypeOption=(
  25. stoIsForwardDef, { foward declaration }
  26. stoAllowTypeDef, { allow type definitions }
  27. stoAllowSpecialization, { allow type specialization }
  28. stoParseClassParent { parse of parent class type }
  29. );
  30. TSingleTypeOptions=set of TSingleTypeOption;
  31. procedure resolve_forward_types;
  32. { reads a string, file type or a type identifier }
  33. procedure single_type(var def:tdef;options:TSingleTypeOptions);
  34. { reads any type declaration, where the resulting type will get name as type identifier }
  35. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  36. { reads any type declaration }
  37. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  38. { generate persistent type information like VMT, RTTI and inittables }
  39. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  40. { add a definition for a method to a record/objectdef that will contain
  41. all code for initialising typed constants (only for targets in
  42. systems.systems_typed_constants_node_init) }
  43. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  44. { parse hint directives (platform, deprecated, ...) for a procdef }
  45. procedure maybe_parse_hint_directives(pd:tprocdef);
  46. implementation
  47. uses
  48. { common }
  49. cutils,
  50. { global }
  51. globals,tokens,verbose,constexp,
  52. systems,
  53. { target }
  54. paramgr,procinfo,
  55. { symtable }
  56. symconst,symsym,symtable,symcreat,
  57. defutil,defcmp,
  58. {$ifdef jvm}
  59. jvmdef,
  60. {$endif}
  61. { modules }
  62. fmodule,
  63. { pass 1 }
  64. node,ncgrtti,nobj,
  65. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  66. { parser }
  67. scanner,
  68. pbase,pexpr,pdecsub,pdecvar,pdecobj,pdecl,pgenutil
  69. {$ifdef jvm}
  70. ,pjvm
  71. {$endif}
  72. ;
  73. procedure maybe_parse_hint_directives(pd:tprocdef);
  74. var
  75. dummysymoptions : tsymoptions;
  76. deprecatedmsg : pshortstring;
  77. begin
  78. dummysymoptions:=[];
  79. deprecatedmsg:=nil;
  80. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  81. Consume(_SEMICOLON);
  82. if assigned(pd) then
  83. begin
  84. pd.symoptions:=pd.symoptions+dummysymoptions;
  85. pd.deprecatedmsg:=deprecatedmsg;
  86. end
  87. else
  88. stringdispose(deprecatedmsg);
  89. end;
  90. procedure resolve_forward_types;
  91. var
  92. i: longint;
  93. hpd,
  94. def : tdef;
  95. srsym : tsym;
  96. srsymtable : TSymtable;
  97. hs : string;
  98. begin
  99. for i:=0 to current_module.checkforwarddefs.Count-1 do
  100. begin
  101. def:=tdef(current_module.checkforwarddefs[i]);
  102. case def.typ of
  103. pointerdef,
  104. classrefdef :
  105. begin
  106. { classrefdef inherits from pointerdef }
  107. hpd:=tabstractpointerdef(def).pointeddef;
  108. { still a forward def ? }
  109. if hpd.typ=forwarddef then
  110. begin
  111. { try to resolve the forward }
  112. if not assigned(tforwarddef(hpd).tosymname) then
  113. internalerror(200211201);
  114. hs:=tforwarddef(hpd).tosymname^;
  115. searchsym(upper(hs),srsym,srsymtable);
  116. { we don't need the forwarddef anymore, dispose it }
  117. hpd.free;
  118. tabstractpointerdef(def).pointeddef:=nil; { if error occurs }
  119. { was a type sym found ? }
  120. if assigned(srsym) and
  121. (srsym.typ=typesym) then
  122. begin
  123. tabstractpointerdef(def).pointeddef:=ttypesym(srsym).typedef;
  124. { avoid wrong unused warnings web bug 801 PM }
  125. inc(ttypesym(srsym).refs);
  126. { we need a class type for classrefdef }
  127. if (def.typ=classrefdef) and
  128. not(is_class(ttypesym(srsym).typedef)) and
  129. not(is_objcclass(ttypesym(srsym).typedef)) and
  130. not(is_javaclass(ttypesym(srsym).typedef)) then
  131. MessagePos1(def.typesym.fileinfo,type_e_class_type_expected,ttypesym(srsym).typedef.typename);
  132. end
  133. else
  134. begin
  135. Message1(sym_e_forward_type_not_resolved,hs);
  136. { try to recover }
  137. tabstractpointerdef(def).pointeddef:=generrordef;
  138. end;
  139. end;
  140. end;
  141. objectdef :
  142. begin
  143. { give an error as the implementation may follow in an
  144. other type block which is allowed by FPC modes }
  145. if not(m_fpc in current_settings.modeswitches) and
  146. (oo_is_forward in tobjectdef(def).objectoptions) then
  147. MessagePos1(def.typesym.fileinfo,type_e_type_is_not_completly_defined,def.typename);
  148. end;
  149. else
  150. internalerror(200811071);
  151. end;
  152. end;
  153. current_module.checkforwarddefs.clear;
  154. end;
  155. procedure id_type(var def : tdef;isforwarddef,checkcurrentrecdef,allowgenericsyms:boolean;out srsym:tsym;out srsymtable:tsymtable); forward;
  156. { def is the outermost type in which other types have to be searched
  157. isforward indicates whether the current definition can be a forward definition
  158. if assigned, currentstructstack is a list of tabstractrecorddefs that, from
  159. last to first, are child types of def that are not yet visible via the
  160. normal symtable searching routines because they are types that are currently
  161. being parsed (so using id_type on them after pushing def on the
  162. symtablestack would result in errors because they'd come back as errordef)
  163. }
  164. procedure parse_nested_types(var def: tdef; isforwarddef: boolean; currentstructstack: tfpobjectlist);
  165. var
  166. t2: tdef;
  167. structstackindex: longint;
  168. srsym: tsym;
  169. srsymtable: tsymtable;
  170. oldsymtablestack: TSymtablestack;
  171. begin
  172. if assigned(currentstructstack) then
  173. structstackindex:=currentstructstack.count-1
  174. else
  175. structstackindex:=-1;
  176. { handle types inside classes, e.g. TNode.TLongint }
  177. while (token=_POINT) do
  178. begin
  179. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  180. begin
  181. if (def.typ=objectdef) then
  182. def:=find_real_class_definition(tobjectdef(def),false);
  183. consume(_POINT);
  184. if (structstackindex>=0) and
  185. (tabstractrecorddef(currentstructstack[structstackindex]).objname^=pattern) then
  186. begin
  187. def:=tdef(currentstructstack[structstackindex]);
  188. dec(structstackindex);
  189. consume(_ID);
  190. end
  191. else
  192. begin
  193. structstackindex:=-1;
  194. oldsymtablestack:=symtablestack;
  195. symtablestack:=TSymtablestack.create;
  196. symtablestack.push(tabstractrecorddef(def).symtable);
  197. t2:=generrordef;
  198. id_type(t2,isforwarddef,false,false,srsym,srsymtable);
  199. symtablestack.pop(tabstractrecorddef(def).symtable);
  200. symtablestack.free;
  201. symtablestack:=oldsymtablestack;
  202. def:=t2;
  203. end;
  204. end
  205. else
  206. break;
  207. end;
  208. end;
  209. function try_parse_structdef_nested_type(out def: tdef; basedef: tabstractrecorddef; isfowarddef: boolean): boolean;
  210. var
  211. structdef : tdef;
  212. structdefstack : tfpobjectlist;
  213. begin
  214. def:=nil;
  215. { use of current parsed object:
  216. classes, objects, records can be used also in themself }
  217. structdef:=basedef;
  218. structdefstack:=nil;
  219. while assigned(structdef) and (structdef.typ in [objectdef,recorddef]) do
  220. begin
  221. if (tabstractrecorddef(structdef).objname^=pattern) then
  222. begin
  223. consume(_ID);
  224. def:=structdef;
  225. { we found the top-most match, now check how far down we can
  226. follow }
  227. structdefstack:=tfpobjectlist.create(false);
  228. structdef:=basedef;
  229. while (structdef<>def) do
  230. begin
  231. structdefstack.add(structdef);
  232. structdef:=tabstractrecorddef(structdef.owner.defowner);
  233. end;
  234. parse_nested_types(def,isfowarddef,structdefstack);
  235. structdefstack.free;
  236. result:=true;
  237. exit;
  238. end;
  239. structdef:=tdef(tabstractrecorddef(structdef).owner.defowner);
  240. end;
  241. result:=false;
  242. end;
  243. procedure id_type(var def : tdef;isforwarddef,checkcurrentrecdef,allowgenericsyms:boolean;out srsym:tsym;out srsymtable:tsymtable);
  244. { reads a type definition }
  245. { to a appropriating tdef, s gets the name of }
  246. { the type to allow name mangling }
  247. var
  248. is_unit_specific : boolean;
  249. pos : tfileposinfo;
  250. s,sorg : TIDString;
  251. t : ttoken;
  252. begin
  253. srsym:=nil;
  254. srsymtable:=nil;
  255. s:=pattern;
  256. sorg:=orgpattern;
  257. pos:=current_tokenpos;
  258. { use of current parsed object:
  259. classes, objects, records can be used also in themself }
  260. if checkcurrentrecdef and
  261. try_parse_structdef_nested_type(def,current_structdef,isforwarddef) then
  262. exit;
  263. { Use the special searchsym_type that search only types }
  264. searchsym_type(s,srsym,srsymtable);
  265. { handle unit specification like System.Writeln }
  266. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t,true);
  267. consume(t);
  268. { Types are first defined with an error def before assigning
  269. the real type so check if it's an errordef. if so then
  270. give an error. Only check for typesyms in the current symbol
  271. table as forwarddef are not resolved directly }
  272. if assigned(srsym) and
  273. (srsym.typ=typesym) and
  274. ((ttypesym(srsym).typedef.typ=errordef) or
  275. (not allowgenericsyms and
  276. (ttypesym(srsym).typedef.typ=undefineddef) and
  277. not (sp_generic_para in srsym.symoptions))) then
  278. begin
  279. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  280. def:=generrordef;
  281. exit;
  282. end;
  283. { are we parsing a possible forward def ? }
  284. if isforwarddef and
  285. not(is_unit_specific) then
  286. begin
  287. def:=tforwarddef.create(sorg,pos);
  288. exit;
  289. end;
  290. { unknown sym ? }
  291. if not assigned(srsym) then
  292. begin
  293. Message1(sym_e_id_not_found,sorg);
  294. def:=generrordef;
  295. exit;
  296. end;
  297. { type sym ? }
  298. if (srsym.typ<>typesym) then
  299. begin
  300. Message(type_e_type_id_expected);
  301. def:=generrordef;
  302. exit;
  303. end;
  304. { Give an error when referring to an errordef }
  305. if (ttypesym(srsym).typedef.typ=errordef) then
  306. begin
  307. Message(sym_e_error_in_type_def);
  308. def:=generrordef;
  309. exit;
  310. end;
  311. { In non-Delphi modes the class/record name of a generic might be used
  312. in the declaration of sub types without type parameters; in that case
  313. we need to check by name as the link from the dummy symbol to the
  314. current type is not yet established }
  315. if (sp_generic_dummy in srsym.symoptions) and
  316. assigned(current_structdef) and
  317. (df_generic in current_structdef.defoptions) and
  318. (ttypesym(srsym).typedef.typ=undefineddef) and
  319. not (m_delphi in current_settings.modeswitches) then
  320. begin
  321. def:=get_generic_in_hierarchy_by_name(srsym,current_structdef);
  322. if assigned(def) then
  323. exit;
  324. end;
  325. def:=ttypesym(srsym).typedef;
  326. end;
  327. procedure single_type(var def:tdef;options:TSingleTypeOptions);
  328. var
  329. t2 : tdef;
  330. dospecialize,
  331. again : boolean;
  332. srsym : tsym;
  333. srsymtable : tsymtable;
  334. begin
  335. dospecialize:=false;
  336. repeat
  337. again:=false;
  338. case token of
  339. _STRING:
  340. string_dec(def,stoAllowTypeDef in options);
  341. _FILE:
  342. begin
  343. consume(_FILE);
  344. if (token=_OF) then
  345. begin
  346. if not(stoAllowTypeDef in options) then
  347. Message(parser_e_no_local_para_def);
  348. consume(_OF);
  349. single_type(t2,[stoAllowTypeDef]);
  350. if is_managed_type(t2) then
  351. Message(parser_e_no_refcounted_typed_file);
  352. def:=tfiledef.createtyped(t2);
  353. end
  354. else
  355. def:=cfiletype;
  356. end;
  357. _ID:
  358. begin
  359. if try_to_consume(_SPECIALIZE) then
  360. begin
  361. if ([stoAllowSpecialization,stoAllowTypeDef] * options = []) then
  362. begin
  363. Message(parser_e_no_local_para_def);
  364. { try to recover }
  365. while token<>_SEMICOLON do
  366. consume(token);
  367. def:=generrordef;
  368. end
  369. else
  370. begin
  371. dospecialize:=true;
  372. again:=true;
  373. end;
  374. end
  375. else
  376. begin
  377. id_type(def,stoIsForwardDef in options,true,true,srsym,srsymtable);
  378. parse_nested_types(def,stoIsForwardDef in options,nil);
  379. end;
  380. end;
  381. else
  382. begin
  383. message(type_e_type_id_expected);
  384. def:=generrordef;
  385. end;
  386. end;
  387. until not again;
  388. if ([stoAllowSpecialization,stoAllowTypeDef] * options <> []) and
  389. (m_delphi in current_settings.modeswitches) then
  390. dospecialize:=token in [_LSHARPBRACKET,_LT];
  391. if dospecialize then
  392. generate_specialization(def,stoParseClassParent in options,'',nil,'')
  393. else
  394. begin
  395. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  396. begin
  397. def:=current_specializedef
  398. end
  399. else if (def=current_genericdef) then
  400. begin
  401. def:=current_genericdef
  402. end
  403. { when parsing a nested specialization in non-Delphi mode it might
  404. use the name of the topmost generic without type paramaters, thus
  405. def will contain the generic definition, but we need a reference
  406. to the specialization of that generic }
  407. { TODO : only in non-Delphi modes? }
  408. else if assigned(current_structdef) and
  409. (df_specialization in current_structdef.defoptions) and
  410. return_specialization_of_generic(current_structdef,def,t2) then
  411. begin
  412. def:=t2
  413. end
  414. else if (df_generic in def.defoptions) and
  415. not
  416. (
  417. parse_generic and
  418. (current_genericdef.typ in [recorddef,objectdef]) and
  419. (
  420. { if both defs belong to the same generic (e.g. both are
  421. subtypes) then we must allow the usage }
  422. defs_belong_to_same_generic(def,current_genericdef) or
  423. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  424. declarations inside a generic }
  425. sym_is_owned_by(srsym,tabstractrecorddef(current_genericdef).symtable)
  426. )
  427. )
  428. then
  429. begin
  430. Message(parser_e_no_generics_as_types);
  431. def:=generrordef;
  432. end
  433. else if (def.typ=undefineddef) and
  434. (sp_generic_dummy in srsym.symoptions) and
  435. parse_generic and
  436. (current_genericdef.typ in [recorddef,objectdef]) and
  437. (Pos(upper(srsym.realname),tabstractrecorddef(current_genericdef).objname^)=1) then
  438. begin
  439. if m_delphi in current_settings.modeswitches then
  440. begin
  441. Message(parser_e_no_generics_as_types);
  442. def:=generrordef;
  443. end
  444. else
  445. def:=current_genericdef;
  446. end
  447. else if is_classhelper(def) and
  448. not (stoParseClassParent in options) then
  449. begin
  450. Message(parser_e_no_category_as_types);
  451. def:=generrordef
  452. end
  453. end;
  454. end;
  455. procedure parse_record_members;
  456. var
  457. pd : tprocdef;
  458. oldparse_only: boolean;
  459. member_blocktype : tblock_type;
  460. fields_allowed, is_classdef, classfields: boolean;
  461. vdoptions: tvar_dec_options;
  462. begin
  463. { empty record declaration ? }
  464. if (token=_SEMICOLON) then
  465. Exit;
  466. current_structdef.symtable.currentvisibility:=vis_public;
  467. fields_allowed:=true;
  468. is_classdef:=false;
  469. classfields:=false;
  470. member_blocktype:=bt_general;
  471. repeat
  472. case token of
  473. _TYPE :
  474. begin
  475. consume(_TYPE);
  476. member_blocktype:=bt_type;
  477. { local and anonymous records can not have inner types. skip top record symtable }
  478. if (current_structdef.objname^='') or
  479. not(symtablestack.stack^.next^.symtable.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) then
  480. Message(parser_e_no_types_in_local_anonymous_records);
  481. end;
  482. _VAR :
  483. begin
  484. consume(_VAR);
  485. fields_allowed:=true;
  486. member_blocktype:=bt_general;
  487. classfields:=is_classdef;
  488. is_classdef:=false;
  489. end;
  490. _CONST:
  491. begin
  492. consume(_CONST);
  493. member_blocktype:=bt_const;
  494. end;
  495. _ID, _CASE, _OPERATOR :
  496. begin
  497. case idtoken of
  498. _PRIVATE :
  499. begin
  500. consume(_PRIVATE);
  501. current_structdef.symtable.currentvisibility:=vis_private;
  502. include(current_structdef.objectoptions,oo_has_private);
  503. fields_allowed:=true;
  504. is_classdef:=false;
  505. classfields:=false;
  506. member_blocktype:=bt_general;
  507. end;
  508. _PROTECTED :
  509. begin
  510. consume(_PROTECTED);
  511. current_structdef.symtable.currentvisibility:=vis_protected;
  512. include(current_structdef.objectoptions,oo_has_protected);
  513. fields_allowed:=true;
  514. is_classdef:=false;
  515. classfields:=false;
  516. member_blocktype:=bt_general;
  517. end;
  518. _PUBLIC :
  519. begin
  520. consume(_PUBLIC);
  521. current_structdef.symtable.currentvisibility:=vis_public;
  522. fields_allowed:=true;
  523. is_classdef:=false;
  524. classfields:=false;
  525. member_blocktype:=bt_general;
  526. end;
  527. _PUBLISHED :
  528. begin
  529. Message(parser_e_no_record_published);
  530. consume(_PUBLISHED);
  531. current_structdef.symtable.currentvisibility:=vis_published;
  532. fields_allowed:=true;
  533. is_classdef:=false;
  534. classfields:=false;
  535. member_blocktype:=bt_general;
  536. end;
  537. _STRICT :
  538. begin
  539. consume(_STRICT);
  540. if token=_ID then
  541. begin
  542. case idtoken of
  543. _PRIVATE:
  544. begin
  545. consume(_PRIVATE);
  546. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  547. include(current_structdef.objectoptions,oo_has_strictprivate);
  548. end;
  549. _PROTECTED:
  550. begin
  551. consume(_PROTECTED);
  552. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  553. include(current_structdef.objectoptions,oo_has_strictprotected);
  554. end;
  555. else
  556. message(parser_e_protected_or_private_expected);
  557. end;
  558. end
  559. else
  560. message(parser_e_protected_or_private_expected);
  561. fields_allowed:=true;
  562. is_classdef:=false;
  563. classfields:=false;
  564. member_blocktype:=bt_general;
  565. end
  566. else
  567. if is_classdef and (idtoken=_OPERATOR) then
  568. begin
  569. pd:=parse_record_method_dec(current_structdef,is_classdef);
  570. fields_allowed:=false;
  571. is_classdef:=false;
  572. end
  573. else
  574. begin
  575. if member_blocktype=bt_general then
  576. begin
  577. if (not fields_allowed)and(idtoken<>_CASE) then
  578. Message(parser_e_field_not_allowed_here);
  579. vdoptions:=[vd_record];
  580. if classfields then
  581. include(vdoptions,vd_class);
  582. read_record_fields(vdoptions);
  583. end
  584. else if member_blocktype=bt_type then
  585. types_dec(true)
  586. else if member_blocktype=bt_const then
  587. consts_dec(true,true)
  588. else
  589. internalerror(201001110);
  590. end;
  591. end;
  592. end;
  593. _PROPERTY :
  594. begin
  595. struct_property_dec(is_classdef);
  596. fields_allowed:=false;
  597. is_classdef:=false;
  598. end;
  599. _CLASS:
  600. begin
  601. is_classdef:=false;
  602. { read class method/field/property }
  603. consume(_CLASS);
  604. { class modifier is only allowed for procedures, functions, }
  605. { constructors, destructors, fields and properties }
  606. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  607. not((token=_ID) and (idtoken=_OPERATOR)) then
  608. Message(parser_e_procedure_or_function_expected);
  609. is_classdef:=true;
  610. end;
  611. _PROCEDURE,
  612. _FUNCTION:
  613. begin
  614. pd:=parse_record_method_dec(current_structdef,is_classdef);
  615. fields_allowed:=false;
  616. is_classdef:=false;
  617. end;
  618. _CONSTRUCTOR :
  619. begin
  620. if not is_classdef then
  621. Message(parser_e_no_constructor_in_records);
  622. if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
  623. Message(parser_w_constructor_should_be_public);
  624. { only 1 class constructor is allowed }
  625. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  626. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  627. oldparse_only:=parse_only;
  628. parse_only:=true;
  629. if is_classdef then
  630. pd:=class_constructor_head(current_structdef)
  631. else
  632. pd:=constructor_head;
  633. parse_only:=oldparse_only;
  634. fields_allowed:=false;
  635. is_classdef:=false;
  636. end;
  637. _DESTRUCTOR :
  638. begin
  639. if not is_classdef then
  640. Message(parser_e_no_destructor_in_records);
  641. { only 1 class destructor is allowed }
  642. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  643. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  644. oldparse_only:=parse_only;
  645. parse_only:=true;
  646. if is_classdef then
  647. pd:=class_destructor_head(current_structdef)
  648. else
  649. pd:=destructor_head;
  650. parse_only:=oldparse_only;
  651. fields_allowed:=false;
  652. is_classdef:=false;
  653. end;
  654. _END :
  655. begin
  656. {$ifdef jvm}
  657. add_java_default_record_methods_intf(trecorddef(current_structdef));
  658. {$endif}
  659. if target_info.system in systems_typed_constants_node_init then
  660. add_typedconst_init_routine(current_structdef);
  661. consume(_END);
  662. break;
  663. end;
  664. else
  665. consume(_ID); { Give a ident expected message, like tp7 }
  666. end;
  667. until false;
  668. end;
  669. { reads a record declaration }
  670. function record_dec(const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  671. var
  672. old_current_structdef: tabstractrecorddef;
  673. old_current_genericdef,
  674. old_current_specializedef: tstoreddef;
  675. old_parse_generic: boolean;
  676. recst: trecordsymtable;
  677. begin
  678. old_current_structdef:=current_structdef;
  679. old_current_genericdef:=current_genericdef;
  680. old_current_specializedef:=current_specializedef;
  681. old_parse_generic:=parse_generic;
  682. current_genericdef:=nil;
  683. current_specializedef:=nil;
  684. { create recdef }
  685. if (n<>'') or
  686. not(target_info.system in systems_jvm) then
  687. begin
  688. recst:=trecordsymtable.create(n,current_settings.packrecords);
  689. { can't use recst.realname^ instead of n, because recst.realname is
  690. nil in case of an empty name }
  691. current_structdef:=trecorddef.create(n,recst);
  692. end
  693. else
  694. begin
  695. { for the JVM target records always need a name, because they are
  696. represented by a class }
  697. recst:=trecordsymtable.create(current_module.realmodulename^+'__fpc_intern_recname_'+tostr(current_module.deflist.count),current_settings.packrecords);
  698. current_structdef:=trecorddef.create(recst.name^,recst);
  699. end;
  700. result:=current_structdef;
  701. { insert in symtablestack }
  702. symtablestack.push(recst);
  703. { usage of specialized type inside its generic template }
  704. if assigned(genericdef) then
  705. current_specializedef:=current_structdef
  706. { reject declaration of generic class inside generic class }
  707. else if assigned(genericlist) then
  708. current_genericdef:=current_structdef;
  709. { nested types of specializations are specializations as well }
  710. if assigned(old_current_structdef) and
  711. (df_specialization in old_current_structdef.defoptions) then
  712. include(current_structdef.defoptions,df_specialization);
  713. if assigned(old_current_structdef) and
  714. (df_generic in old_current_structdef.defoptions) then
  715. begin
  716. include(current_structdef.defoptions,df_generic);
  717. current_genericdef:=current_structdef;
  718. end;
  719. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  720. { when we are parsing a generic already then this is a generic as
  721. well }
  722. if old_parse_generic then
  723. include(current_structdef.defoptions, df_generic);
  724. parse_generic:=(df_generic in current_structdef.defoptions);
  725. { in non-Delphi modes we need a strict private symbol without type
  726. count and type parameters in the name to simply resolving }
  727. maybe_insert_generic_rename_symbol(n,genericlist);
  728. if m_advanced_records in current_settings.modeswitches then
  729. begin
  730. parse_record_members;
  731. end
  732. else
  733. begin
  734. read_record_fields([vd_record]);
  735. {$ifdef jvm}
  736. { we need a constructor to create temps, a deep copy helper, ... }
  737. add_java_default_record_methods_intf(trecorddef(current_structdef));
  738. {$endif}
  739. if target_info.system in systems_typed_constants_node_init then
  740. add_typedconst_init_routine(current_structdef);
  741. consume(_END);
  742. end;
  743. { don't keep track of procdefs in a separate list, because the
  744. compiler may add additional procdefs (e.g. property wrappers for
  745. the jvm backend) }
  746. insert_record_hidden_paras(trecorddef(current_structdef));
  747. { make the record size aligned }
  748. recst.addalignmentpadding;
  749. { restore symtable stack }
  750. symtablestack.pop(recst);
  751. if trecorddef(current_structdef).is_packed and is_managed_type(current_structdef) then
  752. Message(type_e_no_packed_inittable);
  753. { restore old state }
  754. parse_generic:=old_parse_generic;
  755. current_structdef:=old_current_structdef;
  756. current_genericdef:=old_current_genericdef;
  757. current_specializedef:=old_current_specializedef;
  758. end;
  759. { reads a type definition and returns a pointer to it }
  760. procedure read_named_type(var def:tdef;const newsym:tsym;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  761. var
  762. pt : tnode;
  763. tt2 : tdef;
  764. aktenumdef : tenumdef;
  765. s : TIDString;
  766. l,v : TConstExprInt;
  767. oldpackrecords : longint;
  768. defpos,storepos : tfileposinfo;
  769. name: TIDString;
  770. procedure expr_type;
  771. var
  772. pt1,pt2 : tnode;
  773. lv,hv : TConstExprInt;
  774. old_block_type : tblock_type;
  775. dospecialize : boolean;
  776. newdef : tdef;
  777. begin
  778. old_block_type:=block_type;
  779. dospecialize:=false;
  780. { use of current parsed object:
  781. classes, objects, records can be used also in themself }
  782. if (token=_ID) then
  783. if try_parse_structdef_nested_type(def,current_structdef,false) then
  784. exit;
  785. { Generate a specialization in FPC mode? }
  786. dospecialize:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_SPECIALIZE);
  787. { we can't accept a equal in type }
  788. pt1:=comp_expr(false,true);
  789. if not dospecialize and
  790. try_to_consume(_POINTPOINT) then
  791. begin
  792. { get high value of range }
  793. pt2:=comp_expr(false,false);
  794. { make both the same type or give an error. This is not
  795. done when both are integer values, because typecasting
  796. between -3200..3200 will result in a signed-unsigned
  797. conflict and give a range check error (PFV) }
  798. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  799. inserttypeconv(pt1,pt2.resultdef);
  800. { both must be evaluated to constants now }
  801. if (pt1.nodetype=ordconstn) and
  802. (pt2.nodetype=ordconstn) then
  803. begin
  804. lv:=tordconstnode(pt1).value;
  805. hv:=tordconstnode(pt2).value;
  806. { Check bounds }
  807. if hv<lv then
  808. message(parser_e_upper_lower_than_lower)
  809. else if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  810. message(type_e_cant_eval_constant_expr)
  811. else
  812. begin
  813. { All checks passed, create the new def }
  814. case pt1.resultdef.typ of
  815. enumdef :
  816. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  817. orddef :
  818. begin
  819. if is_char(pt1.resultdef) then
  820. def:=torddef.create(uchar,lv,hv)
  821. else
  822. if is_boolean(pt1.resultdef) then
  823. def:=torddef.create(pasbool8,lv,hv)
  824. else if is_signed(pt1.resultdef) then
  825. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  826. else
  827. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  828. end;
  829. end;
  830. end;
  831. end
  832. else
  833. Message(sym_e_error_in_type_def);
  834. pt2.free;
  835. end
  836. else
  837. begin
  838. { a simple type renaming or generic specialization }
  839. if (pt1.nodetype=typen) then
  840. begin
  841. def:=ttypenode(pt1).resultdef;
  842. { Delphi mode specialization? }
  843. if (m_delphi in current_settings.modeswitches) then
  844. dospecialize:=token=_LSHARPBRACKET
  845. else
  846. { in non-Delphi modes we might get a inline specialization
  847. without "specialize" or "<T>" of the same type we're
  848. currently parsing, so we need to handle that special }
  849. newdef:=nil;
  850. if not dospecialize and
  851. assigned(ttypenode(pt1).typesym) and
  852. (ttypenode(pt1).typesym.typ=typesym) and
  853. (sp_generic_dummy in ttypenode(pt1).typesym.symoptions) and
  854. assigned(current_structdef) and
  855. (
  856. (
  857. not (m_delphi in current_settings.modeswitches) and
  858. (ttypesym(ttypenode(pt1).typesym).typedef.typ=undefineddef) and
  859. (df_generic in current_structdef.defoptions) and
  860. (ttypesym(ttypenode(pt1).typesym).typedef.owner=current_structdef.owner) and
  861. (upper(ttypenode(pt1).typesym.realname)=copy(current_structdef.objname^,1,pos('$',current_structdef.objname^)-1))
  862. ) or (
  863. { this could be a nested specialization which uses
  864. the type name of a surrounding generic to
  865. reference the specialization of said surrounding
  866. class }
  867. (df_specialization in current_structdef.defoptions) and
  868. return_specialization_of_generic(current_structdef,ttypesym(ttypenode(pt1).typesym).typedef,newdef)
  869. )
  870. )
  871. then
  872. begin
  873. if assigned(newdef) then
  874. def:=newdef
  875. else
  876. def:=current_structdef;
  877. if assigned(def) then
  878. { handle nested types }
  879. post_comp_expr_gendef(def)
  880. else
  881. def:=generrordef;
  882. end;
  883. if dospecialize then
  884. begin
  885. generate_specialization(def,false,name,nil,'');
  886. { handle nested types }
  887. if assigned(def) then
  888. post_comp_expr_gendef(def);
  889. end
  890. else
  891. begin
  892. if assigned(current_specializedef) and (def=current_specializedef.genericdef) then
  893. begin
  894. def:=current_specializedef
  895. end
  896. else if (def=current_genericdef) then
  897. begin
  898. def:=current_genericdef
  899. end
  900. else if (df_generic in def.defoptions) and
  901. { TODO : check once nested generics are allowed }
  902. not
  903. (
  904. parse_generic and
  905. (current_genericdef.typ in [recorddef,objectdef]) and
  906. (def.typ in [recorddef,objectdef]) and
  907. (
  908. { if both defs belong to the same generic (e.g. both are
  909. subtypes) then we must allow the usage }
  910. defs_belong_to_same_generic(def,current_genericdef) or
  911. { this is needed to correctly resolve "type Foo=SomeGeneric<T>"
  912. declarations inside a generic }
  913. (
  914. (ttypenode(pt1).typesym<>nil) and
  915. sym_is_owned_by(ttypenode(pt1).typesym,tabstractrecorddef(current_genericdef).symtable)
  916. )
  917. )
  918. )
  919. then
  920. begin
  921. Message(parser_e_no_generics_as_types);
  922. def:=generrordef;
  923. end
  924. else if is_classhelper(def) then
  925. begin
  926. Message(parser_e_no_category_as_types);
  927. def:=generrordef
  928. end
  929. end;
  930. end
  931. else
  932. Message(sym_e_error_in_type_def);
  933. end;
  934. pt1.free;
  935. block_type:=old_block_type;
  936. end;
  937. procedure set_dec;
  938. begin
  939. consume(_SET);
  940. consume(_OF);
  941. read_anon_type(tt2,true);
  942. if assigned(tt2) then
  943. begin
  944. case tt2.typ of
  945. { don't forget that min can be negativ PM }
  946. enumdef :
  947. if (tenumdef(tt2).min>=0) and
  948. (tenumdef(tt2).max<=255) then
  949. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  950. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  951. else
  952. Message(sym_e_ill_type_decl_set);
  953. orddef :
  954. begin
  955. if (torddef(tt2).ordtype<>uvoid) and
  956. (torddef(tt2).ordtype<>uwidechar) and
  957. (torddef(tt2).low>=0) then
  958. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  959. if Torddef(tt2).high>int64(high(byte)) then
  960. message(sym_e_ill_type_decl_set)
  961. else
  962. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  963. else
  964. Message(sym_e_ill_type_decl_set);
  965. end;
  966. else
  967. Message(sym_e_ill_type_decl_set);
  968. end;
  969. end
  970. else
  971. def:=generrordef;
  972. end;
  973. procedure array_dec(is_packed:boolean;genericdef:tstoreddef;genericlist:TFPObjectList);
  974. var
  975. lowval,
  976. highval : TConstExprInt;
  977. indexdef : tdef;
  978. hdef : tdef;
  979. arrdef : tarraydef;
  980. procedure setdefdecl(def:tdef);
  981. begin
  982. case def.typ of
  983. enumdef :
  984. begin
  985. lowval:=tenumdef(def).min;
  986. highval:=tenumdef(def).max;
  987. if (m_fpc in current_settings.modeswitches) and
  988. (tenumdef(def).has_jumps) then
  989. Message(type_e_array_index_enums_with_assign_not_possible);
  990. indexdef:=def;
  991. end;
  992. orddef :
  993. begin
  994. if torddef(def).ordtype in [uchar,
  995. u8bit,u16bit,
  996. s8bit,s16bit,s32bit,
  997. {$ifdef cpu64bitaddr}
  998. u32bit,s64bit,
  999. {$endif cpu64bitaddr}
  1000. pasbool8,pasbool16,pasbool32,pasbool64,
  1001. bool8bit,bool16bit,bool32bit,bool64bit,
  1002. uwidechar] then
  1003. begin
  1004. lowval:=torddef(def).low;
  1005. highval:=torddef(def).high;
  1006. indexdef:=def;
  1007. end
  1008. else
  1009. Message1(parser_e_type_cant_be_used_in_array_index,def.typename);
  1010. end;
  1011. else
  1012. Message(sym_e_error_in_type_def);
  1013. end;
  1014. end;
  1015. var
  1016. old_current_genericdef,
  1017. old_current_specializedef: tstoreddef;
  1018. first,
  1019. old_parse_generic: boolean;
  1020. begin
  1021. old_current_genericdef:=current_genericdef;
  1022. old_current_specializedef:=current_specializedef;
  1023. old_parse_generic:=parse_generic;
  1024. current_genericdef:=nil;
  1025. current_specializedef:=nil;
  1026. first:=true;
  1027. arrdef:=tarraydef.create(0,0,s32inttype);
  1028. consume(_ARRAY);
  1029. { usage of specialized type inside its generic template }
  1030. if assigned(genericdef) then
  1031. current_specializedef:=arrdef
  1032. { reject declaration of generic class inside generic class }
  1033. else if assigned(genericlist) then
  1034. current_genericdef:=arrdef;
  1035. symtablestack.push(arrdef.symtable);
  1036. insert_generic_parameter_types(arrdef,genericdef,genericlist);
  1037. { there are two possibilties for the following to be true:
  1038. * the array declaration itself is generic
  1039. * the array is declared inside a generic
  1040. in both cases we need "parse_generic" and "current_genericdef"
  1041. so that e.g. specializations of another generic inside the
  1042. current generic can be used (either inline ones or "type" ones) }
  1043. parse_generic:=(df_generic in arrdef.defoptions) or old_parse_generic;
  1044. if parse_generic and not assigned(current_genericdef) then
  1045. current_genericdef:=old_current_genericdef;
  1046. { open array? }
  1047. if try_to_consume(_LECKKLAMMER) then
  1048. begin
  1049. { defaults }
  1050. indexdef:=generrordef;
  1051. { use defaults which don't overflow the compiler }
  1052. lowval:=0;
  1053. highval:=0;
  1054. repeat
  1055. { read the expression and check it, check apart if the
  1056. declaration is an enum declaration because that needs to
  1057. be parsed by readtype (PFV) }
  1058. if token=_LKLAMMER then
  1059. begin
  1060. read_anon_type(hdef,true);
  1061. setdefdecl(hdef);
  1062. end
  1063. else
  1064. begin
  1065. pt:=expr(true);
  1066. if pt.nodetype=typen then
  1067. setdefdecl(pt.resultdef)
  1068. else
  1069. begin
  1070. if pt.nodetype=rangen then
  1071. begin
  1072. { check the expression only if we are not in a generic declaration }
  1073. if not(parse_generic) then
  1074. begin
  1075. if (trangenode(pt).left.nodetype=ordconstn) and
  1076. (trangenode(pt).right.nodetype=ordconstn) then
  1077. begin
  1078. { make both the same type or give an error. This is not
  1079. done when both are integer values, because typecasting
  1080. between -3200..3200 will result in a signed-unsigned
  1081. conflict and give a range check error (PFV) }
  1082. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  1083. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  1084. lowval:=tordconstnode(trangenode(pt).left).value;
  1085. highval:=tordconstnode(trangenode(pt).right).value;
  1086. if highval<lowval then
  1087. begin
  1088. Message(parser_e_array_lower_less_than_upper_bound);
  1089. highval:=lowval;
  1090. end
  1091. else if (lowval<int64(low(asizeint))) or
  1092. (highval>high(asizeint)) then
  1093. begin
  1094. Message(parser_e_array_range_out_of_bounds);
  1095. lowval :=0;
  1096. highval:=0;
  1097. end;
  1098. if is_integer(trangenode(pt).left.resultdef) then
  1099. range_to_type(lowval,highval,indexdef)
  1100. else
  1101. indexdef:=trangenode(pt).left.resultdef;
  1102. end
  1103. else
  1104. Message(type_e_cant_eval_constant_expr);
  1105. end;
  1106. end
  1107. else
  1108. Message(sym_e_error_in_type_def)
  1109. end;
  1110. pt.free;
  1111. end;
  1112. { if we are not at the first dimension, add the new arrray
  1113. as element of the existing array, otherwise modify the existing array }
  1114. if not(first) then
  1115. begin
  1116. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  1117. { push new symtable }
  1118. symtablestack.pop(arrdef.symtable);
  1119. arrdef:=tarraydef(arrdef.elementdef);
  1120. symtablestack.push(arrdef.symtable);
  1121. end
  1122. else
  1123. begin
  1124. arrdef.lowrange:=lowval.svalue;
  1125. arrdef.highrange:=highval.svalue;
  1126. arrdef.rangedef:=indexdef;
  1127. def:=arrdef;
  1128. first:=false;
  1129. end;
  1130. if is_packed then
  1131. include(arrdef.arrayoptions,ado_IsBitPacked);
  1132. if token=_COMMA then
  1133. consume(_COMMA)
  1134. else
  1135. break;
  1136. until false;
  1137. consume(_RECKKLAMMER);
  1138. end
  1139. else
  1140. begin
  1141. if is_packed then
  1142. Message(parser_e_packed_dynamic_open_array);
  1143. arrdef.lowrange:=0;
  1144. arrdef.highrange:=-1;
  1145. arrdef.rangedef:=s32inttype;
  1146. include(arrdef.arrayoptions,ado_IsDynamicArray);
  1147. def:=arrdef;
  1148. end;
  1149. consume(_OF);
  1150. read_anon_type(tt2,true);
  1151. { set element type of the last array definition }
  1152. if assigned(arrdef) then
  1153. begin
  1154. symtablestack.pop(arrdef.symtable);
  1155. arrdef.elementdef:=tt2;
  1156. if is_packed and
  1157. is_managed_type(tt2) then
  1158. Message(type_e_no_packed_inittable);
  1159. end;
  1160. { restore old state }
  1161. parse_generic:=old_parse_generic;
  1162. current_genericdef:=old_current_genericdef;
  1163. current_specializedef:=old_current_specializedef;
  1164. end;
  1165. function procvar_dec(genericdef:tstoreddef;genericlist:TFPObjectList):tdef;
  1166. var
  1167. is_func:boolean;
  1168. pd:tabstractprocdef;
  1169. newtype:ttypesym;
  1170. old_current_genericdef,
  1171. old_current_specializedef: tstoreddef;
  1172. old_parse_generic: boolean;
  1173. begin
  1174. old_current_genericdef:=current_genericdef;
  1175. old_current_specializedef:=current_specializedef;
  1176. old_parse_generic:=parse_generic;
  1177. current_genericdef:=nil;
  1178. current_specializedef:=nil;
  1179. is_func:=(token=_FUNCTION);
  1180. consume(token);
  1181. pd:=tprocvardef.create(normal_function_level);
  1182. { usage of specialized type inside its generic template }
  1183. if assigned(genericdef) then
  1184. current_specializedef:=pd
  1185. { reject declaration of generic class inside generic class }
  1186. else if assigned(genericlist) then
  1187. current_genericdef:=pd;
  1188. symtablestack.push(pd.parast);
  1189. insert_generic_parameter_types(pd,genericdef,genericlist);
  1190. { there are two possibilties for the following to be true:
  1191. * the procvar declaration itself is generic
  1192. * the procvar is declared inside a generic
  1193. in both cases we need "parse_generic" and "current_genericdef"
  1194. so that e.g. specializations of another generic inside the
  1195. current generic can be used (either inline ones or "type" ones) }
  1196. parse_generic:=(df_generic in pd.defoptions) or old_parse_generic;
  1197. if parse_generic and not assigned(current_genericdef) then
  1198. current_genericdef:=old_current_genericdef;
  1199. { don't allow to add defs to the symtable - use it for type param search only }
  1200. tparasymtable(pd.parast).readonly:=true;
  1201. if token=_LKLAMMER then
  1202. parse_parameter_dec(pd);
  1203. if is_func then
  1204. begin
  1205. consume(_COLON);
  1206. single_type(pd.returndef,[]);
  1207. end;
  1208. if try_to_consume(_OF) then
  1209. begin
  1210. consume(_OBJECT);
  1211. include(pd.procoptions,po_methodpointer);
  1212. end
  1213. else if (m_nested_procvars in current_settings.modeswitches) and
  1214. try_to_consume(_IS) then
  1215. begin
  1216. consume(_NESTED);
  1217. pd.parast.symtablelevel:=normal_function_level+1;
  1218. pd.check_mark_as_nested;
  1219. end;
  1220. symtablestack.pop(pd.parast);
  1221. tparasymtable(pd.parast).readonly:=false;
  1222. result:=pd;
  1223. { possible proc directives }
  1224. if parseprocvardir then
  1225. begin
  1226. if check_proc_directive(true) then
  1227. begin
  1228. newtype:=ttypesym.create('unnamed',result);
  1229. parse_var_proc_directives(tsym(newtype));
  1230. newtype.typedef:=nil;
  1231. result.typesym:=nil;
  1232. newtype.free;
  1233. end;
  1234. { Add implicit hidden parameters and function result }
  1235. handle_calling_convention(pd);
  1236. end;
  1237. { restore old state }
  1238. parse_generic:=old_parse_generic;
  1239. current_genericdef:=old_current_genericdef;
  1240. current_specializedef:=old_current_specializedef;
  1241. end;
  1242. const
  1243. SingleTypeOptionsInTypeBlock:array[Boolean] of TSingleTypeOptions = ([],[stoIsForwardDef]);
  1244. var
  1245. p : tnode;
  1246. hdef : tdef;
  1247. enumdupmsg, first, is_specialize : boolean;
  1248. oldlocalswitches : tlocalswitches;
  1249. bitpacking: boolean;
  1250. stitem: psymtablestackitem;
  1251. sym: tsym;
  1252. st: tsymtable;
  1253. begin
  1254. def:=nil;
  1255. if assigned(newsym) then
  1256. name:=newsym.RealName
  1257. else
  1258. name:='';
  1259. case token of
  1260. _STRING,_FILE:
  1261. begin
  1262. single_type(def,[stoAllowTypeDef]);
  1263. end;
  1264. _LKLAMMER:
  1265. begin
  1266. consume(_LKLAMMER);
  1267. first:=true;
  1268. { allow negativ value_str }
  1269. l:=int64(-1);
  1270. enumdupmsg:=false;
  1271. { check that we are not adding an enum from specialization
  1272. we can't just use current_specializedef because of inner types
  1273. like specialize array of record }
  1274. is_specialize:=false;
  1275. stitem:=symtablestack.stack;
  1276. while assigned(stitem) do
  1277. begin
  1278. { check records, classes and arrays because they can be specialized }
  1279. if stitem^.symtable.symtabletype in [recordsymtable,ObjectSymtable,arraysymtable] then
  1280. begin
  1281. is_specialize:=is_specialize or (df_specialization in tstoreddef(stitem^.symtable.defowner).defoptions);
  1282. stitem:=stitem^.next;
  1283. end
  1284. else
  1285. break;
  1286. end;
  1287. if not is_specialize then
  1288. aktenumdef:=tenumdef.create
  1289. else
  1290. aktenumdef:=nil;
  1291. repeat
  1292. { if it is a specialization then search the first enum member
  1293. and get the member owner instead of just created enumdef }
  1294. if not assigned(aktenumdef) then
  1295. begin
  1296. searchsym(pattern,sym,st);
  1297. if sym.typ=enumsym then
  1298. aktenumdef:=tenumsym(sym).definition
  1299. else
  1300. internalerror(201101021);
  1301. end;
  1302. s:=orgpattern;
  1303. defpos:=current_tokenpos;
  1304. consume(_ID);
  1305. { only allow assigning of specific numbers under fpc mode }
  1306. if not(m_tp7 in current_settings.modeswitches) and
  1307. (
  1308. { in fpc mode also allow := to be compatible
  1309. with previous 1.0.x versions }
  1310. ((m_fpc in current_settings.modeswitches) and
  1311. try_to_consume(_ASSIGNMENT)) or
  1312. try_to_consume(_EQ)
  1313. ) then
  1314. begin
  1315. oldlocalswitches:=current_settings.localswitches;
  1316. include(current_settings.localswitches,cs_allow_enum_calc);
  1317. p:=comp_expr(true,false);
  1318. current_settings.localswitches:=oldlocalswitches;
  1319. if (p.nodetype=ordconstn) then
  1320. begin
  1321. { we expect an integer or an enum of the
  1322. same type }
  1323. if is_integer(p.resultdef) or
  1324. is_char(p.resultdef) or
  1325. equal_defs(p.resultdef,aktenumdef) then
  1326. v:=tordconstnode(p).value
  1327. else
  1328. IncompatibleTypes(p.resultdef,s32inttype);
  1329. end
  1330. else
  1331. Message(parser_e_illegal_expression);
  1332. p.free;
  1333. { please leave that a note, allows type save }
  1334. { declarations in the win32 units ! }
  1335. if (not first) and (v<=l) and (not enumdupmsg) then
  1336. begin
  1337. Message(parser_n_duplicate_enum);
  1338. enumdupmsg:=true;
  1339. end;
  1340. l:=v;
  1341. end
  1342. else
  1343. inc(l.svalue);
  1344. first:=false;
  1345. { don't generate enum members is this is a specialization because aktenumdef is copied from the generic type }
  1346. if not is_specialize then
  1347. begin
  1348. storepos:=current_tokenpos;
  1349. current_tokenpos:=defpos;
  1350. tenumsymtable(aktenumdef.symtable).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1351. if not (cs_scopedenums in current_settings.localswitches) then
  1352. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,longint(l.svalue)));
  1353. current_tokenpos:=storepos;
  1354. end;
  1355. until not try_to_consume(_COMMA);
  1356. def:=aktenumdef;
  1357. consume(_RKLAMMER);
  1358. {$ifdef jvm}
  1359. jvm_maybe_create_enum_class(name,def);
  1360. {$endif}
  1361. end;
  1362. _ARRAY:
  1363. begin
  1364. array_dec(false,genericdef,genericlist);
  1365. end;
  1366. _SET:
  1367. begin
  1368. set_dec;
  1369. end;
  1370. _CARET:
  1371. begin
  1372. consume(_CARET);
  1373. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1374. { don't use getpointerdef() here, since this is a type
  1375. declaration (-> must create new typedef) }
  1376. def:=tpointerdef.create(tt2);
  1377. if tt2.typ=forwarddef then
  1378. current_module.checkforwarddefs.add(def);
  1379. end;
  1380. _RECORD:
  1381. begin
  1382. consume(token);
  1383. if (idtoken=_HELPER) and (m_advanced_records in current_settings.modeswitches) then
  1384. begin
  1385. consume(_HELPER);
  1386. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_record);
  1387. end
  1388. else
  1389. def:=record_dec(name,genericdef,genericlist);
  1390. end;
  1391. _PACKED,
  1392. _BITPACKED:
  1393. begin
  1394. bitpacking :=
  1395. (cs_bitpacking in current_settings.localswitches) or
  1396. (token = _BITPACKED);
  1397. consume(token);
  1398. if token=_ARRAY then
  1399. array_dec(bitpacking,genericdef,genericlist)
  1400. else if token=_SET then
  1401. set_dec
  1402. else if token=_FILE then
  1403. single_type(def,[stoAllowTypeDef])
  1404. else
  1405. begin
  1406. oldpackrecords:=current_settings.packrecords;
  1407. if (not bitpacking) or
  1408. (token in [_CLASS,_OBJECT]) then
  1409. current_settings.packrecords:=1
  1410. else
  1411. current_settings.packrecords:=bit_alignment;
  1412. case token of
  1413. _CLASS :
  1414. begin
  1415. consume(_CLASS);
  1416. def:=object_dec(odt_class,name,newsym,genericdef,genericlist,nil,ht_none);
  1417. end;
  1418. _OBJECT :
  1419. begin
  1420. consume(_OBJECT);
  1421. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1422. end;
  1423. else begin
  1424. consume(_RECORD);
  1425. def:=record_dec(name,genericdef,genericlist);
  1426. end;
  1427. end;
  1428. current_settings.packrecords:=oldpackrecords;
  1429. end;
  1430. end;
  1431. _DISPINTERFACE :
  1432. begin
  1433. { need extra check here since interface is a keyword
  1434. in all pascal modes }
  1435. if not(m_class in current_settings.modeswitches) then
  1436. Message(parser_f_need_objfpc_or_delphi_mode);
  1437. consume(token);
  1438. def:=object_dec(odt_dispinterface,name,newsym,genericdef,genericlist,nil,ht_none);
  1439. end;
  1440. _CLASS :
  1441. begin
  1442. consume(token);
  1443. { Delphi only allows class of in type blocks }
  1444. if (token=_OF) and
  1445. (
  1446. not(m_delphi in current_settings.modeswitches) or
  1447. (block_type=bt_type)
  1448. ) then
  1449. begin
  1450. consume(_OF);
  1451. single_type(hdef,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1452. if is_class(hdef) or
  1453. is_objcclass(hdef) or
  1454. is_javaclass(hdef) then
  1455. def:=tclassrefdef.create(hdef)
  1456. else
  1457. if hdef.typ=forwarddef then
  1458. begin
  1459. def:=tclassrefdef.create(hdef);
  1460. current_module.checkforwarddefs.add(def);
  1461. end
  1462. else
  1463. Message1(type_e_class_or_objcclass_type_expected,hdef.typename);
  1464. end
  1465. else
  1466. if (idtoken=_HELPER) then
  1467. begin
  1468. consume(_HELPER);
  1469. def:=object_dec(odt_helper,name,newsym,genericdef,genericlist,nil,ht_class);
  1470. end
  1471. else
  1472. def:=object_dec(default_class_type,name,newsym,genericdef,genericlist,nil,ht_none);
  1473. end;
  1474. _CPPCLASS :
  1475. begin
  1476. consume(token);
  1477. def:=object_dec(odt_cppclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1478. end;
  1479. _OBJCCLASS :
  1480. begin
  1481. if not(m_objectivec1 in current_settings.modeswitches) then
  1482. Message(parser_f_need_objc);
  1483. consume(token);
  1484. def:=object_dec(odt_objcclass,name,newsym,genericdef,genericlist,nil,ht_none);
  1485. end;
  1486. _INTERFACE :
  1487. begin
  1488. { need extra check here since interface is a keyword
  1489. in all pascal modes }
  1490. if not(m_class in current_settings.modeswitches) then
  1491. Message(parser_f_need_objfpc_or_delphi_mode);
  1492. consume(token);
  1493. case current_settings.interfacetype of
  1494. it_interfacecom:
  1495. def:=object_dec(odt_interfacecom,name,newsym,genericdef,genericlist,nil,ht_none);
  1496. it_interfacecorba:
  1497. def:=object_dec(odt_interfacecorba,name,newsym,genericdef,genericlist,nil,ht_none);
  1498. it_interfacejava:
  1499. def:=object_dec(odt_interfacejava,name,newsym,genericdef,genericlist,nil,ht_none);
  1500. else
  1501. internalerror(2010122612);
  1502. end;
  1503. end;
  1504. _OBJCPROTOCOL :
  1505. begin
  1506. if not(m_objectivec1 in current_settings.modeswitches) then
  1507. Message(parser_f_need_objc);
  1508. consume(token);
  1509. def:=object_dec(odt_objcprotocol,name,newsym,genericdef,genericlist,nil,ht_none);
  1510. end;
  1511. _OBJCCATEGORY :
  1512. begin
  1513. if not(m_objectivec1 in current_settings.modeswitches) then
  1514. Message(parser_f_need_objc);
  1515. consume(token);
  1516. def:=object_dec(odt_objccategory,name,newsym,genericdef,genericlist,nil,ht_none);
  1517. end;
  1518. _OBJECT :
  1519. begin
  1520. consume(token);
  1521. def:=object_dec(odt_object,name,newsym,genericdef,genericlist,nil,ht_none);
  1522. end;
  1523. _PROCEDURE,
  1524. _FUNCTION:
  1525. begin
  1526. def:=procvar_dec(genericdef,genericlist);
  1527. {$ifdef jvm}
  1528. jvm_create_procvar_class(name,def);
  1529. {$endif}
  1530. end;
  1531. else
  1532. if (token=_KLAMMERAFFE) and (m_iso in current_settings.modeswitches) then
  1533. begin
  1534. consume(_KLAMMERAFFE);
  1535. single_type(tt2,SingleTypeOptionsInTypeBlock[block_type=bt_type]);
  1536. def:=tpointerdef.create(tt2);
  1537. if tt2.typ=forwarddef then
  1538. current_module.checkforwarddefs.add(def);
  1539. end
  1540. else
  1541. expr_type;
  1542. end;
  1543. if def=nil then
  1544. def:=generrordef;
  1545. end;
  1546. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  1547. begin
  1548. read_named_type(def,nil,nil,nil,parseprocvardir);
  1549. end;
  1550. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  1551. var
  1552. i : longint;
  1553. def : tdef;
  1554. vmtwriter : TVMTWriter;
  1555. begin
  1556. {$ifdef jvm}
  1557. { no Delphi-style RTTI }
  1558. exit;
  1559. {$endif jvm}
  1560. for i:=0 to st.DefList.Count-1 do
  1561. begin
  1562. def:=tdef(st.DefList[i]);
  1563. case def.typ of
  1564. recorddef :
  1565. write_persistent_type_info(trecorddef(def).symtable,is_global);
  1566. objectdef :
  1567. begin
  1568. { Skip generics and forward defs }
  1569. if (df_generic in def.defoptions) or
  1570. (oo_is_forward in tobjectdef(def).objectoptions) then
  1571. continue;
  1572. write_persistent_type_info(tobjectdef(def).symtable,is_global);
  1573. { Write also VMT if not done yet }
  1574. if not(ds_vmt_written in def.defstates) then
  1575. begin
  1576. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  1577. if is_interface(tobjectdef(def)) then
  1578. vmtwriter.writeinterfaceids;
  1579. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  1580. vmtwriter.writevmt;
  1581. vmtwriter.free;
  1582. include(def.defstates,ds_vmt_written);
  1583. end;
  1584. end;
  1585. procdef :
  1586. begin
  1587. if assigned(tprocdef(def).localst) and
  1588. (tprocdef(def).localst.symtabletype=localsymtable) then
  1589. write_persistent_type_info(tprocdef(def).localst,false);
  1590. if assigned(tprocdef(def).parast) then
  1591. write_persistent_type_info(tprocdef(def).parast,false);
  1592. end;
  1593. end;
  1594. { generate always persistent tables for types in the interface so it can
  1595. be reused in other units and give always the same pointer location. }
  1596. { Init }
  1597. if (
  1598. assigned(def.typesym) and
  1599. is_global and
  1600. not is_objc_class_or_protocol(def)
  1601. ) or
  1602. is_managed_type(def) or
  1603. (ds_init_table_used in def.defstates) then
  1604. RTTIWriter.write_rtti(def,initrtti);
  1605. { RTTI }
  1606. if (
  1607. assigned(def.typesym) and
  1608. is_global and
  1609. not is_objc_class_or_protocol(def)
  1610. ) or
  1611. (ds_rtti_table_used in def.defstates) then
  1612. RTTIWriter.write_rtti(def,fullrtti);
  1613. end;
  1614. end;
  1615. procedure add_typedconst_init_routine(def: tabstractrecorddef);
  1616. var
  1617. sstate: tscannerstate;
  1618. pd: tprocdef;
  1619. begin
  1620. replace_scanner('tcinit_routine',sstate);
  1621. { the typed constant initialization code is called from the class
  1622. constructor by tnodeutils.wrap_proc_body; at this point, we don't
  1623. know yet whether that will be necessary, because there may be
  1624. typed constants inside method bodies -> always force the addition
  1625. of a class constructor.
  1626. We cannot directly add the typed constant initialisations to the
  1627. class constructor, because when it's parsed not all method bodies
  1628. are necessarily already parsed }
  1629. pd:=def.find_procdef_bytype(potype_class_constructor);
  1630. { the class constructor }
  1631. if not assigned(pd) then
  1632. begin
  1633. if str_parse_method_dec('constructor fpc_init_typed_consts_class_constructor;',potype_class_constructor,true,def,pd) then
  1634. pd.synthetickind:=tsk_empty
  1635. else
  1636. internalerror(2011040206);
  1637. end;
  1638. { the initialisation helper }
  1639. if str_parse_method_dec('procedure fpc_init_typed_consts_helper; static;',potype_procedure,true,def,pd) then
  1640. pd.synthetickind:=tsk_tcinit
  1641. else
  1642. internalerror(2011040207);
  1643. restore_scanner(sstate);
  1644. end;
  1645. end.