|
| 1 | +use crate::tls; |
| 2 | +use chalk_ir::interner::{HasInterner, Interner}; |
| 3 | +use chalk_ir::{ |
| 4 | + AliasTy, ApplicationTy, AssocTypeId, CanonicalVarKinds, Goals, Lifetime, OpaqueTy, OpaqueTyId, |
| 5 | + ParameterKinds, ProgramClauseImplication, ProgramClauses, ProjectionTy, QuantifiedWhereClauses, |
| 6 | + SeparatorTraitRef, Substitution, TraitId, Ty, |
| 7 | +}; |
| 8 | +use chalk_ir::{ |
| 9 | + Goal, GoalData, LifetimeData, Parameter, ParameterData, ParameterKind, ProgramClause, |
| 10 | + ProgramClauseData, QuantifiedWhereClause, StructId, TyData, UniverseIndex, |
| 11 | +}; |
| 12 | +use lalrpop_intern::InternedString; |
| 13 | +use std::fmt; |
| 14 | +use std::fmt::Debug; |
| 15 | +use std::sync::Arc; |
| 16 | + |
| 17 | +pub type Identifier = InternedString; |
| 18 | + |
| 19 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 20 | +pub struct RawId { |
| 21 | + pub index: u32, |
| 22 | +} |
| 23 | + |
| 24 | +impl Debug for RawId { |
| 25 | + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 26 | + write!(fmt, "#{}", self.index) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// The default "interner" and the only interner used by chalk |
| 31 | +/// itself. In this interner, no interning actually occurs. |
| 32 | +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] |
| 33 | +pub struct ChalkIr; |
| 34 | + |
| 35 | +impl Interner for ChalkIr { |
| 36 | + type InternedType = Arc<TyData<ChalkIr>>; |
| 37 | + type InternedLifetime = LifetimeData<ChalkIr>; |
| 38 | + type InternedParameter = ParameterData<ChalkIr>; |
| 39 | + type InternedGoal = Arc<GoalData<ChalkIr>>; |
| 40 | + type InternedGoals = Vec<Goal<ChalkIr>>; |
| 41 | + type InternedSubstitution = Vec<Parameter<ChalkIr>>; |
| 42 | + type InternedProgramClause = ProgramClauseData<ChalkIr>; |
| 43 | + type InternedProgramClauses = Vec<ProgramClause<ChalkIr>>; |
| 44 | + type InternedQuantifiedWhereClauses = Vec<QuantifiedWhereClause<ChalkIr>>; |
| 45 | + type InternedParameterKinds = Vec<ParameterKind<()>>; |
| 46 | + type InternedCanonicalVarKinds = Vec<ParameterKind<UniverseIndex>>; |
| 47 | + type DefId = RawId; |
| 48 | + type Identifier = Identifier; |
| 49 | + |
| 50 | + fn debug_struct_id( |
| 51 | + type_kind_id: StructId<ChalkIr>, |
| 52 | + fmt: &mut fmt::Formatter<'_>, |
| 53 | + ) -> Option<fmt::Result> { |
| 54 | + tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt))) |
| 55 | + } |
| 56 | + |
| 57 | + fn debug_trait_id( |
| 58 | + type_kind_id: TraitId<ChalkIr>, |
| 59 | + fmt: &mut fmt::Formatter<'_>, |
| 60 | + ) -> Option<fmt::Result> { |
| 61 | + tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt))) |
| 62 | + } |
| 63 | + |
| 64 | + fn debug_assoc_type_id( |
| 65 | + id: AssocTypeId<ChalkIr>, |
| 66 | + fmt: &mut fmt::Formatter<'_>, |
| 67 | + ) -> Option<fmt::Result> { |
| 68 | + tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) |
| 69 | + } |
| 70 | + |
| 71 | + fn debug_opaque_ty_id( |
| 72 | + id: OpaqueTyId<ChalkIr>, |
| 73 | + fmt: &mut fmt::Formatter<'_>, |
| 74 | + ) -> Option<fmt::Result> { |
| 75 | + tls::with_current_program(|prog| Some(prog?.debug_opaque_ty_id(id, fmt))) |
| 76 | + } |
| 77 | + |
| 78 | + fn debug_alias(alias: &AliasTy<ChalkIr>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { |
| 79 | + tls::with_current_program(|prog| Some(prog?.debug_alias(alias, fmt))) |
| 80 | + } |
| 81 | + |
| 82 | + fn debug_projection_ty( |
| 83 | + proj: &ProjectionTy<ChalkIr>, |
| 84 | + fmt: &mut fmt::Formatter<'_>, |
| 85 | + ) -> Option<fmt::Result> { |
| 86 | + tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt))) |
| 87 | + } |
| 88 | + |
| 89 | + fn debug_opaque_ty( |
| 90 | + opaque_ty: &OpaqueTy<ChalkIr>, |
| 91 | + fmt: &mut fmt::Formatter<'_>, |
| 92 | + ) -> Option<fmt::Result> { |
| 93 | + tls::with_current_program(|prog| Some(prog?.debug_opaque_ty(opaque_ty, fmt))) |
| 94 | + } |
| 95 | + |
| 96 | + fn debug_ty(ty: &Ty<ChalkIr>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { |
| 97 | + tls::with_current_program(|prog| Some(prog?.debug_ty(ty, fmt))) |
| 98 | + } |
| 99 | + |
| 100 | + fn debug_lifetime( |
| 101 | + lifetime: &Lifetime<ChalkIr>, |
| 102 | + fmt: &mut fmt::Formatter<'_>, |
| 103 | + ) -> Option<fmt::Result> { |
| 104 | + tls::with_current_program(|prog| Some(prog?.debug_lifetime(lifetime, fmt))) |
| 105 | + .or_else(|| Some(write!(fmt, "{:?}", lifetime.interned))) |
| 106 | + } |
| 107 | + |
| 108 | + fn debug_parameter( |
| 109 | + parameter: &Parameter<ChalkIr>, |
| 110 | + fmt: &mut fmt::Formatter<'_>, |
| 111 | + ) -> Option<fmt::Result> { |
| 112 | + tls::with_current_program(|prog| Some(prog?.debug_parameter(parameter, fmt))) |
| 113 | + } |
| 114 | + |
| 115 | + fn debug_parameter_kinds( |
| 116 | + parameter_kinds: &ParameterKinds<Self>, |
| 117 | + fmt: &mut fmt::Formatter<'_>, |
| 118 | + ) -> Option<fmt::Result> { |
| 119 | + tls::with_current_program(|prog| Some(prog?.debug_parameter_kinds(parameter_kinds, fmt))) |
| 120 | + } |
| 121 | + |
| 122 | + fn debug_parameter_kinds_with_angles( |
| 123 | + parameter_kinds: &ParameterKinds<Self>, |
| 124 | + fmt: &mut fmt::Formatter<'_>, |
| 125 | + ) -> Option<fmt::Result> { |
| 126 | + tls::with_current_program(|prog| { |
| 127 | + Some(prog?.debug_parameter_kinds_with_angles(parameter_kinds, fmt)) |
| 128 | + }) |
| 129 | + } |
| 130 | + |
| 131 | + fn debug_canonical_var_kinds( |
| 132 | + canonical_var_kinds: &CanonicalVarKinds<Self>, |
| 133 | + fmt: &mut fmt::Formatter<'_>, |
| 134 | + ) -> Option<fmt::Result> { |
| 135 | + tls::with_current_program(|prog| { |
| 136 | + Some(prog?.debug_canonical_var_kinds(canonical_var_kinds, fmt)) |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + fn debug_goal(goal: &Goal<ChalkIr>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { |
| 141 | + tls::with_current_program(|prog| Some(prog?.debug_goal(goal, fmt))) |
| 142 | + } |
| 143 | + |
| 144 | + fn debug_goals(goals: &Goals<ChalkIr>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { |
| 145 | + tls::with_current_program(|prog| Some(prog?.debug_goals(goals, fmt))) |
| 146 | + } |
| 147 | + |
| 148 | + fn debug_program_clause_implication( |
| 149 | + pci: &ProgramClauseImplication<ChalkIr>, |
| 150 | + fmt: &mut fmt::Formatter<'_>, |
| 151 | + ) -> Option<fmt::Result> { |
| 152 | + tls::with_current_program(|prog| Some(prog?.debug_program_clause_implication(pci, fmt))) |
| 153 | + } |
| 154 | + |
| 155 | + fn debug_program_clause( |
| 156 | + clause: &ProgramClause<ChalkIr>, |
| 157 | + fmt: &mut fmt::Formatter<'_>, |
| 158 | + ) -> Option<fmt::Result> { |
| 159 | + tls::with_current_program(|prog| Some(prog?.debug_program_clause(clause, fmt))) |
| 160 | + } |
| 161 | + |
| 162 | + fn debug_program_clauses( |
| 163 | + clause: &ProgramClauses<ChalkIr>, |
| 164 | + fmt: &mut fmt::Formatter<'_>, |
| 165 | + ) -> Option<fmt::Result> { |
| 166 | + tls::with_current_program(|prog| Some(prog?.debug_program_clauses(clause, fmt))) |
| 167 | + } |
| 168 | + |
| 169 | + fn debug_application_ty( |
| 170 | + application_ty: &ApplicationTy<ChalkIr>, |
| 171 | + fmt: &mut fmt::Formatter<'_>, |
| 172 | + ) -> Option<fmt::Result> { |
| 173 | + tls::with_current_program(|prog| Some(prog?.debug_application_ty(application_ty, fmt))) |
| 174 | + } |
| 175 | + |
| 176 | + fn debug_substitution( |
| 177 | + substitution: &Substitution<ChalkIr>, |
| 178 | + fmt: &mut fmt::Formatter<'_>, |
| 179 | + ) -> Option<fmt::Result> { |
| 180 | + tls::with_current_program(|prog| Some(prog?.debug_substitution(substitution, fmt))) |
| 181 | + } |
| 182 | + |
| 183 | + fn debug_separator_trait_ref( |
| 184 | + separator_trait_ref: &SeparatorTraitRef<'_, ChalkIr>, |
| 185 | + fmt: &mut fmt::Formatter<'_>, |
| 186 | + ) -> Option<fmt::Result> { |
| 187 | + tls::with_current_program(|prog| { |
| 188 | + Some(prog?.debug_separator_trait_ref(separator_trait_ref, fmt)) |
| 189 | + }) |
| 190 | + } |
| 191 | + |
| 192 | + fn debug_quantified_where_clauses( |
| 193 | + clauses: &QuantifiedWhereClauses<Self>, |
| 194 | + fmt: &mut fmt::Formatter<'_>, |
| 195 | + ) -> Option<fmt::Result> { |
| 196 | + tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt))) |
| 197 | + } |
| 198 | + |
| 199 | + fn intern_ty(&self, ty: TyData<ChalkIr>) -> Arc<TyData<ChalkIr>> { |
| 200 | + Arc::new(ty) |
| 201 | + } |
| 202 | + |
| 203 | + fn ty_data<'a>(&self, ty: &'a Arc<TyData<ChalkIr>>) -> &'a TyData<Self> { |
| 204 | + ty |
| 205 | + } |
| 206 | + |
| 207 | + fn intern_lifetime(&self, lifetime: LifetimeData<ChalkIr>) -> LifetimeData<ChalkIr> { |
| 208 | + lifetime |
| 209 | + } |
| 210 | + |
| 211 | + fn lifetime_data<'a>(&self, lifetime: &'a LifetimeData<ChalkIr>) -> &'a LifetimeData<ChalkIr> { |
| 212 | + lifetime |
| 213 | + } |
| 214 | + |
| 215 | + fn intern_parameter(&self, parameter: ParameterData<ChalkIr>) -> ParameterData<ChalkIr> { |
| 216 | + parameter |
| 217 | + } |
| 218 | + |
| 219 | + fn parameter_data<'a>( |
| 220 | + &self, |
| 221 | + parameter: &'a ParameterData<ChalkIr>, |
| 222 | + ) -> &'a ParameterData<ChalkIr> { |
| 223 | + parameter |
| 224 | + } |
| 225 | + |
| 226 | + fn intern_goal(&self, goal: GoalData<ChalkIr>) -> Arc<GoalData<ChalkIr>> { |
| 227 | + Arc::new(goal) |
| 228 | + } |
| 229 | + |
| 230 | + fn goal_data<'a>(&self, goal: &'a Arc<GoalData<ChalkIr>>) -> &'a GoalData<ChalkIr> { |
| 231 | + goal |
| 232 | + } |
| 233 | + |
| 234 | + fn intern_goals<E>( |
| 235 | + &self, |
| 236 | + data: impl IntoIterator<Item = Result<Goal<ChalkIr>, E>>, |
| 237 | + ) -> Result<Vec<Goal<ChalkIr>>, E> { |
| 238 | + data.into_iter().collect() |
| 239 | + } |
| 240 | + |
| 241 | + fn goals_data<'a>(&self, goals: &'a Vec<Goal<ChalkIr>>) -> &'a [Goal<ChalkIr>] { |
| 242 | + goals |
| 243 | + } |
| 244 | + |
| 245 | + fn intern_substitution<E>( |
| 246 | + &self, |
| 247 | + data: impl IntoIterator<Item = Result<Parameter<ChalkIr>, E>>, |
| 248 | + ) -> Result<Vec<Parameter<ChalkIr>>, E> { |
| 249 | + data.into_iter().collect() |
| 250 | + } |
| 251 | + |
| 252 | + fn substitution_data<'a>( |
| 253 | + &self, |
| 254 | + substitution: &'a Vec<Parameter<ChalkIr>>, |
| 255 | + ) -> &'a [Parameter<ChalkIr>] { |
| 256 | + substitution |
| 257 | + } |
| 258 | + |
| 259 | + fn intern_program_clause(&self, data: ProgramClauseData<Self>) -> ProgramClauseData<Self> { |
| 260 | + data |
| 261 | + } |
| 262 | + |
| 263 | + fn program_clause_data<'a>( |
| 264 | + &self, |
| 265 | + clause: &'a ProgramClauseData<Self>, |
| 266 | + ) -> &'a ProgramClauseData<Self> { |
| 267 | + clause |
| 268 | + } |
| 269 | + |
| 270 | + fn intern_program_clauses<E>( |
| 271 | + &self, |
| 272 | + data: impl IntoIterator<Item = Result<ProgramClause<Self>, E>>, |
| 273 | + ) -> Result<Vec<ProgramClause<Self>>, E> { |
| 274 | + data.into_iter().collect() |
| 275 | + } |
| 276 | + |
| 277 | + fn program_clauses_data<'a>( |
| 278 | + &self, |
| 279 | + clauses: &'a Vec<ProgramClause<Self>>, |
| 280 | + ) -> &'a [ProgramClause<Self>] { |
| 281 | + clauses |
| 282 | + } |
| 283 | + |
| 284 | + fn intern_quantified_where_clauses<E>( |
| 285 | + &self, |
| 286 | + data: impl IntoIterator<Item = Result<QuantifiedWhereClause<Self>, E>>, |
| 287 | + ) -> Result<Self::InternedQuantifiedWhereClauses, E> { |
| 288 | + data.into_iter().collect() |
| 289 | + } |
| 290 | + |
| 291 | + fn quantified_where_clauses_data<'a>( |
| 292 | + &self, |
| 293 | + clauses: &'a Self::InternedQuantifiedWhereClauses, |
| 294 | + ) -> &'a [QuantifiedWhereClause<Self>] { |
| 295 | + clauses |
| 296 | + } |
| 297 | + fn intern_parameter_kinds<E>( |
| 298 | + &self, |
| 299 | + data: impl IntoIterator<Item = Result<ParameterKind<()>, E>>, |
| 300 | + ) -> Result<Self::InternedParameterKinds, E> { |
| 301 | + data.into_iter().collect() |
| 302 | + } |
| 303 | + |
| 304 | + fn parameter_kinds_data<'a>( |
| 305 | + &self, |
| 306 | + parameter_kinds: &'a Self::InternedParameterKinds, |
| 307 | + ) -> &'a [ParameterKind<()>] { |
| 308 | + parameter_kinds |
| 309 | + } |
| 310 | + |
| 311 | + fn intern_canonical_var_kinds<E>( |
| 312 | + &self, |
| 313 | + data: impl IntoIterator<Item = Result<ParameterKind<UniverseIndex>, E>>, |
| 314 | + ) -> Result<Self::InternedCanonicalVarKinds, E> { |
| 315 | + data.into_iter().collect() |
| 316 | + } |
| 317 | + |
| 318 | + fn canonical_var_kinds_data<'a>( |
| 319 | + &self, |
| 320 | + canonical_var_kinds: &'a Self::InternedCanonicalVarKinds, |
| 321 | + ) -> &'a [ParameterKind<UniverseIndex>] { |
| 322 | + canonical_var_kinds |
| 323 | + } |
| 324 | +} |
| 325 | + |
| 326 | +impl HasInterner for ChalkIr { |
| 327 | + type Interner = ChalkIr; |
| 328 | +} |
0 commit comments