SemaType.cpp revision 62f5f7ffad57e0c2af2b308af3735351505937cb
1//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
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 type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
20using namespace clang;
21
22/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
23/// type object.  This returns null on error.
24QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
25  // FIXME: Should move the logic from DeclSpec::Finish to here for validity
26  // checking.
27  QualType Result;
28
29  switch (DS.getTypeSpecType()) {
30  default: assert(0 && "Unknown TypeSpecType!");
31  case DeclSpec::TST_void:
32    Result = Context.VoidTy;
33    break;
34  case DeclSpec::TST_char:
35    if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
36      Result = Context.CharTy;
37    else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
38      Result = Context.SignedCharTy;
39    else {
40      assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
41             "Unknown TSS value");
42      Result = Context.UnsignedCharTy;
43    }
44    break;
45  case DeclSpec::TST_unspecified:
46    // "<proto1,proto2>" is an objc qualified ID with a missing id.
47    if (llvm::SmallVector<Action::DeclTy *, 8> *PQ=DS.getProtocolQualifiers()) {
48      Action::DeclTy **PPDecl = &(*PQ)[0];
49      Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)(PPDecl),
50                                              DS.getNumProtocolQualifiers());
51      break;
52    }
53
54    // Unspecified typespec defaults to int in C90.  However, the C90 grammar
55    // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
56    // type-qualifier, or storage-class-specifier.  If not, emit an extwarn.
57    // Note that the one exception to this is function definitions, which are
58    // allowed to be completely missing a declspec.  This is handled in the
59    // parser already though by it pretending to have seen an 'int' in this
60    // case.
61    if (getLangOptions().ImplicitInt) {
62      if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
63                                       DeclSpec::PQ_TypeSpecifier |
64                                       DeclSpec::PQ_TypeQualifier)) == 0)
65        Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
66    } else {
67      // C99 and C++ require a type specifier.  For example, C99 6.7.2p2 says:
68      // "At least one type specifier shall be given in the declaration
69      // specifiers in each declaration, and in the specifier-qualifier list in
70      // each struct declaration and type name."
71      if (!DS.hasTypeSpecifier())
72        Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
73    }
74
75    // FALL THROUGH.
76  case DeclSpec::TST_int: {
77    if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
78      switch (DS.getTypeSpecWidth()) {
79      case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
80      case DeclSpec::TSW_short:       Result = Context.ShortTy; break;
81      case DeclSpec::TSW_long:        Result = Context.LongTy; break;
82      case DeclSpec::TSW_longlong:    Result = Context.LongLongTy; break;
83      }
84    } else {
85      switch (DS.getTypeSpecWidth()) {
86      case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
87      case DeclSpec::TSW_short:       Result = Context.UnsignedShortTy; break;
88      case DeclSpec::TSW_long:        Result = Context.UnsignedLongTy; break;
89      case DeclSpec::TSW_longlong:    Result =Context.UnsignedLongLongTy; break;
90      }
91    }
92    break;
93  }
94  case DeclSpec::TST_float: Result = Context.FloatTy; break;
95  case DeclSpec::TST_double:
96    if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
97      Result = Context.LongDoubleTy;
98    else
99      Result = Context.DoubleTy;
100    break;
101  case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
102  case DeclSpec::TST_decimal32:    // _Decimal32
103  case DeclSpec::TST_decimal64:    // _Decimal64
104  case DeclSpec::TST_decimal128:   // _Decimal128
105    assert(0 && "FIXME: GNU decimal extensions not supported yet!");
106  case DeclSpec::TST_class:
107  case DeclSpec::TST_enum:
108  case DeclSpec::TST_union:
109  case DeclSpec::TST_struct: {
110    Decl *D = static_cast<Decl *>(DS.getTypeRep());
111    assert(D && "Didn't get a decl for a class/enum/union/struct?");
112    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
113           DS.getTypeSpecSign() == 0 &&
114           "Can't handle qualifiers on typedef names yet!");
115    // TypeQuals handled by caller.
116    Result = Context.getTypeDeclType(cast<TypeDecl>(D));
117    break;
118  }
119  case DeclSpec::TST_typedef: {
120    Decl *D = static_cast<Decl *>(DS.getTypeRep());
121    assert(D && "Didn't get a decl for a typedef?");
122    assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
123           DS.getTypeSpecSign() == 0 &&
124           "Can't handle qualifiers on typedef names yet!");
125
126    // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
127    // we have this "hack" for now...
128    if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
129      if (DS.getProtocolQualifiers() == 0) {
130        Result = Context.getObjCInterfaceType(ObjCIntDecl);
131        break;
132      }
133
134      Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
135      Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
136                                   reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
137                                                 DS.getNumProtocolQualifiers());
138      break;
139    } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
140      if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
141          && DS.getProtocolQualifiers()) {
142          // id<protocol-list>
143        Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
144        Result = Context.getObjCQualifiedIdType(
145                                 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
146                                            DS.getNumProtocolQualifiers());
147        break;
148      }
149    }
150    // TypeQuals handled by caller.
151    Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
152    break;
153  }
154  case DeclSpec::TST_typeofType:
155    Result = QualType::getFromOpaquePtr(DS.getTypeRep());
156    assert(!Result.isNull() && "Didn't get a type for typeof?");
157    // TypeQuals handled by caller.
158    Result = Context.getTypeOfType(Result);
159    break;
160  case DeclSpec::TST_typeofExpr: {
161    Expr *E = static_cast<Expr *>(DS.getTypeRep());
162    assert(E && "Didn't get an expression for typeof?");
163    // TypeQuals handled by caller.
164    Result = Context.getTypeOfExpr(E);
165    break;
166  }
167  }
168
169  // Handle complex types.
170  if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
171    Result = Context.getComplexType(Result);
172
173  assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
174         "FIXME: imaginary types not supported yet!");
175
176  // See if there are any attributes on the declspec that apply to the type (as
177  // opposed to the decl).
178  if (const AttributeList *AL = DS.getAttributes())
179    ProcessTypeAttributeList(Result, AL);
180
181  // Apply const/volatile/restrict qualifiers to T.
182  if (unsigned TypeQuals = DS.getTypeQualifiers()) {
183
184    // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
185    // or incomplete types shall not be restrict-qualified."  C++ also allows
186    // restrict-qualified references.
187    if (TypeQuals & QualType::Restrict) {
188      if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
189        QualType EltTy = PT->getPointeeType();
190
191        // If we have a pointer or reference, the pointee must have an object or
192        // incomplete type.
193        if (!EltTy->isIncompleteOrObjectType()) {
194          Diag(DS.getRestrictSpecLoc(),
195               diag::err_typecheck_invalid_restrict_invalid_pointee,
196               EltTy.getAsString(), DS.getSourceRange());
197          TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
198        }
199      } else {
200        Diag(DS.getRestrictSpecLoc(),
201             diag::err_typecheck_invalid_restrict_not_pointer,
202             Result.getAsString(), DS.getSourceRange());
203        TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
204      }
205    }
206
207    // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
208    // of a function type includes any type qualifiers, the behavior is
209    // undefined."
210    if (Result->isFunctionType() && TypeQuals) {
211      // Get some location to point at, either the C or V location.
212      SourceLocation Loc;
213      if (TypeQuals & QualType::Const)
214        Loc = DS.getConstSpecLoc();
215      else {
216        assert((TypeQuals & QualType::Volatile) &&
217               "Has CV quals but not C or V?");
218        Loc = DS.getVolatileSpecLoc();
219      }
220      Diag(Loc, diag::warn_typecheck_function_qualifiers,
221           Result.getAsString(), DS.getSourceRange());
222    }
223
224    Result = Result.getQualifiedType(TypeQuals);
225  }
226  return Result;
227}
228
229/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
230/// instances.
231QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
232  // long long is a C99 feature.
233  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
234      D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
235    Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
236
237  QualType T = ConvertDeclSpecToType(D.getDeclSpec());
238
239  // Walk the DeclTypeInfo, building the recursive type as we go.  DeclTypeInfos
240  // are ordered from the identifier out, which is opposite of what we want :).
241  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
242    DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
243    switch (DeclType.Kind) {
244    default: assert(0 && "Unknown decltype!");
245    case DeclaratorChunk::Pointer:
246      if (T->isReferenceType()) {
247        // C++ 8.3.2p4: There shall be no ... pointers to references ...
248        Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
249             D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
250        D.setInvalidType(true);
251        T = Context.IntTy;
252      }
253
254      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
255      // object or incomplete types shall not be restrict-qualified."
256      if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
257          !T->isIncompleteOrObjectType()) {
258        Diag(DeclType.Loc,
259             diag::err_typecheck_invalid_restrict_invalid_pointee,
260             T.getAsString());
261        DeclType.Ptr.TypeQuals &= QualType::Restrict;
262      }
263
264      // Apply the pointer typequals to the pointer object.
265      T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
266      break;
267    case DeclaratorChunk::Reference:
268      if (const ReferenceType *RT = T->getAsReferenceType()) {
269        // C++ 8.3.2p4: There shall be no references to references.
270        Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
271             D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
272        D.setInvalidType(true);
273        T = RT->getPointeeType();
274      }
275
276      // Enforce C99 6.7.3p2: "Types other than pointer types derived from
277      // object or incomplete types shall not be restrict-qualified."
278      if (DeclType.Ref.HasRestrict &&
279          !T->isIncompleteOrObjectType()) {
280        Diag(DeclType.Loc,
281             diag::err_typecheck_invalid_restrict_invalid_pointee,
282             T.getAsString());
283        DeclType.Ref.HasRestrict = false;
284      }
285
286      T = Context.getReferenceType(T);
287
288      // Handle restrict on references.
289      if (DeclType.Ref.HasRestrict)
290        T.addRestrict();
291      break;
292    case DeclaratorChunk::Array: {
293      DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
294      Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
295      ArrayType::ArraySizeModifier ASM;
296      if (ATI.isStar)
297        ASM = ArrayType::Star;
298      else if (ATI.hasStatic)
299        ASM = ArrayType::Static;
300      else
301        ASM = ArrayType::Normal;
302
303      // C99 6.7.5.2p1: If the element type is an incomplete or function type,
304      // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
305      if (T->isIncompleteType()) {
306        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
307             T.getAsString());
308        T = Context.IntTy;
309        D.setInvalidType(true);
310      } else if (T->isFunctionType()) {
311        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
312             D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
313        T = Context.getPointerType(T);
314        D.setInvalidType(true);
315      } else if (const ReferenceType *RT = T->getAsReferenceType()) {
316        // C++ 8.3.2p4: There shall be no ... arrays of references ...
317        Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
318             D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
319        T = RT->getPointeeType();
320        D.setInvalidType(true);
321      } else if (const RecordType *EltTy = T->getAsRecordType()) {
322        // If the element type is a struct or union that contains a variadic
323        // array, reject it: C99 6.7.2.1p2.
324        if (EltTy->getDecl()->hasFlexibleArrayMember()) {
325          Diag(DeclType.Loc, diag::err_flexible_array_in_array,
326               T.getAsString());
327          T = Context.IntTy;
328          D.setInvalidType(true);
329        }
330      }
331      // C99 6.7.5.2p1: The size expression shall have integer type.
332      if (ArraySize && !ArraySize->getType()->isIntegerType()) {
333        Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
334             ArraySize->getType().getAsString(), ArraySize->getSourceRange());
335        D.setInvalidType(true);
336        delete ArraySize;
337        ATI.NumElts = ArraySize = 0;
338      }
339      llvm::APSInt ConstVal(32);
340      if (!ArraySize) {
341        T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
342      } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
343                 !T->isConstantSizeType()) {
344        // Per C99, a variable array is an array with either a non-constant
345        // size or an element type that has a non-constant-size
346        T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
347      } else {
348        // C99 6.7.5.2p1: If the expression is a constant expression, it shall
349        // have a value greater than zero.
350        if (ConstVal.isSigned()) {
351          if (ConstVal.isNegative()) {
352            Diag(ArraySize->getLocStart(),
353                 diag::err_typecheck_negative_array_size,
354                 ArraySize->getSourceRange());
355            D.setInvalidType(true);
356          } else if (ConstVal == 0) {
357            // GCC accepts zero sized static arrays.
358            Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
359                 ArraySize->getSourceRange());
360          }
361        }
362        T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
363      }
364      // If this is not C99, extwarn about VLA's and C99 array size modifiers.
365      if (!getLangOptions().C99 &&
366          (ASM != ArrayType::Normal ||
367           (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
368        Diag(D.getIdentifierLoc(), diag::ext_vla);
369      break;
370    }
371    case DeclaratorChunk::Function:
372      // If the function declarator has a prototype (i.e. it is not () and
373      // does not have a K&R-style identifier list), then the arguments are part
374      // of the type, otherwise the argument list is ().
375      const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
376
377      // C99 6.7.5.3p1: The return type may not be a function or array type.
378      if (T->isArrayType() || T->isFunctionType()) {
379        Diag(DeclType.Loc, diag::err_func_returning_array_function,
380             T.getAsString());
381        T = Context.IntTy;
382        D.setInvalidType(true);
383      }
384
385      if (!FTI.hasPrototype) {
386        // Simple void foo(), where the incoming T is the result type.
387        T = Context.getFunctionTypeNoProto(T);
388
389        // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
390        if (FTI.NumArgs != 0)
391          Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
392
393      } else {
394        // Otherwise, we have a function with an argument list that is
395        // potentially variadic.
396        llvm::SmallVector<QualType, 16> ArgTys;
397
398        for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
399          ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
400          QualType ArgTy = Param->getType();
401          assert(!ArgTy.isNull() && "Couldn't parse type?");
402          //
403          // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
404          // This matches the conversion that is done in
405          // Sema::ActOnParamDeclarator(). Without this conversion, the
406          // argument type in the function prototype *will not* match the
407          // type in ParmVarDecl (which makes the code generator unhappy).
408          //
409          // FIXME: We still apparently need the conversion in
410          // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
411          // it should be driving off the type being created here.
412          //
413          // FIXME: If a source translation tool needs to see the original type,
414          // then we need to consider storing both types somewhere...
415          //
416          if (ArgTy->isArrayType()) {
417            ArgTy = Context.getArrayDecayedType(ArgTy);
418          } else if (ArgTy->isFunctionType())
419            ArgTy = Context.getPointerType(ArgTy);
420
421          // Look for 'void'.  void is allowed only as a single argument to a
422          // function with no other parameters (C99 6.7.5.3p10).  We record
423          // int(void) as a FunctionTypeProto with an empty argument list.
424          else if (ArgTy->isVoidType()) {
425            // If this is something like 'float(int, void)', reject it.  'void'
426            // is an incomplete type (C99 6.2.5p19) and function decls cannot
427            // have arguments of incomplete type.
428            if (FTI.NumArgs != 1 || FTI.isVariadic) {
429              Diag(DeclType.Loc, diag::err_void_only_param);
430              ArgTy = Context.IntTy;
431              Param->setType(ArgTy);
432            } else if (FTI.ArgInfo[i].Ident) {
433              // Reject, but continue to parse 'int(void abc)'.
434              Diag(FTI.ArgInfo[i].IdentLoc,
435                   diag::err_param_with_void_type);
436              ArgTy = Context.IntTy;
437              Param->setType(ArgTy);
438            } else {
439              // Reject, but continue to parse 'float(const void)'.
440              if (ArgTy.getCVRQualifiers())
441                Diag(DeclType.Loc, diag::err_void_param_qualified);
442
443              // Do not add 'void' to the ArgTys list.
444              break;
445            }
446          }
447
448          ArgTys.push_back(ArgTy);
449        }
450        T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
451                                    FTI.isVariadic);
452      }
453      break;
454    }
455
456    // See if there are any attributes on this declarator chunk.
457    if (const AttributeList *AL = DeclType.getAttrs())
458      ProcessTypeAttributeList(T, AL);
459  }
460
461  // If there were any type attributes applied to the decl itself (not the
462  // type, apply the type attribute to the type!)
463  if (const AttributeList *Attrs = D.getAttributes())
464    ProcessTypeAttributeList(T, Attrs);
465
466  return T;
467}
468
469/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
470/// declarator
471QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
472  ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
473  QualType T = MDecl->getResultType();
474  llvm::SmallVector<QualType, 16> ArgTys;
475
476  // Add the first two invisible argument types for self and _cmd.
477  if (MDecl->isInstance()) {
478    QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
479    selfTy = Context.getPointerType(selfTy);
480    ArgTys.push_back(selfTy);
481  }
482  else
483    ArgTys.push_back(Context.getObjCIdType());
484  ArgTys.push_back(Context.getObjCSelType());
485
486  for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
487    ParmVarDecl *PDecl = MDecl->getParamDecl(i);
488    QualType ArgTy = PDecl->getType();
489    assert(!ArgTy.isNull() && "Couldn't parse type?");
490    // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
491    // This matches the conversion that is done in
492    // Sema::ActOnParamDeclarator().
493    if (ArgTy->isArrayType())
494      ArgTy = Context.getArrayDecayedType(ArgTy);
495    else if (ArgTy->isFunctionType())
496      ArgTy = Context.getPointerType(ArgTy);
497    ArgTys.push_back(ArgTy);
498  }
499  T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
500                              MDecl->isVariadic());
501  return T;
502}
503
504Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
505  // C99 6.7.6: Type names have no identifier.  This is already validated by
506  // the parser.
507  assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
508
509  QualType T = GetTypeForDeclarator(D, S);
510
511  assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
512
513  // Check that there are no default arguments (C++ only).
514  if (getLangOptions().CPlusPlus)
515    CheckExtraCXXDefaultArguments(D);
516
517  // In this context, we *do not* check D.getInvalidType(). If the declarator
518  // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
519  // though it will not reflect the user specified type.
520  return T.getAsOpaquePtr();
521}
522
523
524
525//===----------------------------------------------------------------------===//
526// Type Attribute Processing
527//===----------------------------------------------------------------------===//
528
529/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
530/// specified type.  The attribute contains 1 argument, the id of the address
531/// space for the type.
532static void HandleAddressSpaceTypeAttribute(QualType &Type,
533                                            const AttributeList &Attr, Sema &S){
534  // If this type is already address space qualified, reject it.
535  // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
536  // for two or more different address spaces."
537  if (Type.getAddressSpace()) {
538    S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
539    return;
540  }
541
542  // Check the attribute arguments.
543  if (Attr.getNumArgs() != 1) {
544    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
545           std::string("1"));
546    return;
547  }
548  Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
549  llvm::APSInt addrSpace(32);
550  if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
551    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int,
552           ASArgExpr->getSourceRange());
553    return;
554  }
555
556  unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
557  Type = S.Context.getASQualType(Type, ASIdx);
558}
559
560void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
561  // Scan through and apply attributes to this type where it makes sense.  Some
562  // attributes (such as __address_space__, __vector_size__, etc) apply to the
563  // type, but others can be present in the type specifiers even though they
564  // apply to the decl.  Here we apply type attributes and ignore the rest.
565  for (; AL; AL = AL->getNext()) {
566    // If this is an attribute we can handle, do so now, otherwise, add it to
567    // the LeftOverAttrs list for rechaining.
568    switch (AL->getKind()) {
569    default: break;
570    case AttributeList::AT_address_space:
571      HandleAddressSpaceTypeAttribute(Result, *AL, *this);
572      break;
573    }
574  }
575}
576
577
578