ptype.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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,constexp,
  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 if (lv.signed and (lv.svalue<0)) and (not hv.signed and (hv.uvalue>qword(high(int64)))) then
  481. message(type_e_cant_eval_constant_expr)
  482. else
  483. begin
  484. { All checks passed, create the new def }
  485. case pt1.resultdef.typ of
  486. enumdef :
  487. def:=tenumdef.create_subrange(tenumdef(pt1.resultdef),lv.svalue,hv.svalue);
  488. orddef :
  489. begin
  490. if is_char(pt1.resultdef) then
  491. def:=torddef.create(uchar,lv,hv)
  492. else
  493. if is_boolean(pt1.resultdef) then
  494. def:=torddef.create(pasbool,lv,hv)
  495. else if is_signed(pt1.resultdef) then
  496. def:=torddef.create(range_to_basetype(lv,hv),lv,hv)
  497. else
  498. def:=torddef.create(range_to_basetype(lv,hv),lv,hv);
  499. end;
  500. end;
  501. end;
  502. end
  503. else
  504. Message(sym_e_error_in_type_def);
  505. pt2.free;
  506. end
  507. else
  508. begin
  509. { a simple type renaming or generic specialization }
  510. if (pt1.nodetype=typen) then
  511. begin
  512. def:=ttypenode(pt1).resultdef;
  513. if dospecialize then
  514. generate_specialization(def)
  515. else
  516. begin
  517. if (df_generic in def.defoptions) then
  518. begin
  519. Message(parser_e_no_generics_as_types);
  520. def:=generrordef;
  521. end;
  522. end;
  523. end
  524. else
  525. Message(sym_e_error_in_type_def);
  526. end;
  527. pt1.free;
  528. block_type:=old_block_type;
  529. end;
  530. procedure set_dec;
  531. begin
  532. consume(_SET);
  533. consume(_OF);
  534. read_anon_type(tt2,true);
  535. if assigned(tt2) then
  536. begin
  537. case tt2.typ of
  538. { don't forget that min can be negativ PM }
  539. enumdef :
  540. if (tenumdef(tt2).min>=0) and
  541. (tenumdef(tt2).max<=255) then
  542. // !! def:=tsetdef.create(tt2,tenumdef(tt2.def).min,tenumdef(tt2.def).max))
  543. def:=tsetdef.create(tt2,tenumdef(tt2).min,tenumdef(tt2).max)
  544. else
  545. Message(sym_e_ill_type_decl_set);
  546. orddef :
  547. begin
  548. if (torddef(tt2).ordtype<>uvoid) and
  549. (torddef(tt2).ordtype<>uwidechar) and
  550. (torddef(tt2).low>=0) then
  551. // !! def:=tsetdef.create(tt2,torddef(tt2.def).low,torddef(tt2.def).high))
  552. if Torddef(tt2).high>int64(high(byte)) then
  553. message(sym_e_ill_type_decl_set)
  554. else
  555. def:=tsetdef.create(tt2,torddef(tt2).low.svalue,torddef(tt2).high.svalue)
  556. else
  557. Message(sym_e_ill_type_decl_set);
  558. end;
  559. else
  560. Message(sym_e_ill_type_decl_set);
  561. end;
  562. end
  563. else
  564. def:=generrordef;
  565. end;
  566. procedure array_dec(is_packed: boolean);
  567. var
  568. lowval,
  569. highval : TConstExprInt;
  570. indexdef : tdef;
  571. hdef : tdef;
  572. arrdef : tarraydef;
  573. procedure setdefdecl(def:tdef);
  574. begin
  575. case def.typ of
  576. enumdef :
  577. begin
  578. lowval:=tenumdef(def).min;
  579. highval:=tenumdef(def).max;
  580. if (m_fpc in current_settings.modeswitches) and
  581. (tenumdef(def).has_jumps) then
  582. Message(type_e_array_index_enums_with_assign_not_possible);
  583. indexdef:=def;
  584. end;
  585. orddef :
  586. begin
  587. if torddef(def).ordtype in [uchar,
  588. u8bit,u16bit,
  589. s8bit,s16bit,s32bit,
  590. {$ifdef cpu64bitaddr}
  591. u32bit,s64bit,
  592. {$endif cpu64bitaddr}
  593. pasbool,bool8bit,bool16bit,bool32bit,bool64bit,
  594. uwidechar] then
  595. begin
  596. lowval:=torddef(def).low;
  597. highval:=torddef(def).high;
  598. indexdef:=def;
  599. end
  600. else
  601. Message1(parser_e_type_cant_be_used_in_array_index,def.GetTypeName);
  602. end;
  603. else
  604. Message(sym_e_error_in_type_def);
  605. end;
  606. end;
  607. begin
  608. arrdef:=nil;
  609. consume(_ARRAY);
  610. { open array? }
  611. if try_to_consume(_LECKKLAMMER) then
  612. begin
  613. { defaults }
  614. indexdef:=generrordef;
  615. lowval:=int64(low(aint));
  616. highval:=high(aint);
  617. repeat
  618. { read the expression and check it, check apart if the
  619. declaration is an enum declaration because that needs to
  620. be parsed by readtype (PFV) }
  621. if token=_LKLAMMER then
  622. begin
  623. read_anon_type(hdef,true);
  624. setdefdecl(hdef);
  625. end
  626. else
  627. begin
  628. pt:=expr;
  629. if pt.nodetype=typen then
  630. setdefdecl(pt.resultdef)
  631. else
  632. begin
  633. if (pt.nodetype=rangen) then
  634. begin
  635. if (trangenode(pt).left.nodetype=ordconstn) and
  636. (trangenode(pt).right.nodetype=ordconstn) then
  637. begin
  638. { make both the same type or give an error. This is not
  639. done when both are integer values, because typecasting
  640. between -3200..3200 will result in a signed-unsigned
  641. conflict and give a range check error (PFV) }
  642. if not(is_integer(trangenode(pt).left.resultdef) and is_integer(trangenode(pt).left.resultdef)) then
  643. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resultdef);
  644. lowval:=tordconstnode(trangenode(pt).left).value;
  645. highval:=tordconstnode(trangenode(pt).right).value;
  646. if highval<lowval then
  647. begin
  648. Message(parser_e_array_lower_less_than_upper_bound);
  649. highval:=lowval;
  650. end
  651. else if (lowval<int64(low(aint))) or
  652. (highval > high(aint)) then
  653. begin
  654. Message(parser_e_array_range_out_of_bounds);
  655. lowval :=0;
  656. highval:=0;
  657. end;
  658. if is_integer(trangenode(pt).left.resultdef) then
  659. range_to_type(lowval,highval,indexdef)
  660. else
  661. indexdef:=trangenode(pt).left.resultdef;
  662. end
  663. else
  664. Message(type_e_cant_eval_constant_expr);
  665. end
  666. else
  667. Message(sym_e_error_in_type_def)
  668. end;
  669. pt.free;
  670. end;
  671. { if the array is already created add the new arrray
  672. as element of the existing array, otherwise create a new array }
  673. if assigned(arrdef) then
  674. begin
  675. arrdef.elementdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  676. arrdef:=tarraydef(arrdef.elementdef);
  677. end
  678. else
  679. begin
  680. arrdef:=tarraydef.create(lowval.svalue,highval.svalue,indexdef);
  681. def:=arrdef;
  682. end;
  683. if is_packed then
  684. include(arrdef.arrayoptions,ado_IsBitPacked);
  685. if token=_COMMA then
  686. consume(_COMMA)
  687. else
  688. break;
  689. until false;
  690. consume(_RECKKLAMMER);
  691. end
  692. else
  693. begin
  694. if is_packed then
  695. Message(parser_e_packed_dynamic_open_array);
  696. arrdef:=tarraydef.create(0,-1,s32inttype);
  697. include(arrdef.arrayoptions,ado_IsDynamicArray);
  698. def:=arrdef;
  699. end;
  700. consume(_OF);
  701. read_anon_type(tt2,true);
  702. { set element type of the last array definition }
  703. if assigned(arrdef) then
  704. begin
  705. arrdef.elementdef:=tt2;
  706. if is_packed and
  707. tt2.needs_inittable then
  708. Message(type_e_no_packed_inittable);
  709. end;
  710. end;
  711. var
  712. p : tnode;
  713. pd : tabstractprocdef;
  714. is_func,
  715. enumdupmsg, first : boolean;
  716. newtype : ttypesym;
  717. oldlocalswitches : tlocalswitches;
  718. bitpacking: boolean;
  719. begin
  720. def:=nil;
  721. case token of
  722. _STRING,_FILE:
  723. begin
  724. single_type(def,false);
  725. end;
  726. _LKLAMMER:
  727. begin
  728. consume(_LKLAMMER);
  729. first := true;
  730. { allow negativ value_str }
  731. l:=int64(-1);
  732. enumdupmsg:=false;
  733. aktenumdef:=tenumdef.create;
  734. repeat
  735. s:=orgpattern;
  736. defpos:=current_tokenpos;
  737. consume(_ID);
  738. { only allow assigning of specific numbers under fpc mode }
  739. if not(m_tp7 in current_settings.modeswitches) and
  740. (
  741. { in fpc mode also allow := to be compatible
  742. with previous 1.0.x versions }
  743. ((m_fpc in current_settings.modeswitches) and
  744. try_to_consume(_ASSIGNMENT)) or
  745. try_to_consume(_EQUAL)
  746. ) then
  747. begin
  748. oldlocalswitches:=current_settings.localswitches;
  749. include(current_settings.localswitches,cs_allow_enum_calc);
  750. p:=comp_expr(true);
  751. current_settings.localswitches:=oldlocalswitches;
  752. if (p.nodetype=ordconstn) then
  753. begin
  754. { we expect an integer or an enum of the
  755. same type }
  756. if is_integer(p.resultdef) or
  757. is_char(p.resultdef) or
  758. equal_defs(p.resultdef,aktenumdef) then
  759. v:=tordconstnode(p).value
  760. else
  761. IncompatibleTypes(p.resultdef,s32inttype);
  762. end
  763. else
  764. Message(parser_e_illegal_expression);
  765. p.free;
  766. { please leave that a note, allows type save }
  767. { declarations in the win32 units ! }
  768. if (not first) and (v<=l) and (not enumdupmsg) then
  769. begin
  770. Message(parser_n_duplicate_enum);
  771. enumdupmsg:=true;
  772. end;
  773. l:=v;
  774. end
  775. else
  776. inc(l.svalue);
  777. first := false;
  778. storepos:=current_tokenpos;
  779. current_tokenpos:=defpos;
  780. tstoredsymtable(aktenumdef.owner).insert(tenumsym.create(s,aktenumdef,l.svalue));
  781. current_tokenpos:=storepos;
  782. until not try_to_consume(_COMMA);
  783. def:=aktenumdef;
  784. consume(_RKLAMMER);
  785. end;
  786. _ARRAY:
  787. begin
  788. array_dec(false);
  789. end;
  790. _SET:
  791. begin
  792. set_dec;
  793. end;
  794. _CARET:
  795. begin
  796. consume(_CARET);
  797. single_type(tt2,typecanbeforward);
  798. def:=tpointerdef.create(tt2);
  799. end;
  800. _RECORD:
  801. begin
  802. def:=record_dec;
  803. end;
  804. _PACKED,
  805. _BITPACKED:
  806. begin
  807. bitpacking :=
  808. (cs_bitpacking in current_settings.localswitches) or
  809. (token = _BITPACKED);
  810. consume(token);
  811. if token=_ARRAY then
  812. array_dec(bitpacking)
  813. else if token=_SET then
  814. set_dec
  815. else
  816. begin
  817. oldpackrecords:=current_settings.packrecords;
  818. if (not bitpacking) or
  819. (token in [_CLASS,_OBJECT]) then
  820. current_settings.packrecords:=1
  821. else
  822. current_settings.packrecords:=bit_alignment;
  823. if token in [_CLASS,_OBJECT] then
  824. def:=object_dec(name,genericdef,genericlist,nil)
  825. else
  826. def:=record_dec;
  827. current_settings.packrecords:=oldpackrecords;
  828. end;
  829. end;
  830. _DISPINTERFACE,
  831. _CLASS,
  832. _CPPCLASS,
  833. _INTERFACE,
  834. _OBJECT:
  835. begin
  836. def:=object_dec(name,genericdef,genericlist,nil);
  837. end;
  838. _PROCEDURE,
  839. _FUNCTION:
  840. begin
  841. is_func:=(token=_FUNCTION);
  842. consume(token);
  843. pd:=tprocvardef.create(normal_function_level);
  844. if token=_LKLAMMER then
  845. parse_parameter_dec(pd);
  846. if is_func then
  847. begin
  848. consume(_COLON);
  849. single_type(pd.returndef,false);
  850. end;
  851. if token=_OF then
  852. begin
  853. consume(_OF);
  854. consume(_OBJECT);
  855. include(pd.procoptions,po_methodpointer);
  856. end;
  857. def:=pd;
  858. { possible proc directives }
  859. if parseprocvardir then
  860. begin
  861. if check_proc_directive(true) then
  862. begin
  863. newtype:=ttypesym.create('unnamed',def);
  864. parse_var_proc_directives(tsym(newtype));
  865. newtype.typedef:=nil;
  866. def.typesym:=nil;
  867. newtype.free;
  868. end;
  869. { Add implicit hidden parameters and function result }
  870. handle_calling_convention(pd);
  871. end;
  872. end;
  873. else
  874. expr_type;
  875. end;
  876. if def=nil then
  877. def:=generrordef;
  878. end;
  879. procedure read_anon_type(var def : tdef;parseprocvardir:boolean);
  880. begin
  881. read_named_type(def,'',nil,nil,parseprocvardir);
  882. end;
  883. procedure write_persistent_type_info(st:tsymtable);
  884. var
  885. i : longint;
  886. def : tdef;
  887. vmtwriter : TVMTWriter;
  888. begin
  889. for i:=0 to st.DefList.Count-1 do
  890. begin
  891. def:=tdef(st.DefList[i]);
  892. case def.typ of
  893. recorddef :
  894. write_persistent_type_info(trecorddef(def).symtable);
  895. objectdef :
  896. begin
  897. { Skip generics and forward defs }
  898. if (df_generic in def.defoptions) or
  899. (oo_is_forward in tobjectdef(def).objectoptions) then
  900. continue;
  901. write_persistent_type_info(tobjectdef(def).symtable);
  902. { Write also VMT if not done yet }
  903. if not(ds_vmt_written in def.defstates) then
  904. begin
  905. vmtwriter:=TVMTWriter.create(tobjectdef(def));
  906. if is_interface(tobjectdef(def)) then
  907. vmtwriter.writeinterfaceids;
  908. if (oo_has_vmt in tobjectdef(def).objectoptions) then
  909. vmtwriter.writevmt;
  910. vmtwriter.free;
  911. include(def.defstates,ds_vmt_written);
  912. end;
  913. end;
  914. procdef :
  915. begin
  916. if assigned(tprocdef(def).localst) and
  917. (tprocdef(def).localst.symtabletype=localsymtable) then
  918. write_persistent_type_info(tprocdef(def).localst);
  919. if assigned(tprocdef(def).parast) then
  920. write_persistent_type_info(tprocdef(def).parast);
  921. end;
  922. end;
  923. { generate always persistent tables for types in the interface so it can
  924. be reused in other units and give always the same pointer location. }
  925. { Init }
  926. if (
  927. assigned(def.typesym) and
  928. (st.symtabletype=globalsymtable)
  929. ) or
  930. def.needs_inittable or
  931. (ds_init_table_used in def.defstates) then
  932. RTTIWriter.write_rtti(def,initrtti);
  933. { RTTI }
  934. if (
  935. assigned(def.typesym) and
  936. (st.symtabletype=globalsymtable)
  937. ) or
  938. (ds_rtti_table_used in def.defstates) then
  939. RTTIWriter.write_rtti(def,fullrtti);
  940. end;
  941. end;
  942. end.