ptype.pas 74 KB

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