Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
| 19 | static const FunctionTypeProto *getFunctionProto(Decl *d) { |
| 20 | QualType Ty; |
| 21 | |
| 22 | if (ValueDecl *decl = dyn_cast<ValueDecl>(d)) |
| 23 | Ty = decl->getType(); |
| 24 | else if (FieldDecl *decl = dyn_cast<FieldDecl>(d)) |
| 25 | Ty = decl->getType(); |
| 26 | else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) |
| 27 | Ty = decl->getUnderlyingType(); |
| 28 | else |
| 29 | return 0; |
| 30 | |
| 31 | if (Ty->isFunctionPointerType()) |
| 32 | Ty = Ty->getAsPointerType()->getPointeeType(); |
| 33 | |
| 34 | if (const FunctionType *FnTy = Ty->getAsFunctionType()) |
| 35 | return dyn_cast<FunctionTypeProto>(FnTy->getAsFunctionType()); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
| 41 | if (!T->isPointerType()) |
| 42 | return false; |
| 43 | |
| 44 | T = T->getAsPointerType()->getPointeeType().getCanonicalType(); |
| 45 | ObjCInterfaceType* ClsT = dyn_cast<ObjCInterfaceType>(T.getTypePtr()); |
| 46 | |
| 47 | if (!ClsT) |
| 48 | return false; |
| 49 | |
| 50 | IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier(); |
| 51 | |
| 52 | // FIXME: Should we walk the chain of classes? |
| 53 | return ClsName == &Ctx.Idents.get("NSString") || |
| 54 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 55 | } |
| 56 | |
Chris Lattner | 9b384ca | 2008-06-29 00:02:00 +0000 | [diff] [blame^] | 57 | void Sema::ProcessDeclAttributes(Decl *D, Declarator &PD) { |
| 58 | const AttributeList *DeclSpecAttrs = PD.getDeclSpec().getAttributes(); |
| 59 | const AttributeList *DeclaratorAttrs = PD.getAttributes(); |
| 60 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 61 | if (DeclSpecAttrs == 0 && DeclaratorAttrs == 0) return; |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 62 | |
| 63 | ProcessDeclAttributeList(D, DeclSpecAttrs); |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 64 | |
| 65 | // If there are any type attributes that were in the declarator, apply them to |
| 66 | // its top level type. |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 67 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 68 | QualType DT = VD->getType(); |
| 69 | ProcessTypeAttributes(DT, DeclaratorAttrs); |
| 70 | VD->setType(DT); |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 71 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 72 | QualType DT = TD->getUnderlyingType(); |
| 73 | ProcessTypeAttributes(DT, DeclaratorAttrs); |
| 74 | TD->setUnderlyingType(DT); |
| 75 | } |
| 76 | |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 77 | ProcessDeclAttributeList(D, DeclaratorAttrs); |
| 78 | } |
| 79 | |
| 80 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 81 | /// attribute list to the specified decl, ignoring any type attributes. |
| 82 | void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) { |
| 83 | while (AttrList) { |
| 84 | ProcessDeclAttribute(D, *AttrList); |
| 85 | AttrList = AttrList->getNext(); |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | /// HandleDeclAttribute - Apply the specific attribute to the specified decl if |
| 90 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 91 | /// silently ignore it. |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 92 | void Sema::ProcessDeclAttribute(Decl *D, const AttributeList &Attr) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 93 | switch (Attr.getKind()) { |
| 94 | case AttributeList::AT_address_space: |
| 95 | // Ignore this, this is a type attribute, handled by ProcessTypeAttributes. |
| 96 | break; |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 97 | case AttributeList::AT_vector_size: HandleVectorSizeAttribute(D, Attr); break; |
| 98 | case AttributeList::AT_ext_vector_type: |
| 99 | HandleExtVectorTypeAttribute(D, Attr); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 100 | break; |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 101 | case AttributeList::AT_mode: HandleModeAttribute(D, Attr); break; |
| 102 | case AttributeList::AT_alias: HandleAliasAttribute(D, Attr); break; |
| 103 | case AttributeList::AT_deprecated: HandleDeprecatedAttribute(D, Attr);break; |
| 104 | case AttributeList::AT_visibility: HandleVisibilityAttribute(D, Attr);break; |
| 105 | case AttributeList::AT_weak: HandleWeakAttribute(D, Attr); break; |
| 106 | case AttributeList::AT_dllimport: HandleDLLImportAttribute(D, Attr); break; |
| 107 | case AttributeList::AT_dllexport: HandleDLLExportAttribute(D, Attr); break; |
| 108 | case AttributeList::AT_nothrow: HandleNothrowAttribute(D, Attr); break; |
| 109 | case AttributeList::AT_stdcall: HandleStdCallAttribute(D, Attr); break; |
| 110 | case AttributeList::AT_fastcall: HandleFastCallAttribute(D, Attr); break; |
| 111 | case AttributeList::AT_aligned: HandleAlignedAttribute(D, Attr); break; |
| 112 | case AttributeList::AT_packed: HandlePackedAttribute(D, Attr); break; |
| 113 | case AttributeList::AT_annotate: HandleAnnotateAttribute(D, Attr); break; |
| 114 | case AttributeList::AT_noreturn: HandleNoReturnAttribute(D, Attr); break; |
| 115 | case AttributeList::AT_format: HandleFormatAttribute(D, Attr); break; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 116 | case AttributeList::AT_transparent_union: |
Chris Lattner | d7e83d8 | 2008-06-28 23:58:55 +0000 | [diff] [blame] | 117 | HandleTransparentUnionAttribute(D, Attr); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 118 | break; |
| 119 | default: |
| 120 | #if 0 |
| 121 | // TODO: when we have the full set of attributes, warn about unknown ones. |
| 122 | Diag(Attr->getLoc(), diag::warn_attribute_ignored, |
| 123 | Attr->getName()->getName()); |
| 124 | #endif |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 129 | void Sema::HandleExtVectorTypeAttribute(Decl *d, const AttributeList &Attr) { |
| 130 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 131 | if (tDecl == 0) { |
| 132 | Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
| 133 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 136 | QualType curType = tDecl->getUnderlyingType(); |
| 137 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 138 | if (Attr.getNumArgs() != 1) { |
| 139 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 140 | std::string("1")); |
| 141 | return; |
| 142 | } |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 143 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 144 | llvm::APSInt vecSize(32); |
| 145 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 146 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 147 | "ext_vector_type", sizeExpr->getSourceRange()); |
| 148 | return; |
| 149 | } |
| 150 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 151 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
| 152 | Type *canonType = curType.getCanonicalType().getTypePtr(); |
| 153 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 154 | Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 155 | curType.getCanonicalType().getAsString()); |
| 156 | return; |
| 157 | } |
| 158 | // unlike gcc's vector_size attribute, the size is specified as the |
| 159 | // number of elements, not the number of bytes. |
| 160 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 161 | |
| 162 | if (vectorSize == 0) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 163 | Diag(Attr.getLoc(), diag::err_attribute_zero_size, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 164 | sizeExpr->getSourceRange()); |
| 165 | return; |
| 166 | } |
| 167 | // Instantiate/Install the vector type, the number of elements is > 0. |
| 168 | tDecl->setUnderlyingType(Context.getExtVectorType(curType, vectorSize)); |
| 169 | // Remember this typedef decl, we will need it later for diagnostics. |
| 170 | ExtVectorDecls.push_back(tDecl); |
| 171 | } |
| 172 | |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 173 | |
| 174 | /// HandleVectorSizeAttribute - this attribute is only applicable to |
| 175 | /// integral and float scalars, although arrays, pointers, and function |
| 176 | /// return values are allowed in conjunction with this construct. Aggregates |
| 177 | /// with this attribute are invalid, even if they are of the same size as a |
| 178 | /// corresponding scalar. |
| 179 | /// The raw attribute should contain precisely 1 argument, the vector size |
| 180 | /// for the variable, measured in bytes. If curType and rawAttr are well |
| 181 | /// formed, this routine will return a new vector type. |
| 182 | void Sema::HandleVectorSizeAttribute(Decl *D, const AttributeList &Attr) { |
| 183 | QualType CurType; |
| 184 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 185 | CurType = VD->getType(); |
| 186 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 187 | CurType = TD->getUnderlyingType(); |
| 188 | else { |
| 189 | Diag(D->getLocation(), diag::err_attr_wrong_decl,std::string("vector_size"), |
| 190 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // Check the attribute arugments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 195 | if (Attr.getNumArgs() != 1) { |
| 196 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 197 | std::string("1")); |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 198 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 200 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 201 | llvm::APSInt vecSize(32); |
| 202 | if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 203 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 204 | "vector_size", sizeExpr->getSourceRange()); |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 205 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 206 | } |
| 207 | // navigate to the base type - we need to provide for vector pointers, |
| 208 | // vector arrays, and functions returning vectors. |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 209 | Type *canonType = CurType.getCanonicalType().getTypePtr(); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 210 | |
| 211 | if (canonType->isPointerType() || canonType->isArrayType() || |
| 212 | canonType->isFunctionType()) { |
| 213 | assert(0 && "HandleVector(): Complex type construction unimplemented"); |
| 214 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 215 | do { |
| 216 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 217 | canonType = PT->getPointeeType().getTypePtr(); |
| 218 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 219 | canonType = AT->getElementType().getTypePtr(); |
| 220 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 221 | canonType = FT->getResultType().getTypePtr(); |
| 222 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 223 | canonType->isFunctionType()); |
| 224 | */ |
| 225 | } |
| 226 | // the base type must be integer or float. |
| 227 | if (!(canonType->isIntegerType() || canonType->isRealFloatingType())) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 228 | Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type, |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 229 | CurType.getCanonicalType().getAsString()); |
| 230 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 231 | } |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 232 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(CurType)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 233 | // vecSize is specified in bytes - convert to bits. |
| 234 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 235 | |
| 236 | // the vector size needs to be an integral multiple of the type size. |
| 237 | if (vectorSize % typeSize) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 238 | Diag(Attr.getLoc(), diag::err_attribute_invalid_size, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 239 | sizeExpr->getSourceRange()); |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 240 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 241 | } |
| 242 | if (vectorSize == 0) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 243 | Diag(Attr.getLoc(), diag::err_attribute_zero_size, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 244 | sizeExpr->getSourceRange()); |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 245 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 246 | } |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 247 | |
| 248 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 249 | // not required to be a power of 2, unlike GCC. |
| 250 | CurType = Context.getVectorType(CurType, vectorSize/typeSize); |
| 251 | |
| 252 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 253 | VD->setType(CurType); |
| 254 | else |
| 255 | cast<TypedefDecl>(D)->setUnderlyingType(CurType); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 258 | void Sema::HandlePackedAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 259 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 260 | if (Attr.getNumArgs() > 0) { |
| 261 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 262 | std::string("0")); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 267 | TD->addAttr(new PackedAttr()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 268 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 269 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 270 | // has no effect. |
| 271 | if (!FD->getType()->isIncompleteType() && |
| 272 | Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 273 | Diag(Attr.getLoc(), |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 274 | diag::warn_attribute_ignored_for_field_of_type, |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 275 | Attr.getName()->getName(), FD->getType().getAsString()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 276 | else |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 277 | FD->addAttr(new PackedAttr()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 278 | } else |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 279 | Diag(Attr.getLoc(), diag::warn_attribute_ignored, |
| 280 | Attr.getName()->getName()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 283 | void Sema::HandleAliasAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 284 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 285 | if (Attr.getNumArgs() != 1) { |
| 286 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 287 | std::string("1")); |
| 288 | return; |
| 289 | } |
| 290 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 291 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 292 | Arg = Arg->IgnoreParenCasts(); |
| 293 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 294 | |
| 295 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 296 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 297 | "alias", std::string("1")); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | const char *Alias = Str->getStrData(); |
| 302 | unsigned AliasLen = Str->getByteLength(); |
| 303 | |
| 304 | // FIXME: check if target symbol exists in current file |
| 305 | |
| 306 | d->addAttr(new AliasAttr(std::string(Alias, AliasLen))); |
| 307 | } |
| 308 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 309 | void Sema::HandleNoReturnAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 310 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 311 | if (Attr.getNumArgs() != 0) { |
| 312 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 313 | std::string("0")); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 318 | if (!Fn) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 319 | Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 320 | "noreturn", "function"); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | d->addAttr(new NoReturnAttr()); |
| 325 | } |
| 326 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 327 | void Sema::HandleDeprecatedAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 328 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 329 | if (Attr.getNumArgs() != 0) { |
| 330 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 331 | std::string("0")); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | d->addAttr(new DeprecatedAttr()); |
| 336 | } |
| 337 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 338 | void Sema::HandleVisibilityAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 339 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 340 | if (Attr.getNumArgs() != 1) { |
| 341 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 342 | std::string("1")); |
| 343 | return; |
| 344 | } |
| 345 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 346 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 347 | Arg = Arg->IgnoreParenCasts(); |
| 348 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 349 | |
| 350 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 351 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 352 | "visibility", std::string("1")); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | const char *TypeStr = Str->getStrData(); |
| 357 | unsigned TypeLen = Str->getByteLength(); |
| 358 | VisibilityAttr::VisibilityTypes type; |
| 359 | |
| 360 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
| 361 | type = VisibilityAttr::DefaultVisibility; |
| 362 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
| 363 | type = VisibilityAttr::HiddenVisibility; |
| 364 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
| 365 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 366 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
| 367 | type = VisibilityAttr::ProtectedVisibility; |
| 368 | else { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 369 | Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 370 | "visibility", TypeStr); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | d->addAttr(new VisibilityAttr(type)); |
| 375 | } |
| 376 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 377 | void Sema::HandleWeakAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 378 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 379 | if (Attr.getNumArgs() != 0) { |
| 380 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 381 | std::string("0")); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | d->addAttr(new WeakAttr()); |
| 386 | } |
| 387 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 388 | void Sema::HandleDLLImportAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 389 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 390 | if (Attr.getNumArgs() != 0) { |
| 391 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 392 | std::string("0")); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | d->addAttr(new DLLImportAttr()); |
| 397 | } |
| 398 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 399 | void Sema::HandleDLLExportAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 400 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 401 | if (Attr.getNumArgs() != 0) { |
| 402 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 403 | std::string("0")); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | d->addAttr(new DLLExportAttr()); |
| 408 | } |
| 409 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 410 | void Sema::HandleStdCallAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 411 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 412 | if (Attr.getNumArgs() != 0) { |
| 413 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 414 | std::string("0")); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | d->addAttr(new StdCallAttr()); |
| 419 | } |
| 420 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 421 | void Sema::HandleFastCallAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 422 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 423 | if (Attr.getNumArgs() != 0) { |
| 424 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 425 | std::string("0")); |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | d->addAttr(new FastCallAttr()); |
| 430 | } |
| 431 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 432 | void Sema::HandleNothrowAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 433 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 434 | if (Attr.getNumArgs() != 0) { |
| 435 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 436 | std::string("0")); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | d->addAttr(new NoThrowAttr()); |
| 441 | } |
| 442 | |
| 443 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 444 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 445 | void Sema::HandleFormatAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 447 | if (!Attr.getParameterName()) { |
| 448 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 449 | "format", std::string("1")); |
| 450 | return; |
| 451 | } |
| 452 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 453 | if (Attr.getNumArgs() != 2) { |
| 454 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 455 | std::string("3")); |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | // GCC ignores the format attribute on K&R style function |
| 460 | // prototypes, so we ignore it as well |
| 461 | const FunctionTypeProto *proto = getFunctionProto(d); |
| 462 | |
| 463 | if (!proto) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 464 | Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 465 | "format", "function"); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
| 469 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
| 470 | // this is needed in order to be compatible with GCC |
| 471 | // the index must start in 1 and the limit is numargs+1 |
| 472 | unsigned NumArgs = proto->getNumArgs(); |
| 473 | unsigned FirstIdx = 1; |
| 474 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 475 | const char *Format = Attr.getParameterName()->getName(); |
| 476 | unsigned FormatLen = Attr.getParameterName()->getLength(); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 477 | |
| 478 | // Normalize the argument, __foo__ becomes foo. |
| 479 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 480 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 481 | Format += 2; |
| 482 | FormatLen -= 4; |
| 483 | } |
| 484 | |
| 485 | bool Supported = false; |
| 486 | bool is_NSString = false; |
| 487 | bool is_strftime = false; |
| 488 | |
| 489 | switch (FormatLen) { |
| 490 | default: break; |
| 491 | case 5: |
| 492 | Supported = !memcmp(Format, "scanf", 5); |
| 493 | break; |
| 494 | case 6: |
| 495 | Supported = !memcmp(Format, "printf", 6); |
| 496 | break; |
| 497 | case 7: |
| 498 | Supported = !memcmp(Format, "strfmon", 7); |
| 499 | break; |
| 500 | case 8: |
| 501 | Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || |
| 502 | (is_NSString = !memcmp(Format, "NSString", 8)); |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | if (!Supported) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 507 | Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported, |
| 508 | "format", Attr.getParameterName()->getName()); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 509 | return; |
| 510 | } |
| 511 | |
| 512 | // checks for the 2nd argument |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 513 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 514 | llvm::APSInt Idx(Context.getTypeSize(IdxExpr->getType())); |
| 515 | if (!IdxExpr->isIntegerConstantExpr(Idx, Context)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 516 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 517 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 522 | Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 523 | "format", std::string("2"), IdxExpr->getSourceRange()); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | // FIXME: Do we need to bounds check? |
| 528 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| 529 | |
| 530 | // make sure the format string is really a string |
| 531 | QualType Ty = proto->getArgType(ArgIdx); |
| 532 | |
| 533 | if (is_NSString) { |
| 534 | // FIXME: do we need to check if the type is NSString*? What are |
| 535 | // the semantics? |
| 536 | if (!isNSStringType(Ty, Context)) { |
| 537 | // FIXME: Should highlight the actual expression that has the |
| 538 | // wrong type. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 539 | Diag(Attr.getLoc(), diag::err_format_attribute_not_NSString, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 540 | IdxExpr->getSourceRange()); |
| 541 | return; |
| 542 | } |
| 543 | } else if (!Ty->isPointerType() || |
| 544 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
| 545 | // FIXME: Should highlight the actual expression that has the |
| 546 | // wrong type. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 547 | Diag(Attr.getLoc(), diag::err_format_attribute_not_string, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 548 | IdxExpr->getSourceRange()); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | // check the 3rd argument |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 553 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 554 | llvm::APSInt FirstArg(Context.getTypeSize(FirstArgExpr->getType())); |
| 555 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, Context)) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 556 | Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 557 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | // check if the function is variadic if the 3rd argument non-zero |
| 562 | if (FirstArg != 0) { |
| 563 | if (proto->isVariadic()) { |
| 564 | ++NumArgs; // +1 for ... |
| 565 | } else { |
| 566 | Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
| 567 | return; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | // strftime requires FirstArg to be 0 because it doesn't read from any variable |
| 572 | // the input is just the current time + the format string |
| 573 | if (is_strftime) { |
| 574 | if (FirstArg != 0) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 575 | Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 576 | FirstArgExpr->getSourceRange()); |
| 577 | return; |
| 578 | } |
| 579 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 580 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 581 | Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 582 | "format", std::string("3"), FirstArgExpr->getSourceRange()); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | d->addAttr(new FormatAttr(std::string(Format, FormatLen), |
| 587 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 588 | } |
| 589 | |
| 590 | void Sema::HandleTransparentUnionAttribute(Decl *d, |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 591 | const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 592 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 593 | if (Attr.getNumArgs() != 0) { |
| 594 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 595 | std::string("0")); |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | TypeDecl *decl = dyn_cast<TypeDecl>(d); |
| 600 | |
| 601 | if (!decl || !Context.getTypeDeclType(decl)->isUnionType()) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 602 | Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 603 | "transparent_union", "union"); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | //QualType QTy = Context.getTypeDeclType(decl); |
| 608 | //const RecordType *Ty = QTy->getAsUnionType(); |
| 609 | |
| 610 | // FIXME |
| 611 | // Ty->addAttr(new TransparentUnionAttr()); |
| 612 | } |
| 613 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 614 | void Sema::HandleAnnotateAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 615 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 616 | if (Attr.getNumArgs() != 1) { |
| 617 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 618 | std::string("1")); |
| 619 | return; |
| 620 | } |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 621 | Expr *argExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 622 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
| 623 | |
| 624 | // Make sure that there is a string literal as the annotation's single |
| 625 | // argument. |
| 626 | if (!SE) { |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 627 | Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | d->addAttr(new AnnotateAttr(std::string(SE->getStrData(), |
| 631 | SE->getByteLength()))); |
| 632 | } |
| 633 | |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 634 | void Sema::HandleAlignedAttribute(Decl *d, const AttributeList &Attr) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 635 | // check the attribute arguments. |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 636 | if (Attr.getNumArgs() > 1) { |
| 637 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 638 | std::string("1")); |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | unsigned Align = 0; |
Chris Lattner | 1c15113 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 643 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 644 | // FIXME: This should be the target specific maximum alignment. |
| 645 | // (For now we just use 128 bits which is the maximum on X86. |
| 646 | Align = 128; |
| 647 | return; |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | b0011ca | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 649 | |
| 650 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 651 | llvm::APSInt Alignment(32); |
| 652 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, Context)) { |
| 653 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int, |
| 654 | "aligned", alignmentExpr->getSourceRange()); |
| 655 | return; |
| 656 | } |
| 657 | d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6953a07 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 658 | } |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 660 | /// HandleModeAttribute - This attribute modifies the width of a decl with |
| 661 | /// primitive type. |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 662 | /// |
| 663 | /// Despite what would be logical, the mode attribute is a decl attribute, |
| 664 | /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make |
| 665 | /// 'G' be HImode, not an intermediate pointer. |
| 666 | /// |
| 667 | void Sema::HandleModeAttribute(Decl *D, const AttributeList &Attr) { |
| 668 | // This attribute isn't documented, but glibc uses it. It changes |
| 669 | // the width of an int or unsigned int to the specified size. |
| 670 | |
| 671 | // Check that there aren't any arguments |
| 672 | if (Attr.getNumArgs() != 0) { |
| 673 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments, |
| 674 | std::string("0")); |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | IdentifierInfo *Name = Attr.getParameterName(); |
| 679 | if (!Name) { |
| 680 | Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
| 681 | return; |
| 682 | } |
| 683 | const char *Str = Name->getName(); |
| 684 | unsigned Len = Name->getLength(); |
| 685 | |
| 686 | // Normalize the attribute name, __foo__ becomes foo. |
| 687 | if (Len > 4 && Str[0] == '_' && Str[1] == '_' && |
| 688 | Str[Len - 2] == '_' && Str[Len - 1] == '_') { |
| 689 | Str += 2; |
| 690 | Len -= 4; |
| 691 | } |
| 692 | |
| 693 | unsigned DestWidth = 0; |
| 694 | bool IntegerMode = true; |
| 695 | switch (Len) { |
| 696 | case 2: |
| 697 | if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; } |
| 698 | if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; } |
| 699 | if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; } |
| 700 | if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; } |
| 701 | if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; } |
| 702 | if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; } |
| 703 | if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; } |
| 704 | if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; } |
| 705 | if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; } |
| 706 | break; |
| 707 | case 4: |
| 708 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 709 | // pointer on PIC16 and other embedded platforms. |
| 710 | if (!memcmp(Str, "word", 4)) |
| 711 | DestWidth = Context.Target.getPointerWidth(0); |
| 712 | if (!memcmp(Str, "byte", 4)) |
| 713 | DestWidth = Context.Target.getCharWidth(); |
| 714 | break; |
| 715 | case 7: |
| 716 | if (!memcmp(Str, "pointer", 7)) |
| 717 | DestWidth = Context.Target.getPointerWidth(0); |
| 718 | break; |
| 719 | } |
| 720 | |
| 721 | QualType OldTy; |
| 722 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 723 | OldTy = TD->getUnderlyingType(); |
| 724 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 725 | OldTy = VD->getType(); |
| 726 | else { |
Chris Lattner | 8ed14aa | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 727 | Diag(D->getLocation(), diag::err_attr_wrong_decl, "mode", |
Chris Lattner | dc78956 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 728 | SourceRange(Attr.getLoc(), Attr.getLoc())); |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | // FIXME: Need proper fixed-width types |
| 733 | QualType NewTy; |
| 734 | switch (DestWidth) { |
| 735 | case 0: |
| 736 | Diag(Attr.getLoc(), diag::err_unknown_machine_mode, Name->getName()); |
| 737 | return; |
| 738 | default: |
| 739 | Diag(Attr.getLoc(), diag::err_unsupported_machine_mode, Name->getName()); |
| 740 | return; |
| 741 | case 8: |
| 742 | assert(IntegerMode); |
| 743 | if (OldTy->isSignedIntegerType()) |
| 744 | NewTy = Context.SignedCharTy; |
| 745 | else |
| 746 | NewTy = Context.UnsignedCharTy; |
| 747 | break; |
| 748 | case 16: |
| 749 | assert(IntegerMode); |
| 750 | if (OldTy->isSignedIntegerType()) |
| 751 | NewTy = Context.ShortTy; |
| 752 | else |
| 753 | NewTy = Context.UnsignedShortTy; |
| 754 | break; |
| 755 | case 32: |
| 756 | if (!IntegerMode) |
| 757 | NewTy = Context.FloatTy; |
| 758 | else if (OldTy->isSignedIntegerType()) |
| 759 | NewTy = Context.IntTy; |
| 760 | else |
| 761 | NewTy = Context.UnsignedIntTy; |
| 762 | break; |
| 763 | case 64: |
| 764 | if (!IntegerMode) |
| 765 | NewTy = Context.DoubleTy; |
| 766 | else if (OldTy->isSignedIntegerType()) |
| 767 | NewTy = Context.LongLongTy; |
| 768 | else |
| 769 | NewTy = Context.UnsignedLongLongTy; |
| 770 | break; |
| 771 | } |
| 772 | |
| 773 | if (!OldTy->getAsBuiltinType()) |
| 774 | Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 775 | else if (!(IntegerMode && OldTy->isIntegerType()) && |
| 776 | !(!IntegerMode && OldTy->isFloatingType())) { |
| 777 | Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 778 | } |
| 779 | |
| 780 | // Install the new type. |
| 781 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 782 | TD->setUnderlyingType(NewTy); |
| 783 | else |
| 784 | cast<ValueDecl>(D)->setType(NewTy); |
| 785 | } |