htypechk.pas 38 KB

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