ptype.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. const
  24. { forward types should only be possible inside a TYPE statement }
  25. typecanbeforward : boolean = false;
  26. var
  27. { hack, which allows to use the current parsed }
  28. { object type as function argument type }
  29. testcurobject : byte;
  30. { reads a type identifier }
  31. procedure id_type(var def : tdef;isforwarddef:boolean);
  32. { reads a string, file type or a type identifier }
  33. procedure single_type(var def:tdef;isforwarddef:boolean);
  34. { reads any type declaration, where the resulting type will get name as type identifier }
  35. procedure read_named_type(var def:tdef;const name : TIDString;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);
  40. implementation
  41. uses
  42. { common }
  43. cutils,
  44. { global }
  45. globals,tokens,verbose,
  46. systems,
  47. { target }
  48. paramgr,procinfo,
  49. { symtable }
  50. symconst,symsym,symtable,
  51. defutil,defcmp,
  52. { modules }
  53. fmodule,
  54. { pass 1 }
  55. node,ncgrtti,nobj,
  56. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  57. { parser }
  58. scanner,
  59. pbase,pexpr,pdecsub,pdecvar,pdecobj;
  60. procedure generate_specialization(var tt:tdef);
  61. var
  62. st : TSymtable;
  63. srsym : tsym;
  64. pt2 : tnode;
  65. first,
  66. err : boolean;
  67. i : longint;
  68. sym : tsym;
  69. old_block_type : tblock_type;
  70. genericdef : tstoreddef;
  71. generictype : ttypesym;
  72. generictypelist : TFPObjectList;
  73. oldsymtablestack : tsymtablestack;
  74. hmodule : tmodule;
  75. pu : tused_unit;
  76. uspecializename,
  77. specializename : string;
  78. vmtbuilder : TVMTBuilder;
  79. onlyparsepara : boolean;
  80. specializest : tsymtable;
  81. begin
  82. { retrieve generic def that we are going to replace }
  83. genericdef:=tstoreddef(tt);
  84. tt:=nil;
  85. onlyparsepara:=false;
  86. if not(df_generic in genericdef.defoptions) then
  87. begin
  88. Message(parser_e_special_onlygenerics);
  89. tt:=generrordef;
  90. onlyparsepara:=true;
  91. end;
  92. { Only need to record the tokens, then we don't know the type yet }
  93. if parse_generic then
  94. begin
  95. tt:=cundefinedtype;
  96. onlyparsepara:=true;
  97. end;
  98. { Only parse the parameters for recovery or
  99. for recording in genericbuf }
  100. if onlyparsepara then
  101. begin
  102. consume(_LSHARPBRACKET);
  103. repeat
  104. pt2:=factor(false);
  105. pt2.free;
  106. until not try_to_consume(_COMMA);
  107. consume(_RSHARPBRACKET);
  108. exit;
  109. end;
  110. consume(_LSHARPBRACKET);
  111. old_block_type:=block_type;
  112. block_type:=bt_specialize;
  113. { Parse generic parameters, for each undefineddef in the symtable of
  114. the genericdef we need to have a new def }
  115. err:=false;
  116. first:=true;
  117. generictypelist:=TFPObjectList.create(false);
  118. case genericdef.typ of
  119. procdef :
  120. st:=genericdef.GetSymtable(gs_para);
  121. objectdef,
  122. recorddef :
  123. st:=genericdef.GetSymtable(gs_record);
  124. end;
  125. if not assigned(st) then
  126. internalerror(200511182);
  127. { Parse type parameters }
  128. if not assigned(genericdef.typesym) then
  129. internalerror(200710173);
  130. specializename:=genericdef.typesym.realname;
  131. for i:=0 to st.SymList.Count-1 do
  132. begin
  133. sym:=tsym(st.SymList[i]);
  134. if (sp_generic_para in sym.symoptions) then
  135. begin
  136. if not first then
  137. consume(_COMMA)
  138. else
  139. first:=false;
  140. pt2:=factor(false);
  141. if pt2.nodetype=typen then
  142. begin
  143. if df_generic in pt2.resultdef.defoptions then
  144. Message(parser_e_no_generics_as_params);
  145. generictype:=ttypesym.create(sym.realname,pt2.resultdef);
  146. generictypelist.add(generictype);
  147. if not assigned(pt2.resultdef.typesym) then
  148. internalerror(200710172);
  149. specializename:=specializename+'$'+pt2.resultdef.typesym.realname;
  150. end
  151. else
  152. begin
  153. Message(type_e_type_id_expected);
  154. err:=true;
  155. end;
  156. pt2.free;
  157. end;
  158. end;
  159. uspecializename:=upper(specializename);
  160. { force correct error location if too much type parameters are passed }
  161. if token<>_RSHARPBRACKET then
  162. consume(_RSHARPBRACKET);
  163. { Special case if we are referencing the current defined object }
  164. if assigned(aktobjectdef) and
  165. (aktobjectdef.objname^=uspecializename) then
  166. tt:=aktobjectdef;
  167. { for units specializations can already be needed in the interface, therefor we
  168. will use the global symtable. Programs don't have a globalsymtable and there we
  169. use the localsymtable }
  170. if current_module.is_unit then
  171. specializest:=current_module.globalsymtable
  172. else
  173. specializest:=current_module.localsymtable;
  174. { Can we reuse an already specialized type? }
  175. if not assigned(tt) then
  176. begin
  177. srsym:=tsym(specializest.find(uspecializename));
  178. if assigned(srsym) then
  179. begin
  180. if srsym.typ<>typesym then
  181. internalerror(200710171);
  182. tt:=ttypesym(srsym).typedef;
  183. end;
  184. end;
  185. if not assigned(tt) then
  186. begin
  187. { Setup symtablestack at definition time
  188. to get types right, however this is not perfect, we should probably record
  189. the resolved symbols }
  190. oldsymtablestack:=symtablestack;
  191. symtablestack:=tsymtablestack.create;
  192. if not assigned(genericdef) then
  193. internalerror(200705151);
  194. hmodule:=find_module_from_symtable(genericdef.owner);
  195. if hmodule=nil then
  196. internalerror(200705152);
  197. pu:=tused_unit(hmodule.used_units.first);
  198. while assigned(pu) do
  199. begin
  200. if not assigned(pu.u.globalsymtable) then
  201. internalerror(200705153);
  202. symtablestack.push(pu.u.globalsymtable);
  203. pu:=tused_unit(pu.next);
  204. end;
  205. if assigned(hmodule.globalsymtable) then
  206. symtablestack.push(hmodule.globalsymtable);
  207. { hacky, but necessary to insert the newly generated class properly }
  208. symtablestack.push(oldsymtablestack.top);
  209. { Reparse the original type definition }
  210. if not err then
  211. begin
  212. { Firsta new typesym so we can reuse this specialization and
  213. references to this specialization can be handled }
  214. srsym:=ttypesym.create(specializename,generrordef);
  215. specializest.insert(srsym);
  216. if not assigned(genericdef.generictokenbuf) then
  217. internalerror(200511171);
  218. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  219. read_named_type(tt,specializename,genericdef,generictypelist,false);
  220. ttypesym(srsym).typedef:=tt;
  221. tt.typesym:=srsym;
  222. { Consume the semicolon if it is also recorded }
  223. try_to_consume(_SEMICOLON);
  224. { Build VMT indexes for classes }
  225. if (tt.typ=objectdef) then
  226. begin
  227. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  228. vmtbuilder.generate_vmt;
  229. vmtbuilder.free;
  230. end;
  231. end;
  232. { Restore symtablestack }
  233. symtablestack.free;
  234. symtablestack:=oldsymtablestack;
  235. end;
  236. generictypelist.free;
  237. consume(_RSHARPBRACKET);
  238. block_type:=old_block_type;
  239. end;
  240. procedure id_type(var def : tdef;isforwarddef:boolean);
  241. { reads a type definition }
  242. { to a appropriating tdef, s gets the name of }
  243. { the type to allow name mangling }
  244. var
  245. is_unit_specific : boolean;
  246. pos : tfileposinfo;
  247. srsym : tsym;
  248. srsymtable : TSymtable;
  249. s,sorg : TIDString;
  250. t : ttoken;
  251. begin
  252. s:=pattern;
  253. sorg:=orgpattern;
  254. pos:=current_tokenpos;
  255. { use of current parsed object:
  256. - classes can be used also in classes
  257. - objects can be parameters }
  258. if assigned(aktobjectdef) and
  259. (aktobjectdef.objname^=pattern) and
  260. (
  261. (testcurobject=2) or
  262. is_class_or_interface(aktobjectdef)
  263. )then
  264. begin
  265. consume(_ID);
  266. def:=aktobjectdef;
  267. exit;
  268. end;
  269. { Use the special searchsym_type that ignores records,objects and
  270. parameters }
  271. searchsym_type(s,srsym,srsymtable);
  272. { handle unit specification like System.Writeln }
  273. is_unit_specific:=try_consume_unitsym(srsym,srsymtable,t);
  274. consume(t);
  275. { Types are first defined with an error def before assigning
  276. the real type so check if it's an errordef. if so then
  277. give an error. Only check for typesyms in the current symbol
  278. table as forwarddef are not resolved directly }
  279. if assigned(srsym) and
  280. (srsym.typ=typesym) and
  281. (ttypesym(srsym).typedef.typ=errordef) then
  282. begin
  283. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  284. def:=generrordef;
  285. exit;
  286. end;
  287. { are we parsing a possible forward def ? }
  288. if isforwarddef and
  289. not(is_unit_specific) then
  290. begin
  291. def:=tforwarddef.create(s,pos);
  292. exit;
  293. end;
  294. { unknown sym ? }
  295. if not assigned(srsym) then
  296. begin
  297. Message1(sym_e_id_not_found,sorg);
  298. def:=generrordef;
  299. exit;
  300. end;
  301. { type sym ? }
  302. if (srsym.typ<>typesym) then
  303. begin
  304. Message(type_e_type_id_expected);
  305. def:=generrordef;
  306. exit;
  307. end;
  308. { Give an error when referring to an errordef }
  309. if (ttypesym(srsym).typedef.typ=errordef) then
  310. begin
  311. Message(sym_e_error_in_type_def);
  312. def:=generrordef;
  313. exit;
  314. end;
  315. def:=ttypesym(srsym).typedef;
  316. end;
  317. procedure single_type(var def:tdef;isforwarddef:boolean);
  318. var
  319. t2 : tdef;
  320. dospecialize,
  321. again : boolean;
  322. begin
  323. dospecialize:=false;
  324. repeat
  325. again:=false;
  326. case token of
  327. _STRING:
  328. string_dec(def);
  329. _FILE:
  330. begin
  331. consume(_FILE);
  332. if try_to_consume(_OF) then
  333. begin
  334. single_type(t2,false);
  335. def:=tfiledef.createtyped(t2);
  336. end
  337. else
  338. def:=cfiletype;
  339. end;
  340. _ID:
  341. begin
  342. if try_to_consume(_SPECIALIZE) then
  343. begin
  344. dospecialize:=true;
  345. again:=true;
  346. end
  347. else
  348. begin
  349. id_type(def,isforwarddef);
  350. { handle types inside classes for generics, e.g. TNode.TLongint }
  351. while (token=_POINT) do
  352. begin
  353. if parse_generic then
  354. begin
  355. consume(_POINT);
  356. consume(_ID);
  357. end
  358. else if ((def.typ=objectdef) and (df_specialization in def.defoptions)) then
  359. begin
  360. symtablestack.push(tobjectdef(def).symtable);
  361. consume(_POINT);
  362. id_type(t2,isforwarddef);
  363. symtablestack.pop(tobjectdef(def).symtable);
  364. def:=t2;
  365. end
  366. else
  367. break;
  368. end;
  369. end;
  370. end;
  371. else
  372. begin
  373. message(type_e_type_id_expected);
  374. def:=generrordef;
  375. end;
  376. end;
  377. until not again;
  378. if dospecialize then
  379. generate_specialization(def)
  380. else
  381. begin
  382. if (df_generic in def.defoptions) then
  383. begin
  384. Message(parser_e_no_generics_as_types);
  385. def:=generrordef;
  386. end;
  387. end;
  388. end;
  389. { reads a record declaration }
  390. function record_dec : tdef;
  391. var
  392. recst : trecordsymtable;
  393. storetypecanbeforward : boolean;
  394. old_object_option : tsymoptions;
  395. begin
  396. { create recdef }
  397. recst:=trecordsymtable.create(current_settings.packrecords);
  398. record_dec:=trecorddef.create(recst);
  399. { insert in symtablestack }
  400. symtablestack.push(recst);
  401. { parse record }
  402. consume(_RECORD);
  403. old_object_option:=current_object_option;
  404. current_object_option:=[sp_public];
  405. storetypecanbeforward:=typecanbeforward;
  406. { for tp7 don't allow forward types }
  407. if m_tp7 in current_settings.modeswitches then
  408. typecanbeforward:=false;
  409. read_record_fields([vd_record]);
  410. consume(_END);
  411. typecanbeforward:=storetypecanbeforward;
  412. current_object_option:=old_object_option;
  413. { make the record size aligned }
  414. recst.addalignmentpadding;
  415. { restore symtable stack }
  416. symtablestack.pop(recst);
  417. if trecorddef(record_dec).is_packed and
  418. record_dec.needs_inittable then
  419. Message(type_e_no_packed_inittable);
  420. end;
  421. { reads a type definition and returns a pointer to it }
  422. procedure read_named_type(var def : tdef;const name : TIDString;genericdef:tstoreddef;genericlist:TFPObjectList;parseprocvardir:boolean);
  423. var
  424. pt : tnode;
  425. tt2 : tdef;
  426. aktenumdef : tenumdef;
  427. s : TIDString;
  428. l,v : TConstExprInt;
  429. oldpackrecords : longint;
  430. defpos,storepos : tfileposinfo;
  431. procedure expr_type;
  432. var
  433. pt1,pt2 : tnode;
  434. lv,hv : TConstExprInt;
  435. old_block_type : tblock_type;
  436. dospecialize : boolean;
  437. begin
  438. old_block_type:=block_type;
  439. dospecialize:=false;
  440. { use of current parsed object:
  441. - classes can be used also in classes
  442. - objects can be parameters }
  443. if (token=_ID) and
  444. assigned(aktobjectdef) and
  445. (aktobjectdef.objname^=pattern) and
  446. (
  447. (testcurobject=2) or
  448. is_class_or_interface(aktobjectdef)
  449. )then
  450. begin
  451. consume(_ID);
  452. def:=aktobjectdef;
  453. exit;
  454. end;
  455. { Generate a specialization? }
  456. if try_to_consume(_SPECIALIZE) then
  457. dospecialize:=true;
  458. { we can't accept a equal in type }
  459. pt1:=comp_expr(false);
  460. if not dospecialize and
  461. try_to_consume(_POINTPOINT) then
  462. begin
  463. { get high value of range }
  464. pt2:=comp_expr(false);
  465. { make both the same type or give an error. This is not
  466. done when both are integer values, because typecasting
  467. between -3200..3200 will result in a signed-unsigned
  468. conflict and give a range check error (PFV) }
  469. if not(is_integer(pt1.resultdef) and is_integer(pt2.resultdef)) then
  470. inserttypeconv(pt1,pt2.resultdef);
  471. { both must be evaluated to constants now }
  472. if (pt1.nodetype=ordconstn) and
  473. (pt2.nodetype=ordconstn) then
  474. begin
  475. lv:=tordconstnode(pt1).value;
  476. hv:=tordconstnode(pt2).value;
  477. { Check bounds }
  478. if hv<lv then
  479. Message(parser_e_upper_lower_than_lower)
  480. else
  481. begin
  482. { All checks passed, create the new def }
  483. case pt1.resultdef.typ of
  484. enumdef :
  485. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv,hv);
  486. orddef :
  487. begin
  488. if is_char(pt1.resultdef) then
  489. def:=torddef.create(uchar,lv,hv)
  490. else
  491. if is_boolean(pt1.resultdef) then
  492. def:=torddef.create(bool8bit,lv,hv)
  493. else
  494. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  495. end;
  496. end;
  497. end;
  498. end
  499. else
  500. Message(sym_e_error_in_type_def);
  501. pt2.free;
  502. end
  503. else
  504. begin
  505. { a simple type renaming or generic specialization }
  506. if (pt1.nodetype=typen) then
  507. begin
  508. def:=ttypenode(pt1).resultdef;
  509. if dospecialize then
  510. generate_specialization(def)
  511. else
  512. begin
  513. if (df_generic in def.defoptions) then
  514. begin
  515. Message(parser_e_no_generics_as_types);
  516. def:=generrordef;
  517. end;
  518. end;
  519. end
  520. else
  521. Message(sym_e_error_in_type_def);
  522. end;
  523. pt1.free;
  524. block_type:=old_block_type;
  525. end;
  526. procedure set_dec;
  527. begin
  528. consume(_SET);
  529. consume(_OF);
  530. read_anon_type(tt2,true);
  531. if assigned(tt2) then
  532. begin
  533. case tt2.typ of
  534. { don't forget that min can be negativ PM }
  535. enumdef :
  536. if (tenumdef(tt2).min>=0) and
  537. (tenumdef(tt2).max<=255) then
  538. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  539. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  540. else
  541. Message(sym_e_ill_type_decl_set);
  542. orddef :
  543. begin
  544. if (torddef(tt2).ordtype<>uvoid) and
  545. (torddef(tt2).ordtype<>uwidechar) and
  546. (torddef(tt2).low>=0) then
  547. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  548. if Torddef(tt2).high>int64(high(byte)) then
  549. message(sym_e_ill_type_decl_set)
  550. else
  551. def:=tsetdef.create(tt2,torddef(tt2).low,torddef(tt2).high)
  552. else
  553. Message(sym_e_ill_type_decl_set);
  554. end;
  555. else
  556. Message(sym_e_ill_type_decl_set);
  557. end;
  558. end
  559. else
  560. def:=generrordef;
  561. end;
  562. procedure array_dec(is_packed: boolean);
  563. var
  564. lowval,
  565. highval : TConstExprInt;
  566. indexdef : tdef;
  567. hdef : tdef;
  568. arrdef : tarraydef;
  569. procedure setdefdecl(def:tdef);
  570. begin
  571. case def.typ of
  572. enumdef :
  573. begin
  574. lowval:=tenumdef(def).min;
  575. highval:=tenumdef(def).max;
  576. if (m_fpc in current_settings.modeswitches) and
  577. (tenumdef(def).has_jumps) then
  578. Message(type_e_array_index_enums_with_assign_not_possible);
  579. indexdef:=def;
  580. end;
  581. orddef :
  582. begin
  583. if torddef(def).ordtype in [uchar,
  584. u8bit,u16bit,
  585. s8bit,s16bit,s32bit,
  586. {$ifdef cpu64bit}
  587. u32bit,s64bit,
  588. {$endif cpu64bit}
  589. bool8bit,bool16bit,bool32bit,bool64bit,
  590. uwidechar] then
  591. begin
  592. lowval:=torddef(def).low;
  593. highval:=torddef(def).high;
  594. indexdef:=def;
  595. end
  596. else
  597. Message1(parser_e_type_cant_be_used_in_array_index,def.GetTypeName);
  598. end;
  599. else
  600. Message(sym_e_error_in_type_def);
  601. end;
  602. end;
  603. begin
  604. arrdef:=nil;
  605. consume(_ARRAY);
  606. { open array? }
  607. if try_to_consume(_LECKKLAMMER) then
  608. begin
  609. { defaults }
  610. indexdef:=generrordef;
  611. { use defaults which don't overflow the compiler }
  612. lowval:=0;
  613. highval:=0;
  614. repeat
  615. { read the expression and check it, check apart if the
  616. declaration is an enum declaration because that needs to
  617. be parsed by readtype (PFV) }
  618. if token=_LKLAMMER then
  619. begin
  620. read_anon_type(hdef,true);
  621. setdefdecl(hdef);
  622. end
  623. else
  624. begin
  625. pt:=expr;
  626. if pt.nodetype=typen then
  627. setdefdecl(pt.resultdef)
  628. else
  629. begin
  630. if (pt.nodetype=rangen) then
  631. begin
  632. if (trangenode(pt).left.nodetype=ordconstn) and
  633. (trangenode(pt).right.nodetype=ordconstn) then
  634. begin
  635. { make both the same type or give an error. This is not
  636. done when both are integer values, because typecasting
  637. between -3200..3200 will result in a signed-unsigned
  638. conflict and give a range check error (PFV) }
  639. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  640. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  641. lowval:=tordconstnode(trangenode(pt).left).value;
  642. highval:=tordconstnode(trangenode(pt).right).value;
  643. if highval<lowval then
  644. begin
  645. Message(parser_e_array_lower_less_than_upper_bound);
  646. highval:=lowval;
  647. end
  648. else if (lowval<int64(low(aint))) or
  649. (highval > high(aint)) then
  650. begin
  651. Message(parser_e_array_range_out_of_bounds);
  652. lowval :=0;
  653. highval:=0;
  654. end;
  655. if is_integer(trangenode(pt).left.resultdef) then
  656. range_to_type(lowval,highval,indexdef)
  657. else
  658. indexdef:=trangenode(pt).left.resultdef;
  659. end
  660. else
  661. Message(type_e_cant_eval_constant_expr);
  662. end
  663. else
  664. Message(sym_e_error_in_type_def)
  665. end;
  666. pt.free;
  667. end;
  668. { if the array is already created add the new arrray
  669. as element of the existing array, otherwise create a new array }
  670. if assigned(arrdef) then
  671. begin
  672. arrdef.elementdef:=tarraydef.create(lowval,highval,indexdef);
  673. arrdef:=tarraydef(arrdef.elementdef);
  674. end
  675. else
  676. begin
  677. arrdef:=tarraydef.create(lowval,highval,indexdef);
  678. def:=arrdef;
  679. end;
  680. if is_packed then
  681. include(arrdef.arrayoptions,ado_IsBitPacked);
  682. if token=_COMMA then
  683. consume(_COMMA)
  684. else
  685. break;
  686. until false;
  687. consume(_RECKKLAMMER);
  688. end
  689. else
  690. begin
  691. if is_packed then
  692. Message(parser_e_packed_dynamic_open_array);
  693. arrdef:=tarraydef.create(0,-1,s32inttype);
  694. include(arrdef.arrayoptions,ado_IsDynamicArray);
  695. def:=arrdef;
  696. end;
  697. consume(_OF);
  698. read_anon_type(tt2,true);
  699. { set element type of the last array definition }
  700. if assigned(arrdef) then
  701. begin
  702. arrdef.elementdef:=tt2;
  703. if is_packed and
  704. tt2.needs_inittable then
  705. Message(type_e_no_packed_inittable);
  706. end;
  707. end;
  708. var
  709. p : tnode;
  710. pd : tabstractprocdef;
  711. is_func,
  712. enumdupmsg, first : boolean;
  713. newtype : ttypesym;
  714. oldlocalswitches : tlocalswitches;
  715. bitpacking: boolean;
  716. begin
  717. def:=nil;
  718. case token of
  719. _STRING,_FILE:
  720. begin
  721. single_type(def,false);
  722. end;
  723. _LKLAMMER:
  724. begin
  725. consume(_LKLAMMER);
  726. first := true;
  727. { allow negativ value_str }
  728. l:=-1;
  729. enumdupmsg:=false;
  730. aktenumdef:=tenumdef.create;
  731. repeat
  732. s:=orgpattern;
  733. defpos:=current_tokenpos;
  734. consume(_ID);
  735. { only allow assigning of specific numbers under fpc mode }
  736. if not(m_tp7 in current_settings.modeswitches) and
  737. (
  738. { in fpc mode also allow := to be compatible
  739. with previous 1.0.x versions }
  740. ((m_fpc in current_settings.modeswitches) and
  741. try_to_consume(_ASSIGNMENT)) or
  742. try_to_consume(_EQUAL)
  743. ) then
  744. begin
  745. oldlocalswitches:=current_settings.localswitches;
  746. include(current_settings.localswitches,cs_allow_enum_calc);
  747. p:=comp_expr(true);
  748. current_settings.localswitches:=oldlocalswitches;
  749. if (p.nodetype=ordconstn) then
  750. begin
  751. { we expect an integer or an enum of the
  752. same type }
  753. if is_integer(p.resultdef) or
  754. is_char(p.resultdef) or
  755. equal_defs(p.resultdef,aktenumdef) then
  756. v:=tordconstnode(p).value
  757. else
  758. IncompatibleTypes(p.resultdef,s32inttype);
  759. end
  760. else
  761. Message(parser_e_illegal_expression);
  762. p.free;
  763. { please leave that a note, allows type save }
  764. { declarations in the win32 units ! }
  765. if (not first) and (v<=l) and (not enumdupmsg) then
  766. begin
  767. Message(parser_n_duplicate_enum);
  768. enumdupmsg:=true;
  769. end;
  770. l:=v;
  771. end
  772. else
  773. inc(l);
  774. first := false;
  775. storepos:=current_tokenpos;
  776. current_tokenpos:=defpos;
  777. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,l));
  778. current_tokenpos:=storepos;
  779. until not try_to_consume(_COMMA);
  780. def:=aktenumdef;
  781. consume(_RKLAMMER);
  782. end;
  783. _ARRAY:
  784. begin
  785. array_dec(false);
  786. end;
  787. _SET:
  788. begin
  789. set_dec;
  790. end;
  791. _CARET:
  792. begin
  793. consume(_CARET);
  794. single_type(tt2,typecanbeforward);
  795. def:=tpointerdef.create(tt2);
  796. end;
  797. _RECORD:
  798. begin
  799. def:=record_dec;
  800. end;
  801. _PACKED,
  802. _BITPACKED:
  803. begin
  804. bitpacking :=
  805. (cs_bitpacking in current_settings.localswitches) or
  806. (token = _BITPACKED);
  807. consume(token);
  808. if token=_ARRAY then
  809. array_dec(bitpacking)
  810. else if token=_SET then
  811. set_dec
  812. else
  813. begin
  814. oldpackrecords:=current_settings.packrecords;
  815. if (not bitpacking) or
  816. (token in [_CLASS,_OBJECT]) then
  817. current_settings.packrecords:=1
  818. else
  819. current_settings.packrecords:=bit_alignment;
  820. if token in [_CLASS,_OBJECT] then
  821. def:=object_dec(name,genericdef,genericlist,nil)
  822. else
  823. def:=record_dec;
  824. current_settings.packrecords:=oldpackrecords;
  825. end;
  826. end;
  827. _DISPINTERFACE,
  828. _CLASS,
  829. _CPPCLASS,
  830. _INTERFACE,
  831. _OBJECT:
  832. begin
  833. def:=object_dec(name,genericdef,genericlist,nil);
  834. end;
  835. _PROCEDURE,
  836. _FUNCTION:
  837. begin
  838. is_func:=(token=_FUNCTION);
  839. consume(token);
  840. pd:=tprocvardef.create(normal_function_level);
  841. if token=_LKLAMMER then
  842. parse_parameter_dec(pd);
  843. if is_func then
  844. begin
  845. consume(_COLON);
  846. single_type(pd.returndef,false);
  847. end;
  848. if token=_OF then
  849. begin
  850. consume(_OF);
  851. consume(_OBJECT);
  852. include(pd.procoptions,po_methodpointer);
  853. end;
  854. def:=pd;
  855. { possible proc directives }
  856. if parseprocvardir then
  857. begin
  858. if check_proc_directive(true) then
  859. begin
  860. newtype:=ttypesym.create('unnamed',def);
  861. parse_var_proc_directives(tsym(newtype));
  862. newtype.typedef:=nil;
  863. def.typesym:=nil;
  864. newtype.free;
  865. end;
  866. { Add implicit hidden parameters and function result }
  867. handle_calling_convention(pd);
  868. end;
  869. end;
  870. else
  871. expr_type;
  872. end;
  873. if def=nil then
  874. def:=generrordef;
  875. end;
  876. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  877. begin
  878. read_named_type(def,'',nil,nil,parseprocvardir);
  879. end;
  880. procedure write_persistent_type_info(st:tsymtable);
  881. var
  882. i : longint;
  883. def : tdef;
  884. vmtwriter : TVMTWriter;
  885. begin
  886. for i:=0 to st.DefList.Count-1 do
  887. begin
  888. def:=tdef(st.DefList[i]);
  889. case def.typ of
  890. recorddef :
  891. write_persistent_type_info(trecorddef(def).symtable);
  892. objectdef :
  893. begin
  894. { Skip generics and forward defs }
  895. if (df_generic in def.defoptions) or
  896. (oo_is_forward in tobjectdef(def).objectoptions) then
  897. continue;
  898. write_persistent_type_info(tobjectdef(def).symtable);
  899. { Write also VMT if not done yet }
  900. if not(ds_vmt_written in def.defstates) then
  901. begin
  902. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  903. if is_interface(tobjectdef(def)) then
  904. vmtwriter.writeinterfaceids;
  905. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  906. vmtwriter.writevmt;
  907. vmtwriter.free;
  908. include(def.defstates,ds_vmt_written);
  909. end;
  910. end;
  911. procdef :
  912. begin
  913. if assigned(tprocdef(def).localst) and
  914. (tprocdef(def).localst.symtabletype=localsymtable) then
  915. write_persistent_type_info(tprocdef(def).localst);
  916. if assigned(tprocdef(def).parast) then
  917. write_persistent_type_info(tprocdef(def).parast);
  918. end;
  919. end;
  920. { generate always persistent tables for types in the interface so it can
  921. be reused in other units and give always the same pointer location. }
  922. { Init }
  923. if (
  924. assigned(def.typesym) and
  925. (st.symtabletype=globalsymtable)
  926. ) or
  927. def.needs_inittable or
  928. (ds_init_table_used in def.defstates) then
  929. RTTIWriter.write_rtti(def,initrtti);
  930. { RTTI }
  931. if (
  932. assigned(def.typesym) and
  933. (st.symtabletype=globalsymtable)
  934. ) or
  935. (ds_rtti_table_used in def.defstates) then
  936. RTTIWriter.write_rtti(def,fullrtti);
  937. end;
  938. end;
  939. end.