aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-05-30 12:05:35 +0100
committerAleksey Kladov <[email protected]>2019-05-30 12:06:28 +0100
commite6545cc647829091dd5aae5753bd4f5302becab6 (patch)
tree640de902fede2421a299ba22609fbd9db5b07231 /crates/ra_hir
parent6b88735fe6cd3b259816c7c90a2675ee057c9e4c (diff)
add ModuleDef::BuiltInType
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model.rs15
-rw-r--r--crates/ra_hir/src/lib.rs5
-rw-r--r--crates/ra_hir/src/ty/infer.rs3
-rw-r--r--crates/ra_hir/src/ty/lower.rs36
-rw-r--r--crates/ra_hir/src/ty/primitive.rs12
5 files changed, 61 insertions, 10 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 69496b624..3f77850dd 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -9,7 +9,7 @@ use crate::{
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, 10 nameres::{ModuleScope, Namespace, ImportId, CrateModuleId},
11 expr::{Body, BodySourceMap, validation::ExprValidator}, 11 expr::{Body, BodySourceMap, validation::ExprValidator},
12 ty::{TraitRef, InferenceResult}, 12 ty::{TraitRef, InferenceResult, primitive::{IntTy, FloatTy}},
13 adt::{EnumVariantId, StructFieldId, VariantDef}, 13 adt::{EnumVariantId, StructFieldId, VariantDef},
14 generics::HasGenericParams, 14 generics::HasGenericParams,
15 docs::{Documentation, Docs, docs_from_ast}, 15 docs::{Documentation, Docs, docs_from_ast},
@@ -75,6 +75,15 @@ pub struct Module {
75 pub(crate) module_id: CrateModuleId, 75 pub(crate) module_id: CrateModuleId,
76} 76}
77 77
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
79pub enum BuiltinType {
80 Char,
81 Bool,
82 Str,
83 Int(IntTy),
84 Float(FloatTy),
85}
86
78/// The defs which can be visible in the module. 87/// The defs which can be visible in the module.
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 88#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
80pub enum ModuleDef { 89pub enum ModuleDef {
@@ -89,6 +98,7 @@ pub enum ModuleDef {
89 Static(Static), 98 Static(Static),
90 Trait(Trait), 99 Trait(Trait),
91 TypeAlias(TypeAlias), 100 TypeAlias(TypeAlias),
101 BuiltinType(BuiltinType),
92} 102}
93impl_froms!( 103impl_froms!(
94 ModuleDef: Module, 104 ModuleDef: Module,
@@ -100,7 +110,8 @@ impl_froms!(
100 Const, 110 Const,
101 Static, 111 Static,
102 Trait, 112 Trait,
103 TypeAlias 113 TypeAlias,
114 BuiltinType
104); 115);
105 116
106pub enum ModuleSource { 117pub enum ModuleSource {
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index cb09c60f8..3e00eea26 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -6,7 +6,7 @@
6//! applied. So, the relation between syntax and HIR is many-to-one. 6//! applied. So, the relation between syntax and HIR is many-to-one.
7 7
8macro_rules! impl_froms { 8macro_rules! impl_froms {
9 ($e:ident: $($v:ident), *) => { 9 ($e:ident: $($v:ident),*) => {
10 $( 10 $(
11 impl From<$v> for $e { 11 impl From<$v> for $e {
12 fn from(it: $v) -> $e { 12 fn from(it: $v) -> $e {
@@ -80,5 +80,6 @@ pub use self::code_model::{
80 Function, FnSignature, 80 Function, FnSignature,
81 StructField, FieldSource, 81 StructField, FieldSource,
82 Static, Const, ConstSignature, 82 Static, Const, ConstSignature,
83 Trait, TypeAlias, MacroDef, Container 83 Trait, TypeAlias, MacroDef, Container,
84 BuiltinType,
84}; 85};
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 7d8250292..e8ae33ead 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -649,7 +649,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
649 | TypableDef::Function(_) 649 | TypableDef::Function(_)
650 | TypableDef::Enum(_) 650 | TypableDef::Enum(_)
651 | TypableDef::Const(_) 651 | TypableDef::Const(_)
652 | TypableDef::Static(_) => (Ty::Unknown, None), 652 | TypableDef::Static(_)
653 | TypableDef::BuiltinType(_) => (Ty::Unknown, None),
653 } 654 }
654 } 655 }
655 656
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 7defa7a9b..c4cef2d7c 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -10,7 +10,7 @@ use std::iter;
10 10
11use crate::{ 11use crate::{
12 Function, Struct, Union, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static, 12 Function, Struct, Union, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static,
13 HirDatabase, 13 HirDatabase, BuiltinType,
14 type_ref::TypeRef, 14 type_ref::TypeRef,
15 name::KnownName, 15 name::KnownName,
16 nameres::Namespace, 16 nameres::Namespace,
@@ -66,7 +66,7 @@ impl Ty {
66 66
67 pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self { 67 pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self {
68 if let Some(name) = path.as_ident() { 68 if let Some(name) = path.as_ident() {
69 // FIXME handle primitive type names in resolver as well? 69 // TODO: remove this
70 if let Some(int_ty) = primitive::IntTy::from_type_name(name) { 70 if let Some(int_ty) = primitive::IntTy::from_type_name(name) {
71 return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty))); 71 return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty)));
72 } else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) { 72 } else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) {
@@ -128,7 +128,7 @@ impl Ty {
128 TypableDef::Enum(e) => Some(e.into()), 128 TypableDef::Enum(e) => Some(e.into()),
129 TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()), 129 TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()),
130 TypableDef::TypeAlias(t) => Some(t.into()), 130 TypableDef::TypeAlias(t) => Some(t.into()),
131 TypableDef::Const(_) | TypableDef::Static(_) => None, 131 TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None,
132 }; 132 };
133 substs_from_path_segment(db, resolver, segment, def_generic, false) 133 substs_from_path_segment(db, resolver, segment, def_generic, false)
134 } 134 }
@@ -149,7 +149,8 @@ impl Ty {
149 | TypableDef::Enum(_) 149 | TypableDef::Enum(_)
150 | TypableDef::Const(_) 150 | TypableDef::Const(_)
151 | TypableDef::Static(_) 151 | TypableDef::Static(_)
152 | TypableDef::TypeAlias(_) => last, 152 | TypableDef::TypeAlias(_)
153 | TypableDef::BuiltinType(_) => last,
153 TypableDef::EnumVariant(_) => { 154 TypableDef::EnumVariant(_) => {
154 // the generic args for an enum variant may be either specified 155 // the generic args for an enum variant may be either specified
155 // on the segment referring to the enum, or on the segment 156 // on the segment referring to the enum, or on the segment
@@ -299,6 +300,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace
299 (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), 300 (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t),
300 (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), 301 (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c),
301 (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c), 302 (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c),
303 (TypableDef::BuiltinType(t), Namespace::Types) => type_for_builtin(t),
302 304
303 // 'error' cases: 305 // 'error' cases:
304 (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, 306 (TypableDef::Function(_), Namespace::Types) => Ty::Unknown,
@@ -308,6 +310,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace
308 (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, 310 (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown,
309 (TypableDef::Const(_), Namespace::Types) => Ty::Unknown, 311 (TypableDef::Const(_), Namespace::Types) => Ty::Unknown,
310 (TypableDef::Static(_), Namespace::Types) => Ty::Unknown, 312 (TypableDef::Static(_), Namespace::Types) => Ty::Unknown,
313 (TypableDef::BuiltinType(_), Namespace::Values) => Ty::Unknown,
311 } 314 }
312} 315}
313 316
@@ -399,6 +402,17 @@ fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty {
399 Ty::from_hir(db, &resolver, signature.type_ref()) 402 Ty::from_hir(db, &resolver, signature.type_ref())
400} 403}
401 404
405/// Build the declared type of a static.
406fn type_for_builtin(def: BuiltinType) -> Ty {
407 Ty::simple(match def {
408 BuiltinType::Char => TypeCtor::Char,
409 BuiltinType::Bool => TypeCtor::Bool,
410 BuiltinType::Str => TypeCtor::Str,
411 BuiltinType::Int(ty) => TypeCtor::Int(ty.into()),
412 BuiltinType::Float(ty) => TypeCtor::Float(ty.into()),
413 })
414}
415
402fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { 416fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
403 let var_data = def.variant_data(db); 417 let var_data = def.variant_data(db);
404 let fields = match var_data.fields() { 418 let fields = match var_data.fields() {
@@ -477,8 +491,19 @@ pub enum TypableDef {
477 TypeAlias(TypeAlias), 491 TypeAlias(TypeAlias),
478 Const(Const), 492 Const(Const),
479 Static(Static), 493 Static(Static),
494 BuiltinType(BuiltinType),
480} 495}
481impl_froms!(TypableDef: Function, Struct, Union, Enum, EnumVariant, TypeAlias, Const, Static); 496impl_froms!(
497 TypableDef: Function,
498 Struct,
499 Union,
500 Enum,
501 EnumVariant,
502 TypeAlias,
503 Const,
504 Static,
505 BuiltinType
506);
482 507
483impl From<ModuleDef> for Option<TypableDef> { 508impl From<ModuleDef> for Option<TypableDef> {
484 fn from(def: ModuleDef) -> Option<TypableDef> { 509 fn from(def: ModuleDef) -> Option<TypableDef> {
@@ -491,6 +516,7 @@ impl From<ModuleDef> for Option<TypableDef> {
491 ModuleDef::TypeAlias(t) => t.into(), 516 ModuleDef::TypeAlias(t) => t.into(),
492 ModuleDef::Const(v) => v.into(), 517 ModuleDef::Const(v) => v.into(),
493 ModuleDef::Static(v) => v.into(), 518 ModuleDef::Static(v) => v.into(),
519 ModuleDef::BuiltinType(t) => t.into(),
494 ModuleDef::Module(_) | ModuleDef::Trait(_) => return None, 520 ModuleDef::Module(_) | ModuleDef::Trait(_) => return None,
495 }; 521 };
496 Some(res) 522 Some(res)
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs
index b37326db7..e1ab16a6f 100644
--- a/crates/ra_hir/src/ty/primitive.rs
+++ b/crates/ra_hir/src/ty/primitive.rs
@@ -30,6 +30,12 @@ pub enum UncertainIntTy {
30 Known(IntTy), 30 Known(IntTy),
31} 31}
32 32
33impl From<IntTy> for UncertainIntTy {
34 fn from(ty: IntTy) -> Self {
35 UncertainIntTy::Known(ty)
36 }
37}
38
33impl fmt::Display for UncertainIntTy { 39impl fmt::Display for UncertainIntTy {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 40 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 match *self { 41 match *self {
@@ -45,6 +51,12 @@ pub enum UncertainFloatTy {
45 Known(FloatTy), 51 Known(FloatTy),
46} 52}
47 53
54impl From<FloatTy> for UncertainFloatTy {
55 fn from(ty: FloatTy) -> Self {
56 UncertainFloatTy::Known(ty)
57 }
58}
59
48impl fmt::Display for UncertainFloatTy { 60impl fmt::Display for UncertainFloatTy {
49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50 match *self { 62 match *self {