htypechk.pas 41 KB

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