OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This file contains the pieces of the tool that use typechecking from the go/t
ypes package. | 5 // This file contains the pieces of the tool that use typechecking from the go/t
ypes package. |
6 | 6 |
7 package main | 7 package main |
8 | 8 |
9 import ( | 9 import ( |
10 "go/ast" | 10 "go/ast" |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 } | 266 } |
267 // There must be a receiver for it to be a method call. Otherwise it is | 267 // There must be a receiver for it to be a method call. Otherwise it is |
268 // a function, not something that satisfies the error interface. | 268 // a function, not something that satisfies the error interface. |
269 if sig.Recv == nil { | 269 if sig.Recv == nil { |
270 return false | 270 return false |
271 } | 271 } |
272 // There must be no arguments. Already verified by type checking, but be
thorough. | 272 // There must be no arguments. Already verified by type checking, but be
thorough. |
273 if sig.Params().Len() > 0 { | 273 if sig.Params().Len() > 0 { |
274 return false | 274 return false |
275 } | 275 } |
276 » // Finally the real questions. | 276 » // Finally the real question. |
277 » // There must be one result. | 277 » return hasStringReturn(sig) |
278 » if sig.Results().Len() != 1 { | 278 } |
| 279 |
| 280 // returnsString reports whether expr is a call of a function that returns a str
ing. |
| 281 // If it cannot be type checked it returns true. |
| 282 func (f *File) returnsString(expr ast.Expr) bool { |
| 283 » typ := f.pkg.types[expr] |
| 284 » if typ == nil { |
| 285 » » return true |
| 286 » } |
| 287 » sig, ok := typ.(*types.Signature) |
| 288 » if !ok { |
279 return false | 289 return false |
280 } | 290 } |
281 » // It must have return type "string" from the universe. | 291 » return hasStringReturn(sig) |
282 » return sig.Results().At(0).Type() == types.Typ[types.String] | |
283 } | 292 } |
| 293 |
| 294 // hasStringReturn reports whether the function signature has exactly |
| 295 // one return value, of universe type string. |
| 296 func hasStringReturn(sig *types.Signature) bool { |
| 297 return sig.Results().Len() == 1 && sig.Results().At(0).Type() == types.T
yp[types.String] |
| 298 } |
OLD | NEW |