2
0

htypechk.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit exports some help routines for the type checking
  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 htypechk;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,
  23. node,
  24. symtype,symdef;
  25. type
  26. Ttok2nodeRec=record
  27. tok : ttoken;
  28. nod : tnodetype;
  29. op_overloading_supported : boolean;
  30. end;
  31. const
  32. tok2nodes=25;
  33. tok2node:array[1..tok2nodes] of ttok2noderec=(
  34. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  35. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  36. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  37. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  38. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  39. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  40. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  41. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  42. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  43. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  44. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  45. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  46. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  47. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  48. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  49. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  50. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  51. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  52. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  53. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  54. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  55. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  56. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  57. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  58. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  59. );
  60. const
  61. { firstcallparan without varspez we don't count the ref }
  62. {$ifdef extdebug}
  63. count_ref : boolean = true;
  64. {$endif def extdebug}
  65. get_para_resulttype : boolean = false;
  66. allow_array_constructor : boolean = false;
  67. { is overloading of this operator allowed for this
  68. binary operator }
  69. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  70. { is overloading of this operator allowed for this
  71. unary operator }
  72. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  73. { check operator args and result type }
  74. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  75. function isbinaryoverloaded(var t : tnode) : boolean;
  76. { Register Allocation }
  77. procedure make_not_regable(p : tnode);
  78. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  79. { subroutine handling }
  80. function is_procsym_load(p:tnode):boolean;
  81. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  82. {
  83. type
  84. tvarstaterequire = (vsr_can_be_undefined,vsr_must_be_valid,
  85. vsr_is_used_after,vsr_must_be_valid_and_is_used_after); }
  86. { sets varsym varstate field correctly }
  87. procedure unset_varstate(p : tnode);
  88. procedure set_varstate(p : tnode;must_be_valid : boolean);
  89. { sets the callunique flag, if the node is a vecn, }
  90. { takes care of type casts etc. }
  91. procedure set_unique(p : tnode);
  92. function valid_for_formal_var(p : tnode) : boolean;
  93. function valid_for_formal_const(p : tnode) : boolean;
  94. function valid_for_var(p:tnode):boolean;
  95. function valid_for_assignment(p:tnode):boolean;
  96. implementation
  97. uses
  98. globtype,systems,
  99. cutils,verbose,globals,
  100. symconst,symsym,symtable,
  101. defutil,defcmp,cpubase,
  102. ncnv,nld,
  103. nmem,ncal,nmat,
  104. cginfo,cgbase
  105. ;
  106. type
  107. TValidAssign=(Valid_Property,Valid_Void);
  108. TValidAssigns=set of TValidAssign;
  109. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  110. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  111. begin
  112. internal_check:=true;
  113. case ld.deftype of
  114. recorddef,
  115. variantdef :
  116. begin
  117. allowed:=true;
  118. end;
  119. procvardef :
  120. begin
  121. if (rd.deftype in [pointerdef,procdef,procvardef]) and
  122. (treetyp in [equaln,unequaln]) then
  123. begin
  124. allowed:=false;
  125. exit;
  126. end;
  127. allowed:=true;
  128. end;
  129. pointerdef :
  130. begin
  131. if ((rd.deftype in [pointerdef,classrefdef,procvardef]) or
  132. is_class_or_interface(rd)) and
  133. (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn]) then
  134. begin
  135. allowed:=false;
  136. exit;
  137. end;
  138. { don't allow operations on pointer/integer }
  139. if is_integer(rd) then
  140. begin
  141. allowed:=false;
  142. exit;
  143. end;
  144. { don't allow pchar+string }
  145. if is_pchar(ld) and
  146. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  147. (is_chararray(rd) or
  148. is_char(rd) or
  149. (rd.deftype=stringdef)) then
  150. begin
  151. allowed:=false;
  152. exit;
  153. end;
  154. allowed:=true;
  155. end;
  156. arraydef :
  157. begin
  158. { not mmx }
  159. if (cs_mmx in aktlocalswitches) and
  160. is_mmx_able_array(ld) then
  161. begin
  162. allowed:=false;
  163. exit;
  164. end;
  165. { not chararray+[char,string,chararray] }
  166. if is_chararray(ld) and
  167. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  168. (is_char(rd) or
  169. is_pchar(rd) or
  170. is_integer(rd) or
  171. (rd.deftype=stringdef) or
  172. is_chararray(rd) or
  173. (rt=niln)) then
  174. begin
  175. allowed:=false;
  176. exit;
  177. end;
  178. { dynamic array compare with niln }
  179. if is_dynamic_array(ld) and
  180. (rt=niln) and
  181. (treetyp in [equaln,unequaln]) then
  182. begin
  183. allowed:=false;
  184. exit;
  185. end;
  186. allowed:=true;
  187. end;
  188. objectdef :
  189. begin
  190. { <> and = are defined for classes }
  191. if (treetyp in [equaln,unequaln]) and
  192. is_class_or_interface(ld) then
  193. begin
  194. allowed:=false;
  195. exit;
  196. end;
  197. allowed:=true;
  198. end;
  199. stringdef :
  200. begin
  201. if ((rd.deftype=stringdef) or
  202. is_char(rd) or
  203. is_pchar(rd) or
  204. is_chararray(rd)) and
  205. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) then
  206. begin
  207. allowed:=false;
  208. exit;
  209. end;
  210. allowed:=true;
  211. end;
  212. else
  213. internal_check:=false;
  214. end;
  215. end;
  216. var
  217. allowed : boolean;
  218. begin
  219. { power ** is always possible }
  220. if (treetyp=starstarn) then
  221. begin
  222. isbinaryoperatoroverloadable:=true;
  223. exit;
  224. end;
  225. { order of arguments does not matter so we have to check also
  226. the reversed order }
  227. allowed:=false;
  228. if not internal_check(treetyp,ld,lt,rd,rt,allowed) then
  229. internal_check(treetyp,rd,rt,ld,lt,allowed);
  230. isbinaryoperatoroverloadable:=allowed;
  231. end;
  232. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  233. var
  234. eq : tequaltype;
  235. conv : tconverttype;
  236. pd : tprocdef;
  237. begin
  238. isunaryoperatoroverloadable:=false;
  239. case treetyp of
  240. assignn :
  241. begin
  242. eq:=compare_defs_ext(rd,dd,nothingn,true,false,conv,pd);
  243. if eq<>te_incompatible then
  244. begin
  245. isunaryoperatoroverloadable:=false;
  246. exit;
  247. end;
  248. isunaryoperatoroverloadable:=true;
  249. end;
  250. subn :
  251. begin
  252. if is_integer(rd) or
  253. (rd.deftype=floatdef) then
  254. begin
  255. isunaryoperatoroverloadable:=false;
  256. exit;
  257. end;
  258. {$ifdef SUPPORT_MMX}
  259. if (cs_mmx in aktlocalswitches) and
  260. is_mmx_able_array(rd) then
  261. begin
  262. isunaryoperatoroverloadable:=false;
  263. exit;
  264. end;
  265. {$endif SUPPORT_MMX}
  266. isunaryoperatoroverloadable:=true;
  267. end;
  268. notn :
  269. begin
  270. if is_integer(rd) or
  271. is_boolean(rd) then
  272. begin
  273. isunaryoperatoroverloadable:=false;
  274. exit;
  275. end;
  276. {$ifdef SUPPORT_MMX}
  277. if (cs_mmx in aktlocalswitches) and
  278. is_mmx_able_array(rd) then
  279. begin
  280. isunaryoperatoroverloadable:=false;
  281. exit;
  282. end;
  283. {$endif SUPPORT_MMX}
  284. isunaryoperatoroverloadable:=true;
  285. end;
  286. end;
  287. end;
  288. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  289. var
  290. ld,rd,dd : tdef;
  291. i : longint;
  292. begin
  293. case pf.parast.symindex.count of
  294. 2 : begin
  295. isoperatoracceptable:=false;
  296. for i:=1 to tok2nodes do
  297. if tok2node[i].tok=optoken then
  298. begin
  299. ld:=tvarsym(pf.parast.symindex.first).vartype.def;
  300. rd:=tvarsym(pf.parast.symindex.first.indexnext).vartype.def;
  301. dd:=pf.rettype.def;
  302. isoperatoracceptable:=
  303. tok2node[i].op_overloading_supported and
  304. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  305. break;
  306. end;
  307. end;
  308. 1 : begin
  309. rd:=tvarsym(pf.parast.symindex.first).vartype.def;
  310. dd:=pf.rettype.def;
  311. for i:=1 to tok2nodes do
  312. if tok2node[i].tok=optoken then
  313. begin
  314. isoperatoracceptable:=
  315. tok2node[i].op_overloading_supported and
  316. isunaryoperatoroverloadable(rd,dd,tok2node[i].nod);
  317. break;
  318. end;
  319. end;
  320. else
  321. isoperatoracceptable:=false;
  322. end;
  323. end;
  324. function isbinaryoverloaded(var t : tnode) : boolean;
  325. var
  326. rd,ld : tdef;
  327. optoken : ttoken;
  328. operpd : tprocdef;
  329. ht : tnode;
  330. begin
  331. isbinaryoverloaded:=false;
  332. operpd:=nil;
  333. { load easier access variables }
  334. ld:=tbinarynode(t).left.resulttype.def;
  335. rd:=tbinarynode(t).right.resulttype.def;
  336. if isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  337. begin
  338. isbinaryoverloaded:=true;
  339. case t.nodetype of
  340. equaln,
  341. unequaln :
  342. optoken:=_EQUAL;
  343. addn:
  344. optoken:=_PLUS;
  345. subn:
  346. optoken:=_MINUS;
  347. muln:
  348. optoken:=_STAR;
  349. starstarn:
  350. optoken:=_STARSTAR;
  351. slashn:
  352. optoken:=_SLASH;
  353. ltn:
  354. optoken:=tokens._lt;
  355. gtn:
  356. optoken:=tokens._gt;
  357. lten:
  358. optoken:=_lte;
  359. gten:
  360. optoken:=_gte;
  361. symdifn :
  362. optoken:=_SYMDIF;
  363. modn :
  364. optoken:=_OP_MOD;
  365. orn :
  366. optoken:=_OP_OR;
  367. xorn :
  368. optoken:=_OP_XOR;
  369. andn :
  370. optoken:=_OP_AND;
  371. divn :
  372. optoken:=_OP_DIV;
  373. shln :
  374. optoken:=_OP_SHL;
  375. shrn :
  376. optoken:=_OP_SHR;
  377. else
  378. exit;
  379. end;
  380. { check if the operator contains overloaded procdefs }
  381. if overloaded_operators[optoken]=nil then
  382. begin
  383. CGMessage(parser_e_operator_not_overloaded);
  384. isbinaryoverloaded:=false;
  385. exit;
  386. end;
  387. { Check if the assignment is available, if not then
  388. give a message that the types are not compatible }
  389. if optoken in [_EQUAL] then
  390. begin
  391. operpd:=overloaded_operators[optoken].search_procdef_binary_operator(ld,rd);
  392. if not assigned(operpd) then
  393. begin
  394. CGMessage2(type_e_incompatible_types,ld.typename,rd.typename);
  395. isbinaryoverloaded:=false;
  396. exit;
  397. end;
  398. end;
  399. { the nil as symtable signs firstcalln that this is
  400. an overloaded operator }
  401. inc(overloaded_operators[optoken].refs);
  402. ht:=ccallnode.create(nil,overloaded_operators[optoken],nil,nil);
  403. { we already know the procdef to use for equal, so it can
  404. skip the overload choosing in callnode.det_resulttype }
  405. if assigned(operpd) then
  406. tcallnode(ht).procdefinition:=operpd;
  407. { we need copies, because the originals will be destroyed when we give a }
  408. { changed node back to firstpass! (JM) }
  409. if assigned(tbinarynode(t).left) then
  410. if assigned(tbinarynode(t).right) then
  411. tcallnode(ht).left :=
  412. ccallparanode.create(tbinarynode(t).right.getcopy,
  413. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  414. else
  415. tcallnode(ht).left :=
  416. ccallparanode.create(nil,
  417. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  418. else if assigned(tbinarynode(t).right) then
  419. tcallnode(ht).left :=
  420. ccallparanode.create(tbinarynode(t).right.getcopy,
  421. ccallparanode.create(nil,nil));
  422. if t.nodetype=unequaln then
  423. ht:=cnotnode.create(ht);
  424. t:=ht;
  425. end;
  426. end;
  427. {****************************************************************************
  428. Register Calculation
  429. ****************************************************************************}
  430. { marks an lvalue as "unregable" }
  431. procedure make_not_regable(p : tnode);
  432. begin
  433. case p.nodetype of
  434. typeconvn :
  435. make_not_regable(ttypeconvnode(p).left);
  436. loadn :
  437. if tloadnode(p).symtableentry.typ=varsym then
  438. tvarsym(tloadnode(p).symtableentry).varoptions:=tvarsym(tloadnode(p).symtableentry).varoptions-[vo_regable,vo_fpuregable];
  439. end;
  440. end;
  441. { calculates the needed registers for a binary operator }
  442. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  443. begin
  444. p.left_right_max;
  445. { Only when the difference between the left and right registers < the
  446. wanted registers allocate the amount of registers }
  447. if assigned(p.left) then
  448. begin
  449. if assigned(p.right) then
  450. begin
  451. { the location must be already filled in because we need it to }
  452. { calculate the necessary number of registers (JM) }
  453. if p.expectloc = LOC_INVALID then
  454. internalerror(200110101);
  455. if (abs(p.left.registers32-p.right.registers32)<r32) or
  456. ((p.expectloc = LOC_FPUREGISTER) and
  457. (p.right.registersfpu <= p.left.registersfpu) and
  458. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  459. (p.left.registers32 < p.right.registers32)) then
  460. inc(p.registers32,r32);
  461. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  462. inc(p.registersfpu,fpu);
  463. {$ifdef SUPPORT_MMX}
  464. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  465. inc(p.registersmmx,mmx);
  466. {$endif SUPPORT_MMX}
  467. { the following is a little bit guessing but I think }
  468. { it's the only way to solve same internalerrors: }
  469. { if the left and right node both uses registers }
  470. { and return a mem location, but the current node }
  471. { doesn't use an integer register we get probably }
  472. { trouble when restoring a node }
  473. if (p.left.registers32=p.right.registers32) and
  474. (p.registers32=p.left.registers32) and
  475. (p.registers32>0) and
  476. (p.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  477. (p.right.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  478. inc(p.registers32);
  479. end
  480. else
  481. begin
  482. if (p.left.registers32<r32) then
  483. inc(p.registers32,r32);
  484. if (p.left.registersfpu<fpu) then
  485. inc(p.registersfpu,fpu);
  486. {$ifdef SUPPORT_MMX}
  487. if (p.left.registersmmx<mmx) then
  488. inc(p.registersmmx,mmx);
  489. {$endif SUPPORT_MMX}
  490. end;
  491. end;
  492. { error CGMessage, if more than 8 floating point }
  493. { registers are needed }
  494. { if p.registersfpu>maxfpuregs then
  495. CGMessage(cg_e_too_complex_expr); now pushed if needed PM }
  496. end;
  497. {****************************************************************************
  498. Subroutine Handling
  499. ****************************************************************************}
  500. function is_procsym_load(p:tnode):boolean;
  501. begin
  502. { ignore vecn,subscriptn }
  503. repeat
  504. case p.nodetype of
  505. vecn :
  506. p:=tvecnode(p).left;
  507. subscriptn :
  508. p:=tsubscriptnode(p).left;
  509. else
  510. break;
  511. end;
  512. until false;
  513. is_procsym_load:=((p.nodetype=loadn) and (tloadnode(p).symtableentry.typ=procsym)) or
  514. ((p.nodetype=addrn) and (taddrnode(p).left.nodetype=loadn)
  515. and (tloadnode(taddrnode(p).left).symtableentry.typ=procsym)) ;
  516. end;
  517. { local routines can't be assigned to procvars }
  518. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  519. begin
  520. if (from_def.parast.symtablelevel>normal_function_level) and
  521. (to_def.deftype=procvardef) then
  522. CGMessage(type_e_cannot_local_proc_to_procvar);
  523. end;
  524. procedure set_varstate(p : tnode;must_be_valid : boolean);
  525. var
  526. hsym : tvarsym;
  527. begin
  528. while assigned(p) do
  529. begin
  530. if (nf_varstateset in p.flags) then
  531. exit;
  532. include(p.flags,nf_varstateset);
  533. case p.nodetype of
  534. typeconvn :
  535. begin
  536. case ttypeconvnode(p).convtype of
  537. tc_cchar_2_pchar,
  538. tc_cstring_2_pchar,
  539. tc_array_2_pointer :
  540. must_be_valid:=false;
  541. tc_pchar_2_string,
  542. tc_pointer_2_array :
  543. must_be_valid:=true;
  544. end;
  545. p:=tunarynode(p).left;
  546. end;
  547. subscriptn :
  548. p:=tunarynode(p).left;
  549. vecn:
  550. begin
  551. set_varstate(tbinarynode(p).right,true);
  552. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  553. must_be_valid:=true;
  554. p:=tunarynode(p).left;
  555. end;
  556. { do not parse calln }
  557. calln :
  558. break;
  559. callparan :
  560. begin
  561. set_varstate(tbinarynode(p).right,must_be_valid);
  562. p:=tunarynode(p).left;
  563. end;
  564. loadn :
  565. begin
  566. if (tloadnode(p).symtableentry.typ=varsym) then
  567. begin
  568. hsym:=tvarsym(tloadnode(p).symtableentry);
  569. if must_be_valid and (nf_first_use in p.flags) then
  570. begin
  571. if (hsym.varstate=vs_declared_and_first_found) or
  572. (hsym.varstate=vs_set_but_first_not_passed) then
  573. begin
  574. if (assigned(hsym.owner) and
  575. assigned(current_procinfo) and
  576. (hsym.owner=current_procinfo.procdef.localst)) then
  577. begin
  578. if (vo_is_funcret in hsym.varoptions) then
  579. CGMessage(sym_w_function_result_not_set)
  580. else
  581. if tloadnode(p).symtable.symtabletype=localsymtable then
  582. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  583. else
  584. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  585. end;
  586. end;
  587. end;
  588. if (nf_first_use in p.flags) then
  589. begin
  590. if hsym.varstate=vs_declared_and_first_found then
  591. begin
  592. { this can only happen at left of an assignment, no ? PM }
  593. if (parsing_para_level=0) and not must_be_valid then
  594. hsym.varstate:=vs_assigned
  595. else
  596. hsym.varstate:=vs_used;
  597. end
  598. else
  599. if hsym.varstate=vs_set_but_first_not_passed then
  600. hsym.varstate:=vs_used;
  601. exclude(p.flags,nf_first_use);
  602. end
  603. else
  604. begin
  605. if (hsym.varstate=vs_assigned) and
  606. (must_be_valid or (parsing_para_level>0) or
  607. (p.resulttype.def.deftype=procvardef)) then
  608. hsym.varstate:=vs_used;
  609. if (hsym.varstate=vs_declared_and_first_found) and
  610. (must_be_valid or (parsing_para_level>0) or
  611. (p.resulttype.def.deftype=procvardef)) then
  612. hsym.varstate:=vs_set_but_first_not_passed;
  613. end;
  614. end;
  615. break;
  616. end;
  617. else
  618. break;
  619. end;{case }
  620. end;
  621. end;
  622. procedure unset_varstate(p : tnode);
  623. begin
  624. while assigned(p) do
  625. begin
  626. exclude(p.flags,nf_varstateset);
  627. case p.nodetype of
  628. typeconvn,
  629. subscriptn,
  630. vecn :
  631. p:=tunarynode(p).left;
  632. else
  633. break;
  634. end;
  635. end;
  636. end;
  637. procedure set_unique(p : tnode);
  638. begin
  639. while assigned(p) do
  640. begin
  641. case p.nodetype of
  642. vecn:
  643. begin
  644. include(p.flags,nf_callunique);
  645. break;
  646. end;
  647. typeconvn,
  648. subscriptn,
  649. derefn:
  650. p:=tunarynode(p).left;
  651. else
  652. break;
  653. end;
  654. end;
  655. end;
  656. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  657. var
  658. hp : tnode;
  659. gotwith,
  660. gotsubscript,
  661. gotpointer,
  662. gotvec,
  663. gotclass,
  664. gotderef : boolean;
  665. fromdef,
  666. todef : tdef;
  667. begin
  668. valid_for_assign:=false;
  669. gotsubscript:=false;
  670. gotvec:=false;
  671. gotderef:=false;
  672. gotclass:=false;
  673. gotpointer:=false;
  674. gotwith:=false;
  675. hp:=p;
  676. if not(valid_void in opts) and
  677. is_void(hp.resulttype.def) then
  678. begin
  679. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  680. exit;
  681. end;
  682. while assigned(hp) do
  683. begin
  684. { property allowed? calln has a property check itself }
  685. if (nf_isproperty in hp.flags) then
  686. begin
  687. if (valid_property in opts) then
  688. valid_for_assign:=true
  689. else
  690. begin
  691. { check return type }
  692. case hp.resulttype.def.deftype of
  693. pointerdef :
  694. gotpointer:=true;
  695. objectdef :
  696. gotclass:=is_class_or_interface(hp.resulttype.def);
  697. recorddef, { handle record like class it needs a subscription }
  698. classrefdef :
  699. gotclass:=true;
  700. end;
  701. { 1. if it returns a pointer and we've found a deref,
  702. 2. if it returns a class or record and a subscription or with is found }
  703. if (gotpointer and gotderef) or
  704. (gotclass and (gotsubscript or gotwith)) then
  705. valid_for_assign:=true
  706. else
  707. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  708. end;
  709. exit;
  710. end;
  711. case hp.nodetype of
  712. temprefn :
  713. begin
  714. valid_for_assign := true;
  715. exit;
  716. end;
  717. derefn :
  718. begin
  719. gotderef:=true;
  720. hp:=tderefnode(hp).left;
  721. end;
  722. typeconvn :
  723. begin
  724. { typecast sizes must match, exceptions:
  725. - from formaldef
  726. - from void
  727. - typecast from pointer to array }
  728. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  729. todef:=hp.resulttype.def;
  730. if not((fromdef.deftype=formaldef) or
  731. is_void(fromdef) or
  732. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  733. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  734. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  735. (fromdef.size<>todef.size) then
  736. begin
  737. { in TP it is allowed to typecast to smaller types }
  738. if not(m_tp7 in aktmodeswitches) or
  739. (todef.size>fromdef.size) then
  740. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  741. end;
  742. case hp.resulttype.def.deftype of
  743. pointerdef :
  744. gotpointer:=true;
  745. objectdef :
  746. gotclass:=is_class_or_interface(hp.resulttype.def);
  747. classrefdef :
  748. gotclass:=true;
  749. arraydef :
  750. begin
  751. { pointer -> array conversion is done then we need to see it
  752. as a deref, because a ^ is then not required anymore }
  753. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  754. gotderef:=true;
  755. end;
  756. end;
  757. hp:=ttypeconvnode(hp).left;
  758. end;
  759. vecn :
  760. begin
  761. gotvec:=true;
  762. hp:=tunarynode(hp).left;
  763. end;
  764. asn :
  765. hp:=tunarynode(hp).left;
  766. subscriptn :
  767. begin
  768. gotsubscript:=true;
  769. { a class/interface access is an implicit }
  770. { dereferencing }
  771. hp:=tsubscriptnode(hp).left;
  772. if is_class_or_interface(hp.resulttype.def) then
  773. gotderef:=true;
  774. end;
  775. subn,
  776. addn :
  777. begin
  778. { Allow add/sub operators on a pointer, or an integer
  779. and a pointer typecast and deref has been found }
  780. if ((hp.resulttype.def.deftype=pointerdef) or
  781. (is_integer(hp.resulttype.def) and gotpointer)) and
  782. gotderef then
  783. valid_for_assign:=true
  784. else
  785. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  786. exit;
  787. end;
  788. addrn :
  789. begin
  790. if gotderef or
  791. (nf_procvarload in hp.flags) then
  792. valid_for_assign:=true
  793. else
  794. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  795. exit;
  796. end;
  797. calln :
  798. begin
  799. { check return type }
  800. case hp.resulttype.def.deftype of
  801. arraydef :
  802. begin
  803. { dynamic arrays are allowed when there is also a
  804. vec node }
  805. if is_dynamic_array(hp.resulttype.def) and
  806. gotvec then
  807. begin
  808. gotderef:=true;
  809. gotpointer:=true;
  810. end;
  811. end;
  812. pointerdef :
  813. gotpointer:=true;
  814. objectdef :
  815. gotclass:=is_class_or_interface(hp.resulttype.def);
  816. recorddef, { handle record like class it needs a subscription }
  817. classrefdef :
  818. gotclass:=true;
  819. end;
  820. { 1. if it returns a pointer and we've found a deref,
  821. 2. if it returns a class or record and a subscription or with is found }
  822. if (gotpointer and gotderef) or
  823. (gotclass and (gotsubscript or gotwith)) then
  824. valid_for_assign:=true
  825. else
  826. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  827. exit;
  828. end;
  829. loadn :
  830. begin
  831. case tloadnode(hp).symtableentry.typ of
  832. absolutesym,
  833. varsym :
  834. begin
  835. if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  836. begin
  837. { allow p^:= constructions with p is const parameter }
  838. if gotderef then
  839. valid_for_assign:=true
  840. else
  841. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  842. exit;
  843. end;
  844. { Are we at a with symtable, then we need to process the
  845. withrefnode also to check for maybe a const load }
  846. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  847. begin
  848. { continue with processing the withref node }
  849. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  850. gotwith:=true;
  851. end
  852. else
  853. begin
  854. { set the assigned flag for varsyms }
  855. if (tvarsym(tloadnode(hp).symtableentry).varstate=vs_declared) then
  856. tvarsym(tloadnode(hp).symtableentry).varstate:=vs_assigned;
  857. valid_for_assign:=true;
  858. exit;
  859. end;
  860. end;
  861. typedconstsym :
  862. begin
  863. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  864. valid_for_assign:=true
  865. else
  866. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  867. exit;
  868. end;
  869. else
  870. begin
  871. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  872. exit;
  873. end;
  874. end;
  875. end;
  876. else
  877. begin
  878. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  879. exit;
  880. end;
  881. end;
  882. end;
  883. end;
  884. function valid_for_var(p:tnode):boolean;
  885. begin
  886. valid_for_var:=valid_for_assign(p,[]);
  887. end;
  888. function valid_for_formal_var(p : tnode) : boolean;
  889. begin
  890. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  891. end;
  892. function valid_for_formal_const(p : tnode) : boolean;
  893. var
  894. v : boolean;
  895. begin
  896. { p must have been firstpass'd before }
  897. { accept about anything but not a statement ! }
  898. case p.nodetype of
  899. calln,
  900. statementn,
  901. addrn :
  902. begin
  903. { addrn is not allowed as this generate a constant value,
  904. but a tp procvar are allowed (PFV) }
  905. if nf_procvarload in p.flags then
  906. v:=true
  907. else
  908. v:=false;
  909. end;
  910. else
  911. v:=true;
  912. end;
  913. valid_for_formal_const:=v;
  914. end;
  915. function valid_for_assignment(p:tnode):boolean;
  916. begin
  917. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  918. end;
  919. end.
  920. {
  921. $Log$
  922. Revision 1.64 2003-06-13 21:19:30 peter
  923. * current_procdef removed, use current_procinfo.procdef instead
  924. Revision 1.63 2003/05/09 17:47:02 peter
  925. * self moved to hidden parameter
  926. * removed hdisposen,hnewn,selfn
  927. Revision 1.62 2003/04/27 11:21:32 peter
  928. * aktprocdef renamed to current_procinfo.procdef
  929. * procinfo renamed to current_procinfo
  930. * procinfo will now be stored in current_module so it can be
  931. cleaned up properly
  932. * gen_main_procsym changed to create_main_proc and release_main_proc
  933. to also generate a tprocinfo structure
  934. * fixed unit implicit initfinal
  935. Revision 1.61 2003/04/27 07:29:50 peter
  936. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  937. a new procdef declaration
  938. * aktprocsym removed
  939. * lexlevel removed, use symtable.symtablelevel instead
  940. * implicit init/final code uses the normal genentry/genexit
  941. * funcret state checking updated for new funcret handling
  942. Revision 1.60 2003/04/25 20:59:33 peter
  943. * removed funcretn,funcretsym, function result is now in varsym
  944. and aliases for result and function name are added using absolutesym
  945. * vs_hidden parameter for funcret passed in parameter
  946. * vs_hidden fixes
  947. * writenode changed to printnode and released from extdebug
  948. * -vp option added to generate a tree.log with the nodetree
  949. * nicer printnode for statements, callnode
  950. Revision 1.59 2003/04/22 23:50:22 peter
  951. * firstpass uses expectloc
  952. * checks if there are differences between the expectloc and
  953. location.loc from secondpass in EXTDEBUG
  954. Revision 1.58 2003/01/03 17:17:26 peter
  955. * use compare_def_ext to test if assignn operator is allowed
  956. Revision 1.57 2003/01/02 22:21:19 peter
  957. * fixed previous operator change
  958. Revision 1.56 2003/01/02 19:50:21 peter
  959. * fixed operator checking for objects
  960. * made binary operator checking simpeler
  961. Revision 1.55 2002/12/27 18:06:32 peter
  962. * fix overload error for dynarr:=nil
  963. Revision 1.54 2002/12/22 16:34:49 peter
  964. * proc-procvar crash fixed (tw2277)
  965. Revision 1.53 2002/12/11 22:39:24 peter
  966. * better error message when no operator is found for equal
  967. Revision 1.52 2002/11/27 22:11:59 peter
  968. * rewrote isbinaryoverloadable to use a case. it's now much easier
  969. to understand what is happening
  970. Revision 1.51 2002/11/25 17:43:17 peter
  971. * splitted defbase in defutil,symutil,defcmp
  972. * merged isconvertable and is_equal into compare_defs(_ext)
  973. * made operator search faster by walking the list only once
  974. Revision 1.50 2002/10/07 20:12:08 peter
  975. * ugly hack to fix tb0411
  976. Revision 1.49 2002/10/05 00:47:03 peter
  977. * support dynamicarray<>nil
  978. Revision 1.48 2002/10/04 21:13:59 peter
  979. * ignore vecn,subscriptn when checking for a procvar loadn
  980. Revision 1.47 2002/09/16 18:09:34 peter
  981. * set_funcret_valid fixed when result was already used in a nested
  982. procedure
  983. Revision 1.46 2002/07/20 11:57:53 florian
  984. * types.pas renamed to defbase.pas because D6 contains a types
  985. unit so this would conflicts if D6 programms are compiled
  986. + Willamette/SSE2 instructions to assembler added
  987. Revision 1.45 2002/05/18 13:34:08 peter
  988. * readded missing revisions
  989. Revision 1.44 2002/05/16 19:46:37 carl
  990. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  991. + try to fix temp allocation (still in ifdef)
  992. + generic constructor calls
  993. + start of tassembler / tmodulebase class cleanup
  994. Revision 1.42 2002/04/02 17:11:28 peter
  995. * tlocation,treference update
  996. * LOC_CONSTANT added for better constant handling
  997. * secondadd splitted in multiple routines
  998. * location_force_reg added for loading a location to a register
  999. of a specified size
  1000. * secondassignment parses now first the right and then the left node
  1001. (this is compatible with Kylix). This saves a lot of push/pop especially
  1002. with string operations
  1003. * adapted some routines to use the new cg methods
  1004. Revision 1.41 2002/01/16 09:33:46 jonas
  1005. * no longer allow assignments to pointer expressions (unless there's a
  1006. deref), reported by John Lee
  1007. }