aboutsummaryrefslogtreecommitdiff
path: root/src/comp
diff options
context:
space:
mode:
authorPatrick Walton <[email protected]>2011-01-04 12:46:11 -0800
committerPatrick Walton <[email protected]>2011-01-04 12:46:11 -0800
commit29fb238a39e94b3fad05cc1ab0a015930ce784e5 (patch)
tree753ba49a4df436a375b329d8330cd39a45aa3732 /src/comp
parentAdd support for looking up a name introduced by a 'use'. (diff)
downloadrust-29fb238a39e94b3fad05cc1ab0a015930ce784e5.tar.xz
rust-29fb238a39e94b3fad05cc1ab0a015930ce784e5.zip
rustc: Add an item type accessor
Diffstat (limited to 'src/comp')
-rw-r--r--src/comp/middle/ty.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs
index 12d17443..d4658080 100644
--- a/src/comp/middle/ty.rs
+++ b/src/comp/middle/ty.rs
@@ -543,6 +543,44 @@ fn is_fn_ty(@t fty) -> bool {
// Type accessors for AST nodes
+// Given an item, returns the associated type as well as a list of the IDs of
+// its type parameters.
+fn item_ty(@ast.item it) -> tup(vec[ast.def_id], @t) {
+ let vec[ast.ty_param] ty_params;
+ auto result_ty;
+ alt (it.node) {
+ case (ast.item_const(_, _, _, _, ?ann)) {
+ ty_params = vec();
+ result_ty = ann_to_type(ann);
+ }
+ case (ast.item_fn(_, _, ?tps, _, ?ann)) {
+ ty_params = tps;
+ result_ty = ann_to_type(ann);
+ }
+ case (ast.item_mod(_, _, _)) {
+ fail; // modules are typeless
+ }
+ case (ast.item_ty(_, _, ?tps, _, ?ann)) {
+ ty_params = tps;
+ result_ty = ann_to_type(ann);
+ }
+ case (ast.item_tag(_, _, ?tps, ?did)) {
+ ty_params = tps;
+ result_ty = plain_ty(ty_tag(did));
+ }
+ case (ast.item_obj(_, _, ?tps, _, ?ann)) {
+ ty_params = tps;
+ result_ty = ann_to_type(ann);
+ }
+ }
+
+ let vec[ast.def_id] ty_param_ids = vec();
+ for (ast.ty_param tp in ty_params) {
+ ty_param_ids += vec(tp.id);
+ }
+ ret tup(ty_param_ids, result_ty);
+}
+
fn stmt_ty(@ast.stmt s) -> @t {
alt (s.node) {
case (ast.stmt_expr(?e)) {