<commit_before>//===--- TypeCheckType.cpp - Type Validation ------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements validation for Swift types, emitting semantic errors as
// appropriate and checking default initializer values.
//
//===----------------------------------------------------------------------===//

#include "TypeChecker.h"
#include "swift/AST/ASTWalker.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
using namespace swift;

/// Given an array slice type with a valid base type and no
/// implementation type, find its canonicalization.
static bool buildArraySliceType(TypeChecker &TC, ArraySliceType *sliceTy) {
  Type baseTy = sliceTy->getBaseType();

  SourceLoc loc = sliceTy->getFirstRefLoc();

  // As our current hack for array slices, require the base type to be
  // a nominal type X, then look for a type SliceX.
  Identifier name;
  if (OneOfType *oneof = baseTy->getAs<OneOfType>()) {
    name = oneof->getDecl()->getName();
  } else if (StructType *st = baseTy->getAs<StructType>()) {
    name = st->getDecl()->getName();
  } else {
    TC.diagnose(loc, diag::base_of_array_slice_not_nominal, baseTy);
    return true;
  }

  llvm::SmallString<32> nameBuffer("Slice");
  nameBuffer.append(name.str());

  SmallVector<ValueDecl*, 8> Decls;
  TC.TU.lookupGlobalValue(TC.Context.getIdentifier(nameBuffer),
                          NLKind::UnqualifiedLookup, Decls);
  if (Decls.size() != 1 || !isa<TypeDecl>(Decls.back())) {
    TC.diagnose(loc, diag::slice_type_not_found, nameBuffer);
    return true;
  }

  // FIXME: there's no reason to think that the alias type will have
  // been validated in general.
  TypeDecl *TD = cast<TypeDecl>(Decls.back());
  sliceTy->setImplementationType(TD->getDeclaredType());
  return false;
}

Type TypeChecker::getArraySliceType(SourceLoc loc, Type elementType) {
  ArraySliceType *sliceTy = ArraySliceType::get(elementType, loc, Context);
  if (sliceTy->hasCanonicalTypeComputed()) return sliceTy;
  if (buildArraySliceType(*this, sliceTy)) return Type();
  sliceTy->getCanonicalType();
  return sliceTy;
}
<commit_msg>Extend the list of types we can use to lookup T[] -> SliceT.<commit_after>//===--- TypeCheckType.cpp - Type Validation ------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements validation for Swift types, emitting semantic errors as
// appropriate and checking default initializer values.
//
//===----------------------------------------------------------------------===//

#include "TypeChecker.h"
#include "swift/AST/ASTWalker.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
using namespace swift;

/// Given an array slice type with a valid base type and no
/// implementation type, find its canonicalization.
static bool buildArraySliceType(TypeChecker &TC, ArraySliceType *sliceTy) {
  Type baseTy = sliceTy->getBaseType();

  SourceLoc loc = sliceTy->getFirstRefLoc();

  // As our current hack for array slices, require the base type to be
  // a nominal type X, then look for a type SliceX.
  Identifier name;
  if (OneOfType *oneof = baseTy->getAs<OneOfType>()) {
    name = oneof->getDecl()->getName();
  } else if (StructType *st = baseTy->getAs<StructType>()) {
    name = st->getDecl()->getName();
  } else if (ProtocolType *pt = baseTy->getAs<ProtocolType>()) {
    name = pt->getDecl()->getName();
  } else if (ClassType *ct = baseTy->getAs<ClassType>()) {
    name = ct->getDecl()->getName();
  } else {
    TC.diagnose(loc, diag::base_of_array_slice_not_nominal, baseTy);
    return true;
  }

  llvm::SmallString<32> nameBuffer("Slice");
  nameBuffer.append(name.str());

  SmallVector<ValueDecl*, 8> Decls;
  TC.TU.lookupGlobalValue(TC.Context.getIdentifier(nameBuffer),
                          NLKind::UnqualifiedLookup, Decls);
  if (Decls.size() != 1 || !isa<TypeDecl>(Decls.back())) {
    TC.diagnose(loc, diag::slice_type_not_found, nameBuffer);
    return true;
  }

  // FIXME: there's no reason to think that the alias type will have
  // been validated in general.
  TypeDecl *TD = cast<TypeDecl>(Decls.back());
  sliceTy->setImplementationType(TD->getDeclaredType());
  return false;
}

Type TypeChecker::getArraySliceType(SourceLoc loc, Type elementType) {
  ArraySliceType *sliceTy = ArraySliceType::get(elementType, loc, Context);
  if (sliceTy->hasCanonicalTypeComputed()) return sliceTy;
  if (buildArraySliceType(*this, sliceTy)) return Type();
  sliceTy->getCanonicalType();
  return sliceTy;
}

{'fraction_non_alphanumeric': 0.19430391605771033, 'fraction_numerical': 0.005246393104740491, 'mean_word_length': 2.9746835443037973, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '893251', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1520, 'n_words': 449}