1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
//===------------------------ ConstantnessSemaDiagnostics.cpp -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements checks for checking whether certain arguments to some
// specific APIs are compile-time constants (see below for the definition of
// constants). In particular, this code checks whether the new os_log APIs are
// invoked with constant arguments, and whether the primitive atomic operations
// are invoked with constant "orderings". These APIs are identified through
// @_semantics attributes.
//
// A "compile-time constant" is either a literal (including
// string/integer/float/boolean/string-interpolation literal) or a call to a
// "constant_evaluable" function (or property) with compile-time constant
// arguments. A closure expression is also considered a compile-time constant
// (it is a constant of a function type).
//===----------------------------------------------------------------------===//
#include "MiscDiagnostics.h"
#include "TypeChecker.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/SemanticAttrs.h"
using namespace swift;
/// Check whether a given \p decl has a @_semantics attribute with the given
/// attribute name \c attrName.
static bool hasSemanticsAttr(ValueDecl *decl, StringRef attrName) {
return decl->getAttrs().hasSemanticsAttr(attrName);
}
/// Return true iff the given \p structDecl has a name that matches one of the
/// known atomic orderings structs.
static bool isAtomicOrderingDecl(StructDecl *structDecl) {
ASTContext &astContext = structDecl->getASTContext();
Identifier structName = structDecl->getName();
return (structName == astContext.Id_AtomicLoadOrdering ||
structName == astContext.Id_AtomicStoreOrdering ||
structName == astContext.Id_AtomicUpdateOrdering);
}
/// Return true iff the given nominal type decl \p nominal has a name that
/// matches one of the known OSLog types that need not be a constant in the new
/// os_log APIs.
static bool isOSLogDynamicObject(NominalTypeDecl *nominal) {
ASTContext &astContext = nominal->getASTContext();
Identifier name = nominal->getName();
return (name == astContext.Id_OSLog || name == astContext.Id_OSLogType);
}
/// Return true iff the parameter \p param of function \c funDecl is required to
/// be a constant. This is true if either the function is an os_log function or
/// it is an atomics operation and the parameter represents the ordering.
static bool isParamRequiredToBeConstant(AbstractFunctionDecl *funcDecl, ParamDecl *param) {
assert(funcDecl && param && "funcDecl and param must not be null");
Type paramType;
NominalTypeDecl *nominal;
StructDecl *structDecl;
if (hasSemanticsAttr(funcDecl, semantics::OSLOG_REQUIRES_CONSTANT_ARGUMENTS))
return true;
if (hasSemanticsAttr(funcDecl, semantics::OSLOG_LOG_WITH_LEVEL)) {
// We are looking at a top-level os_log function that accepts level and
// possibly custom log object. Those need not be constants, but every other
// parameter must be.
paramType = param->getTypeInContext();
nominal = paramType->getNominalOrBoundGenericNominal();
return !nominal || !isOSLogDynamicObject(nominal);
}
if (!hasSemanticsAttr(funcDecl,
semantics::ATOMICS_REQUIRES_CONSTANT_ORDERINGS))
return false;
paramType = param->getTypeInContext();
structDecl = paramType->getStructOrBoundGenericStruct();
if (!structDecl)
return false;
return isAtomicOrderingDecl(structDecl);
}
/// Return true iff the \c decl is annotated as
/// @_semantics("constant_evaluable").
static bool hasConstantEvaluableAttr(ValueDecl *decl) {
return hasSemanticsAttr(decl, semantics::CONSTANT_EVALUABLE);
}
/// Return true iff the \p decl is annotated with oslog.message.init semantics
/// attribute.
static bool isOSLogMessageInitializer(ValueDecl *decl) {
return hasSemanticsAttr(decl, semantics::OSLOG_MESSAGE_INIT_STRING_LITERAL) ||
hasSemanticsAttr(decl, semantics::OSLOG_MESSAGE_INIT_INTERPOLATION);
}
/// Check whether \p expr is a compile-time constant. It must either be a
/// literal_expr, which does not include array and dictionary literal, or a
/// closure expression, which is considered a compile-time constant of a
/// function type, or a call to a "constant_evaluable" function (or property)
/// whose arguments are themselves compile-time constants.
static Expr *checkConstantness(Expr *expr) {
SmallVector<Expr *, 4> expressionsToCheck;
expressionsToCheck.push_back(expr);
while (!expressionsToCheck.empty()) {
Expr *expr = expressionsToCheck.pop_back_val();
// Lookthrough identity_expr, tuple, binary_expr and inject_into_optional expressions.
if (IdentityExpr *identityExpr = dyn_cast<IdentityExpr>(expr)) {
expressionsToCheck.push_back(identityExpr->getSubExpr());
continue;
}
if (TupleExpr *tupleExpr = dyn_cast<TupleExpr>(expr)) {
for (Expr *element : tupleExpr->getElements())
expressionsToCheck.push_back(element);
continue;
}
if (BinaryExpr *binaryExpr = dyn_cast<BinaryExpr>(expr)) {
expressionsToCheck.push_back(binaryExpr->getLHS());
expressionsToCheck.push_back(binaryExpr->getRHS());
continue;
}
if (InjectIntoOptionalExpr *optionalExpr =
dyn_cast<InjectIntoOptionalExpr>(expr)) {
expressionsToCheck.push_back(optionalExpr->getSubExpr());
continue;
}
// Literal expressions also includes InterpolatedStringLiteralExpr.
if (isa<LiteralExpr>(expr))
continue;
if (isa<TypeExpr>(expr))
continue;
// Closure expressions are always treated as constants. They are
// constants of function types.
if (isa<AbstractClosureExpr>(expr))
continue;
// Default argument expressions of a constant_evaluable or a
// requires_constant function must be ensured to be a constant by the
// definition of the function.
if (isa<DefaultArgumentExpr>(expr))
continue;
// If this is a member-ref, it has to be annotated constant evaluable.
if (MemberRefExpr *memberRef = dyn_cast<MemberRefExpr>(expr)) {
if (ValueDecl *memberDecl = memberRef->getMember().getDecl()) {
if (hasConstantEvaluableAttr(memberDecl))
continue;
}
return expr;
}
// If this is a variable, it has to be a known constant parameter of the
// enclosing function.
if (DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(expr)) {
ValueDecl *decl = declRef->getDecl();
if (!decl)
return expr;
ParamDecl *paramDecl = dyn_cast<ParamDecl>(decl);
if (!paramDecl)
return expr;
Decl *declContext = paramDecl->getDeclContext()->getAsDecl();
if (!declContext)
return expr;
AbstractFunctionDecl *funcDecl = dyn_cast<AbstractFunctionDecl>(declContext);
if (!funcDecl || !isParamRequiredToBeConstant(funcDecl, paramDecl))
return expr;
continue;
}
if (!isa<ApplyExpr>(expr))
return expr;
ApplyExpr *apply = cast<ApplyExpr>(expr);
ValueDecl *calledValue = apply->getCalledValue();
if (!calledValue)
return expr;
// If this is an enum case, check whether the arguments are constants.
if (isa<EnumElementDecl>(calledValue)) {
for (auto arg : *apply->getArgs())
expressionsToCheck.push_back(arg.getExpr());
continue;
}
AbstractFunctionDecl *callee = dyn_cast<AbstractFunctionDecl>(calledValue);
if (!callee)
return expr;
// If this is an application of OSLogMessage initializer, fail the check
// as this type must be created from string interpolations.
if (isOSLogMessageInitializer(callee))
return expr;
// If this is a constant_evaluable function, check whether the arguments are
// constants.
if (!hasConstantEvaluableAttr(callee))
return expr;
for (auto arg : *apply->getArgs())
expressionsToCheck.push_back(arg.getExpr());
}
return nullptr;
}
/// Return true iff the given \p type is a Stdlib integer type.
static bool isIntegerType(Type type) {
return type->isInt() || type->isInt8() || type->isInt16() ||
type->isInt32() || type->isInt64() || type->isUInt() ||
type->isUInt8() || type->isUInt16() || type->isUInt32() ||
type->isUInt64();
}
/// Return true iff the given \p type is a Float type.
static bool isFloatType(Type type) {
return type->isFloat() || type->isDouble() || type->isFloat80();
}
/// Given an error expression \p errorExpr, diagnose the error based on the type
/// of the expression. For instance, if the expression's type is expressible by
/// a literal e.g. integer, boolean etc. report that it must be a literal.
/// Otherwise, if the expression is a nominal type, report that it must be
/// static member of the type.
static void diagnoseError(Expr *errorExpr, const ASTContext &astContext,
AbstractFunctionDecl *funcDecl) {
DiagnosticEngine &diags = astContext.Diags;
Type exprType = errorExpr->getType();
SourceLoc errorLoc = errorExpr->getLoc();
// Diagnose atomics ordering related error here.
if (hasSemanticsAttr(funcDecl,
semantics::ATOMICS_REQUIRES_CONSTANT_ORDERINGS)) {
NominalTypeDecl *nominalDecl = exprType->getNominalOrBoundGenericNominal();
if (!nominalDecl) {
// This case should normally not happen. This is a safe guard against
// possible mismatch between the atomics library and the compiler.
diags.diagnose(errorLoc, diag::argument_must_be_constant);
}
diags.diagnose(errorLoc, diag::atomics_ordering_must_be_constant,
nominalDecl->getName());
return;
}
// Diagnose os_log specific errors here.
// Diagnose primitive stdlib types.
if (exprType->isBool()) {
diags.diagnose(errorLoc, diag::oslog_arg_must_be_bool_literal);
return;
}
if (exprType->isString()) {
diags.diagnose(errorLoc, diag::oslog_arg_must_be_string_literal);
return;
}
if (isIntegerType(exprType)) {
diags.diagnose(errorLoc, diag::oslog_arg_must_be_integer_literal);
return;
}
if (isFloatType(exprType)) {
diags.diagnose(errorLoc, diag::oslog_arg_must_be_float_literal);
return;
}
if (exprType->is<MetatypeType>()) {
diags.diagnose(errorLoc, diag::oslog_arg_must_be_metatype_literal);
return;
}
if (exprType->is<AnyFunctionType>()) {
diags.diagnose(errorLoc, diag::oslog_arg_must_be_closure);
return;
}
if (EnumDecl *enumDecl = exprType->getEnumOrBoundGenericEnum()) {
diags.diagnose(errorLoc, diag::oslog_arg_must_be_enum_case,
enumDecl->getName());
return;
}
NominalTypeDecl *nominalDecl = exprType->getNominalOrBoundGenericNominal();
if (!nominalDecl) {
// This case should normally not happen. This is a safe guard against
// possible mismatch between the os overlay and the compiler.
diags.diagnose(errorLoc, diag::argument_must_be_constant);
return;
}
// If this is OSLogMessage, it should be a string-interpolation literal.
Identifier declName = nominalDecl->getName();
if (declName == astContext.Id_OSLogMessage ||
nominalDecl->hasSemanticsAttr(semantics::OSLOG_MESSAGE_TYPE)) {
diags.diagnose(errorLoc, diag::oslog_message_must_be_string_interpolation);
return;
}
diags.diagnose(errorLoc, diag::oslog_arg_must_be_type_member_access,
declName);
}
/// Given a call \c callExpr, if some or all of its arguments are required to be
/// constants, check that property on the arguments.
static void diagnoseConstantArgumentRequirementOfCall(const CallExpr *callExpr,
const ASTContext &ctx) {
assert(callExpr && callExpr->getType() &&
"callExpr should have a valid type");
ValueDecl *calledDecl = callExpr->getCalledValue();
if (!calledDecl || !isa<AbstractFunctionDecl>(calledDecl))
return;
AbstractFunctionDecl *callee = cast<AbstractFunctionDecl>(calledDecl);
// Collect argument indices that are required to be constants.
SmallVector<unsigned, 4> constantArgumentIndices;
auto paramList = callee->getParameters();
for (unsigned i = 0; i < paramList->size(); ++i) {
ParamDecl *param = paramList->get(i);
if (isParamRequiredToBeConstant(callee, param))
constantArgumentIndices.push_back(i);
}
if (constantArgumentIndices.empty())
return;
// Check that the arguments at the constantArgumentIndices are constants.
SmallVector<Expr *, 4> arguments;
for (auto arg : *callExpr->getArgs())
arguments.push_back(arg.getExpr());
for (unsigned constantIndex : constantArgumentIndices) {
assert(constantIndex < arguments.size() &&
"constantIndex exceeds the number of arguments to the function");
Expr *argument = arguments[constantIndex];
Expr *errorExpr = checkConstantness(argument);
if (errorExpr)
diagnoseError(errorExpr, ctx, callee);
}
}
void swift::diagnoseConstantArgumentRequirement(
const Expr *expr, const DeclContext *declContext) {
class ConstantReqCallWalker : public ASTWalker {
DeclContext *DC;
bool insideClosure;
public:
ConstantReqCallWalker(DeclContext *DC) : DC(DC), insideClosure(false) {}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::ArgumentsAndExpansion;
}
// Descend until we find a call expressions. Note that the input expression
// could be an assign expression or another expression that contains the
// call.
PreWalkResult<Expr *> walkToExprPre(Expr *expr) override {
// Handle closure expressions separately as we may need to
// manually descend into the body.
if (auto *closureExpr = dyn_cast<ClosureExpr>(expr)) {
return walkToClosureExprPre(closureExpr);
}
if (!expr || isa<ErrorExpr>(expr) || !expr->getType())
return Action::SkipNode(expr);
if (auto *callExpr = dyn_cast<CallExpr>(expr)) {
diagnoseConstantArgumentRequirementOfCall(callExpr, DC->getASTContext());
}
return Action::Continue(expr);
}
PreWalkResult<Expr *> walkToClosureExprPre(ClosureExpr *closure) {
DC = closure;
insideClosure = true;
return Action::Continue(closure);
}
PostWalkResult<Expr *> walkToExprPost(Expr *expr) override {
if (auto *closureExpr = dyn_cast<ClosureExpr>(expr)) {
// Reset the DeclContext to the outer scope if we descended
// into a closure expr and check whether or not we are still
// within a closure context.
DC = closureExpr->getParent();
insideClosure = isa<ClosureExpr>(DC);
}
return Action::Continue(expr);
}
};
// We manually check closure bodies from their outer contexts,
// so bail early if we are being called directly on expressions
// inside of a closure body.
if (isa<ClosureExpr>(declContext)) {
return;
}
ConstantReqCallWalker walker(const_cast<DeclContext *>(declContext));
const_cast<Expr *>(expr)->walk(walker);
}
|