ptype.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Does parsing types for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ptype;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype;
  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. curobjectname : stringid;
  31. { reads a string, file type or a type id and returns a name and }
  32. { tdef }
  33. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  34. procedure read_type(var tt:ttype;const name : stringid;parseprocvardir:boolean);
  35. { reads a type definition }
  36. { to a appropriating tdef, s gets the name of }
  37. { the type to allow name mangling }
  38. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  39. implementation
  40. uses
  41. { common }
  42. cutils,
  43. { global }
  44. globals,tokens,verbose,
  45. systems,
  46. { target }
  47. paramgr,
  48. { symtable }
  49. symconst,symbase,symdef,symsym,symtable,
  50. defutil,defcmp,
  51. { pass 1 }
  52. node,
  53. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  54. { parser }
  55. scanner,
  56. pbase,pexpr,pdecsub,pdecvar,pdecobj;
  57. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  58. { reads a type definition }
  59. { to a appropriating tdef, s gets the name of }
  60. { the type to allow name mangling }
  61. var
  62. is_unit_specific : boolean;
  63. pos : tfileposinfo;
  64. srsym : tsym;
  65. srsymtable : tsymtable;
  66. sorg : stringid;
  67. begin
  68. s:=pattern;
  69. sorg:=orgpattern;
  70. pos:=akttokenpos;
  71. { classes can be used also in classes }
  72. if (curobjectname=pattern) and is_class_or_interface(aktobjectdef) then
  73. begin
  74. tt.setdef(aktobjectdef);
  75. consume(_ID);
  76. exit;
  77. end;
  78. { objects can be parameters }
  79. if (testcurobject=2) and (curobjectname=pattern) then
  80. begin
  81. tt.setdef(aktobjectdef);
  82. consume(_ID);
  83. exit;
  84. end;
  85. { try to load the symbol to see if it's a unitsym. Use the
  86. special searchsym_type that ignores records,objects and
  87. parameters }
  88. is_unit_specific:=false;
  89. searchsym_type(s,srsym,srsymtable);
  90. consume(_ID);
  91. if assigned(srsym) and
  92. (srsym.typ=unitsym) then
  93. begin
  94. is_unit_specific:=true;
  95. consume(_POINT);
  96. if srsym.owner.unitid=0 then
  97. begin
  98. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,pattern);
  99. pos:=akttokenpos;
  100. s:=pattern;
  101. end
  102. else
  103. srsym:=nil;
  104. consume(_ID);
  105. end;
  106. { Types are first defined with an error def before assigning
  107. the real type so check if it's an errordef. if so then
  108. give an error. Only check for typesyms in the current symbol
  109. table as forwarddef are not resolved directly }
  110. if assigned(srsym) and
  111. (srsym.typ=typesym) and
  112. (srsym.owner=symtablestack) and
  113. (ttypesym(srsym).restype.def.deftype=errordef) then
  114. begin
  115. Message1(type_e_type_is_not_completly_defined,ttypesym(srsym).realname);
  116. tt:=generrortype;
  117. exit;
  118. end;
  119. { are we parsing a possible forward def ? }
  120. if isforwarddef and
  121. not(is_unit_specific) then
  122. begin
  123. tt.setdef(tforwarddef.create(s,pos));
  124. exit;
  125. end;
  126. { unknown sym ? }
  127. if not assigned(srsym) then
  128. begin
  129. Message1(sym_e_id_not_found,sorg);
  130. tt:=generrortype;
  131. exit;
  132. end;
  133. { type sym ? }
  134. if (srsym.typ<>typesym) then
  135. begin
  136. Message(type_e_type_id_expected);
  137. tt:=generrortype;
  138. exit;
  139. end;
  140. { Give an error when referring to an errordef }
  141. if (ttypesym(srsym).restype.def.deftype=errordef) then
  142. begin
  143. Message(sym_e_error_in_type_def);
  144. tt:=generrortype;
  145. exit;
  146. end;
  147. { Use the definitions for current unit, because
  148. they can be refered from the parameters and symbols are not
  149. loaded at that time. Only write the definition when the
  150. symbol is the real owner of the definition (not a redefine) }
  151. if (ttypesym(srsym).owner.unitid=0) and
  152. ((ttypesym(srsym).restype.def.typesym=nil) or
  153. (srsym=ttypesym(srsym).restype.def.typesym)) then
  154. tt.setdef(ttypesym(srsym).restype.def)
  155. else
  156. tt.setsym(srsym);
  157. end;
  158. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  159. { reads a string, file type or a type id and returns a name and }
  160. { tdef }
  161. var
  162. hs : string;
  163. t2 : ttype;
  164. begin
  165. case token of
  166. _STRING:
  167. begin
  168. string_dec(tt);
  169. s:='STRING';
  170. end;
  171. _FILE:
  172. begin
  173. consume(_FILE);
  174. if token=_OF then
  175. begin
  176. consume(_OF);
  177. single_type(t2,hs,false);
  178. tt.setdef(tfiledef.createtyped(t2));
  179. s:='FILE$OF$'+hs;
  180. end
  181. else
  182. begin
  183. tt:=cfiletype;
  184. s:='FILE';
  185. end;
  186. end;
  187. _ID:
  188. begin
  189. id_type(tt,s,isforwarddef);
  190. end;
  191. else
  192. begin
  193. message(type_e_type_id_expected);
  194. s:='<unknown>';
  195. tt:=generrortype;
  196. end;
  197. end;
  198. end;
  199. { reads a record declaration }
  200. function record_dec : tdef;
  201. var
  202. symtable : tsymtable;
  203. storetypecanbeforward : boolean;
  204. old_object_option : tsymoptions;
  205. begin
  206. { create recdef }
  207. symtable:=trecordsymtable.create(aktpackrecords);
  208. record_dec:=trecorddef.create(symtable);
  209. { update symtable stack }
  210. symtable.next:=symtablestack;
  211. symtablestack:=symtable;
  212. { parse record }
  213. consume(_RECORD);
  214. old_object_option:=current_object_option;
  215. current_object_option:=[sp_public];
  216. storetypecanbeforward:=typecanbeforward;
  217. { for tp7 don't allow forward types }
  218. if m_tp7 in aktmodeswitches then
  219. typecanbeforward:=false;
  220. read_var_decs(true,false,false);
  221. consume(_END);
  222. typecanbeforward:=storetypecanbeforward;
  223. current_object_option:=old_object_option;
  224. { make the record size aligned }
  225. trecordsymtable(symtablestack).addalignmentpadding;
  226. { restore symtable stack }
  227. symtablestack:=symtable.next;
  228. end;
  229. { reads a type definition and returns a pointer to it }
  230. procedure read_type(var tt : ttype;const name : stringid;parseprocvardir:boolean);
  231. var
  232. pt : tnode;
  233. tt2 : ttype;
  234. aktenumdef : tenumdef;
  235. ap : tarraydef;
  236. s : stringid;
  237. l,v : TConstExprInt;
  238. oldaktpackrecords : longint;
  239. hs : string;
  240. defpos,storepos : tfileposinfo;
  241. procedure expr_type;
  242. var
  243. pt1,pt2 : tnode;
  244. lv,hv : TConstExprInt;
  245. begin
  246. { use of current parsed object ? }
  247. if (token=_ID) and (testcurobject=2) and (curobjectname=pattern) then
  248. begin
  249. consume(_ID);
  250. tt.setdef(aktobjectdef);
  251. exit;
  252. end;
  253. { classes can be used also in classes }
  254. if (curobjectname=pattern) and is_class_or_interface(aktobjectdef) then
  255. begin
  256. tt.setdef(aktobjectdef);
  257. consume(_ID);
  258. exit;
  259. end;
  260. { we can't accept a equal in type }
  261. pt1:=comp_expr(not(ignore_equal));
  262. if (token=_POINTPOINT) then
  263. begin
  264. consume(_POINTPOINT);
  265. { get high value of range }
  266. pt2:=comp_expr(not(ignore_equal));
  267. { make both the same type or give an error. This is not
  268. done when both are integer values, because typecasting
  269. between -3200..3200 will result in a signed-unsigned
  270. conflict and give a range check error (PFV) }
  271. if not(is_integer(pt1.resulttype.def) and is_integer(pt2.resulttype.def)) then
  272. inserttypeconv(pt1,pt2.resulttype);
  273. { both must be evaluated to constants now }
  274. if (pt1.nodetype=ordconstn) and
  275. (pt2.nodetype=ordconstn) then
  276. begin
  277. lv:=tordconstnode(pt1).value;
  278. hv:=tordconstnode(pt2).value;
  279. { Check bounds }
  280. if hv<lv then
  281. Message(parser_e_upper_lower_than_lower)
  282. else
  283. begin
  284. { All checks passed, create the new def }
  285. case pt1.resulttype.def.deftype of
  286. enumdef :
  287. tt.setdef(tenumdef.create_subrange(tenumdef(pt1.resulttype.def),lv,hv));
  288. orddef :
  289. begin
  290. if is_char(pt1.resulttype.def) then
  291. tt.setdef(torddef.create(uchar,lv,hv))
  292. else
  293. if is_boolean(pt1.resulttype.def) then
  294. tt.setdef(torddef.create(bool8bit,l,hv))
  295. else
  296. tt.setdef(torddef.create(range_to_basetype(lv,hv),lv,hv));
  297. end;
  298. end;
  299. end;
  300. end
  301. else
  302. Message(sym_e_error_in_type_def);
  303. pt2.free;
  304. end
  305. else
  306. begin
  307. { a simple type renaming }
  308. if (pt1.nodetype=typen) then
  309. tt:=ttypenode(pt1).resulttype
  310. else
  311. Message(sym_e_error_in_type_def);
  312. end;
  313. pt1.free;
  314. end;
  315. procedure array_dec;
  316. var
  317. lowval,
  318. highval : aint;
  319. arraytype : ttype;
  320. ht : ttype;
  321. procedure setdefdecl(const t:ttype);
  322. begin
  323. case t.def.deftype of
  324. enumdef :
  325. begin
  326. lowval:=tenumdef(t.def).min;
  327. highval:=tenumdef(t.def).max;
  328. if (m_fpc in aktmodeswitches) and
  329. (tenumdef(t.def).has_jumps) then
  330. Message(type_e_array_index_enums_with_assign_not_possible);
  331. arraytype:=t;
  332. end;
  333. orddef :
  334. begin
  335. if torddef(t.def).typ in [uchar,
  336. u8bit,u16bit,
  337. s8bit,s16bit,s32bit,
  338. {$ifdef cpu64bit}
  339. u32bit,s64bit,
  340. {$endif cpu64bit}
  341. bool8bit,bool16bit,bool32bit,
  342. uwidechar] then
  343. begin
  344. lowval:=torddef(t.def).low;
  345. highval:=torddef(t.def).high;
  346. arraytype:=t;
  347. end
  348. else
  349. Message1(parser_e_type_cant_be_used_in_array_index,t.def.gettypename);
  350. end;
  351. else
  352. Message(sym_e_error_in_type_def);
  353. end;
  354. end;
  355. begin
  356. consume(_ARRAY);
  357. { open array? }
  358. if token=_LECKKLAMMER then
  359. begin
  360. consume(_LECKKLAMMER);
  361. { defaults }
  362. arraytype:=generrortype;
  363. lowval:=low(aint);
  364. highval:=high(aint);
  365. tt.reset;
  366. repeat
  367. { read the expression and check it, check apart if the
  368. declaration is an enum declaration because that needs to
  369. be parsed by readtype (PFV) }
  370. if token=_LKLAMMER then
  371. begin
  372. read_type(ht,'',true);
  373. setdefdecl(ht);
  374. end
  375. else
  376. begin
  377. pt:=expr;
  378. if pt.nodetype=typen then
  379. setdefdecl(pt.resulttype)
  380. else
  381. begin
  382. if (pt.nodetype=rangen) then
  383. begin
  384. if (trangenode(pt).left.nodetype=ordconstn) and
  385. (trangenode(pt).right.nodetype=ordconstn) then
  386. begin
  387. { make both the same type or give an error. This is not
  388. done when both are integer values, because typecasting
  389. between -3200..3200 will result in a signed-unsigned
  390. conflict and give a range check error (PFV) }
  391. if not(is_integer(trangenode(pt).left.resulttype.def) and is_integer(trangenode(pt).left.resulttype.def)) then
  392. inserttypeconv(trangenode(pt).left,trangenode(pt).right.resulttype);
  393. lowval:=tordconstnode(trangenode(pt).left).value;
  394. highval:=tordconstnode(trangenode(pt).right).value;
  395. if highval<lowval then
  396. begin
  397. Message(parser_e_array_lower_less_than_upper_bound);
  398. highval:=lowval;
  399. end;
  400. if is_integer(trangenode(pt).left.resulttype.def) then
  401. range_to_type(lowval,highval,arraytype)
  402. else
  403. arraytype:=trangenode(pt).left.resulttype;
  404. end
  405. else
  406. Message(type_e_cant_eval_constant_expr);
  407. end
  408. else
  409. Message(sym_e_error_in_type_def)
  410. end;
  411. pt.free;
  412. end;
  413. { create arraydef }
  414. if not assigned(tt.def) then
  415. begin
  416. ap:=tarraydef.create(lowval,highval,arraytype);
  417. tt.setdef(ap);
  418. end
  419. else
  420. begin
  421. ap.elementtype.setdef(tarraydef.create(lowval,highval,arraytype));
  422. ap:=tarraydef(ap.elementtype.def);
  423. end;
  424. if token=_COMMA then
  425. consume(_COMMA)
  426. else
  427. break;
  428. until false;
  429. consume(_RECKKLAMMER);
  430. end
  431. else
  432. begin
  433. ap:=tarraydef.create(0,-1,s32inttype);
  434. ap.IsDynamicArray:=true;
  435. tt.setdef(ap);
  436. end;
  437. consume(_OF);
  438. read_type(tt2,'',true);
  439. { if no error, set element type }
  440. if assigned(ap) then
  441. ap.setelementtype(tt2);
  442. end;
  443. var
  444. p : tnode;
  445. pd : tabstractprocdef;
  446. is_func,
  447. enumdupmsg : boolean;
  448. newtype : ttypesym;
  449. begin
  450. tt.reset;
  451. case token of
  452. _STRING,_FILE:
  453. begin
  454. single_type(tt,hs,false);
  455. end;
  456. _LKLAMMER:
  457. begin
  458. consume(_LKLAMMER);
  459. { allow negativ value_str }
  460. l:=-1;
  461. enumdupmsg:=false;
  462. aktenumdef:=tenumdef.create;
  463. repeat
  464. s:=orgpattern;
  465. defpos:=akttokenpos;
  466. consume(_ID);
  467. { only allow assigning of specific numbers under fpc mode }
  468. if not(m_tp7 in aktmodeswitches) and
  469. (
  470. { in fpc mode also allow := to be compatible
  471. with previous 1.0.x versions }
  472. ((m_fpc in aktmodeswitches) and
  473. try_to_consume(_ASSIGNMENT)) or
  474. try_to_consume(_EQUAL)
  475. ) then
  476. begin
  477. p:=comp_expr(true);
  478. if (p.nodetype=ordconstn) then
  479. begin
  480. { we expect an integer or an enum of the
  481. same type }
  482. if is_integer(p.resulttype.def) or
  483. is_char(p.resulttype.def) or
  484. equal_defs(p.resulttype.def,aktenumdef) then
  485. v:=tordconstnode(p).value
  486. else
  487. IncompatibleTypes(p.resulttype.def,s32inttype.def);
  488. end
  489. else
  490. Message(parser_e_illegal_expression);
  491. p.free;
  492. { please leave that a note, allows type save }
  493. { declarations in the win32 units ! }
  494. if (v<=l) and (not enumdupmsg) then
  495. begin
  496. Message(parser_n_duplicate_enum);
  497. enumdupmsg:=true;
  498. end;
  499. l:=v;
  500. end
  501. else
  502. inc(l);
  503. storepos:=akttokenpos;
  504. akttokenpos:=defpos;
  505. constsymtable.insert(tenumsym.create(s,aktenumdef,l));
  506. akttokenpos:=storepos;
  507. until not try_to_consume(_COMMA);
  508. tt.setdef(aktenumdef);
  509. consume(_RKLAMMER);
  510. end;
  511. _ARRAY:
  512. begin
  513. array_dec;
  514. end;
  515. _SET:
  516. begin
  517. consume(_SET);
  518. consume(_OF);
  519. read_type(tt2,'',true);
  520. if assigned(tt2.def) then
  521. begin
  522. case tt2.def.deftype of
  523. { don't forget that min can be negativ PM }
  524. enumdef :
  525. if tenumdef(tt2.def).min>=0 then
  526. tt.setdef(tsetdef.create(tt2,tenumdef(tt2.def).max))
  527. else
  528. Message(sym_e_ill_type_decl_set);
  529. orddef :
  530. begin
  531. case torddef(tt2.def).typ of
  532. uchar :
  533. tt.setdef(tsetdef.create(tt2,255));
  534. u8bit,u16bit,u32bit,
  535. s8bit,s16bit,s32bit :
  536. begin
  537. if (torddef(tt2.def).low>=0) then
  538. tt.setdef(tsetdef.create(tt2,torddef(tt2.def).high))
  539. else
  540. Message(sym_e_ill_type_decl_set);
  541. end;
  542. else
  543. Message(sym_e_ill_type_decl_set);
  544. end;
  545. end;
  546. else
  547. Message(sym_e_ill_type_decl_set);
  548. end;
  549. end
  550. else
  551. tt:=generrortype;
  552. end;
  553. _CARET:
  554. begin
  555. consume(_CARET);
  556. single_type(tt2,hs,typecanbeforward);
  557. tt.setdef(tpointerdef.create(tt2));
  558. end;
  559. _RECORD:
  560. begin
  561. tt.setdef(record_dec);
  562. end;
  563. _PACKED:
  564. begin
  565. consume(_PACKED);
  566. if token=_ARRAY then
  567. array_dec
  568. else
  569. begin
  570. oldaktpackrecords:=aktpackrecords;
  571. aktpackrecords:=1;
  572. if token in [_CLASS,_OBJECT] then
  573. tt.setdef(object_dec(name,nil))
  574. else
  575. tt.setdef(record_dec);
  576. aktpackrecords:=oldaktpackrecords;
  577. end;
  578. end;
  579. _CLASS,
  580. _CPPCLASS,
  581. _INTERFACE,
  582. _OBJECT:
  583. begin
  584. tt.setdef(object_dec(name,nil));
  585. end;
  586. _PROCEDURE,
  587. _FUNCTION:
  588. begin
  589. is_func:=(token=_FUNCTION);
  590. consume(token);
  591. pd:=tprocvardef.create(normal_function_level);
  592. if token=_LKLAMMER then
  593. parse_parameter_dec(pd);
  594. if is_func then
  595. begin
  596. consume(_COLON);
  597. single_type(pd.rettype,hs,false);
  598. end;
  599. if token=_OF then
  600. begin
  601. consume(_OF);
  602. consume(_OBJECT);
  603. include(pd.procoptions,po_methodpointer);
  604. end;
  605. tt.def:=pd;
  606. { possible proc directives }
  607. if parseprocvardir then
  608. begin
  609. if check_proc_directive(true) then
  610. begin
  611. newtype:=ttypesym.create('unnamed',tt);
  612. parse_var_proc_directives(tsym(newtype));
  613. newtype.restype.def:=nil;
  614. tt.def.typesym:=nil;
  615. newtype.free;
  616. end;
  617. { Add implicit hidden parameters and function result }
  618. handle_calling_convention(pd);
  619. end;
  620. end;
  621. else
  622. expr_type;
  623. end;
  624. if tt.def=nil then
  625. tt:=generrortype;
  626. end;
  627. end.
  628. {
  629. $Log$
  630. Revision 1.72 2005-01-04 16:39:12 peter
  631. * allow enum with jumps as array index in delphi mode
  632. Revision 1.71 2004/11/16 20:32:41 peter
  633. * fixes for win32 mangledname
  634. Revision 1.70 2004/11/15 23:35:31 peter
  635. * tparaitem removed, use tparavarsym instead
  636. * parameter order is now calculated from paranr value in tparavarsym
  637. Revision 1.69 2004/11/01 23:30:11 peter
  638. * support > 32bit accesses for x86_64
  639. * rewrote array size checking to support 64bit
  640. Revision 1.68 2004/06/20 08:55:30 florian
  641. * logs truncated
  642. Revision 1.67 2004/06/16 20:07:09 florian
  643. * dwarf branch merged
  644. Revision 1.66.2.1 2004/04/28 19:55:52 peter
  645. * new warning for ordinal-pointer when size is different
  646. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  647. Revision 1.66 2004/03/29 14:44:10 peter
  648. * fixes to previous constant integer commit
  649. Revision 1.65 2004/03/23 22:34:49 peter
  650. * constants ordinals now always have a type assigned
  651. * integer constants have the smallest type, unsigned prefered over
  652. signed
  653. Revision 1.64 2004/02/03 22:32:54 peter
  654. * renamed xNNbittype to xNNinttype
  655. * renamed registers32 to registersint
  656. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  657. }