htypechk.pas 40 KB

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