zcov: / include/clang/Parse/Designator.h


Files: 1 Branches Taken: 70.0% 28 / 40
Generated: 2010-02-10 01:31 Branches Executed: 100.0% 40 / 40
Line Coverage: 100.0% 81 / 81


Programs: 6 Runs 9054


       1                 : //===--- Designator.h - Initialization Designator ---------------*- C++ -*-===//
       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 defines interfaces used to represent Designators in the parser and
      11                 : // is the input to Actions module.
      12                 : //
      13                 : //===----------------------------------------------------------------------===//
      14                 : 
      15                 : #ifndef LLVM_CLANG_PARSE_DESIGNATOR_H
      16                 : #define LLVM_CLANG_PARSE_DESIGNATOR_H
      17                 : 
      18                 : #include "clang/Parse/Action.h"
      19                 : 
      20                 : namespace clang {
      21                 : 
      22                 : /// Designator - This class is a discriminated union which holds the various
      23                 : /// different sorts of designators possible.  A Designation is an array of
      24                 : /// these.  An example of a designator are things like this:
      25                 : ///     [8] .field [47]        // C99 designation: 3 designators
      26                 : ///     [8 ... 47]  field:     // GNU extensions: 2 designators
      27                 : /// These occur in initializers, e.g.:
      28                 : ///  int a[10] = {2, 4, [8]=9, 10};
      29                 : ///
      30              261: class Designator {
      31                 : public:
      32                 :   enum DesignatorKind {
      33                 :     FieldDesignator, ArrayDesignator, ArrayRangeDesignator
      34                 :   };
      35                 : private:
      36                 :   DesignatorKind Kind;
      37                 : 
      38                 :   struct FieldDesignatorInfo {
      39                 :     const IdentifierInfo *II;
      40                 :     unsigned DotLoc;
      41                 :     unsigned NameLoc;
      42                 :   };
      43                 :   struct ArrayDesignatorInfo {
      44                 :     ActionBase::ExprTy *Index;
      45                 :     unsigned LBracketLoc;
      46                 :     mutable unsigned  RBracketLoc;
      47                 :   };
      48                 :   struct ArrayRangeDesignatorInfo {
      49                 :     ActionBase::ExprTy *Start, *End;
      50                 :     unsigned LBracketLoc, EllipsisLoc;
      51                 :     mutable unsigned RBracketLoc;
      52                 :   };
      53                 : 
      54                 :   union {
      55                 :     FieldDesignatorInfo FieldInfo;
      56                 :     ArrayDesignatorInfo ArrayInfo;
      57                 :     ArrayRangeDesignatorInfo ArrayRangeInfo;
      58                 :   };
      59                 : 
      60                 : public:
      61                 : 
      62              221:   DesignatorKind getKind() const { return Kind; }
      63              438:   bool isFieldDesignator() const { return Kind == FieldDesignator; }
      64              512:   bool isArrayDesignator() const { return Kind == ArrayDesignator; }
      65               57:   bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
      66                 : 
      67              146:   const IdentifierInfo *getField() const {
                      146: branch 1 taken
                        0: branch 2 not taken
      68              146:     assert(isFieldDesignator() && "Invalid accessor");
      69              146:     return FieldInfo.II;
      70                 :   }
      71                 : 
      72              146:   SourceLocation getDotLoc() const {
                      146: branch 1 taken
                        0: branch 2 not taken
      73              146:     assert(isFieldDesignator() && "Invalid accessor");
      74              146:     return SourceLocation::getFromRawEncoding(FieldInfo.DotLoc);
      75                 :   }
      76                 : 
      77              146:   SourceLocation getFieldLoc() const {
                      146: branch 1 taken
                        0: branch 2 not taken
      78              146:     assert(isFieldDesignator() && "Invalid accessor");
      79              146:     return SourceLocation::getFromRawEncoding(FieldInfo.NameLoc);
      80                 :   }
      81                 : 
      82               65:   ActionBase::ExprTy *getArrayIndex() const {
                       65: branch 1 taken
                        0: branch 2 not taken
      83               65:     assert(isArrayDesignator() && "Invalid accessor");
      84               65:     return ArrayInfo.Index;
      85                 :   }
      86                 : 
      87               10:   ActionBase::ExprTy *getArrayRangeStart() const {
                       10: branch 1 taken
                        0: branch 2 not taken
      88               10:     assert(isArrayRangeDesignator() && "Invalid accessor");
      89               10:     return ArrayRangeInfo.Start;
      90                 :   }
      91               10:   ActionBase::ExprTy *getArrayRangeEnd() const {
                       10: branch 1 taken
                        0: branch 2 not taken
      92               10:     assert(isArrayRangeDesignator() && "Invalid accessor");
      93               10:     return ArrayRangeInfo.End;
      94                 :   }
      95                 : 
      96               73:   SourceLocation getLBracketLoc() const {
      97                 :     assert((isArrayDesignator() || isArrayRangeDesignator()) &&
                        9: branch 1 taken
                       64: branch 2 taken
                        9: branch 4 taken
                        0: branch 5 not taken
      98               73:            "Invalid accessor");
                       64: branch 1 taken
                        9: branch 2 taken
      99               73:     if (isArrayDesignator())
     100               64:       return SourceLocation::getFromRawEncoding(ArrayInfo.LBracketLoc);
     101                 :     else
     102                9:       return SourceLocation::getFromRawEncoding(ArrayRangeInfo.LBracketLoc);
     103                 :   }
     104                 : 
     105               73:   SourceLocation getRBracketLoc() const {
     106                 :     assert((isArrayDesignator() || isArrayRangeDesignator()) &&
                        9: branch 1 taken
                       64: branch 2 taken
                        9: branch 4 taken
                        0: branch 5 not taken
     107               73:            "Invalid accessor");
                       64: branch 1 taken
                        9: branch 2 taken
     108               73:     if (isArrayDesignator())
     109               64:       return SourceLocation::getFromRawEncoding(ArrayInfo.RBracketLoc);
     110                 :     else
     111                9:       return SourceLocation::getFromRawEncoding(ArrayRangeInfo.RBracketLoc);
     112                 :   }
     113                 : 
     114               10:   SourceLocation getEllipsisLoc() const {
                       10: branch 1 taken
                        0: branch 2 not taken
     115               10:     assert(isArrayRangeDesignator() && "Invalid accessor");
     116               10:     return SourceLocation::getFromRawEncoding(ArrayRangeInfo.EllipsisLoc);
     117                 :   }
     118                 : 
     119                 :   static Designator getField(const IdentifierInfo *II, SourceLocation DotLoc,
     120              152:                              SourceLocation NameLoc) {
     121                 :     Designator D;
     122              152:     D.Kind = FieldDesignator;
     123              152:     D.FieldInfo.II = II;
     124              152:     D.FieldInfo.DotLoc = DotLoc.getRawEncoding();
     125              152:     D.FieldInfo.NameLoc = NameLoc.getRawEncoding();
     126                 :     return D;
     127                 :   }
     128                 : 
     129                 :   static Designator getArray(ActionBase::ExprTy *Index,
     130               69:                              SourceLocation LBracketLoc) {
     131                 :     Designator D;
     132               69:     D.Kind = ArrayDesignator;
     133               69:     D.ArrayInfo.Index = Index;
     134               69:     D.ArrayInfo.LBracketLoc = LBracketLoc.getRawEncoding();
     135               69:     D.ArrayInfo.RBracketLoc = 0;
     136                 :     return D;
     137                 :   }
     138                 : 
     139                 :   static Designator getArrayRange(ActionBase::ExprTy *Start,
     140                 :                                   ActionBase::ExprTy *End,
     141                 :                                   SourceLocation LBracketLoc,
     142               10:                                   SourceLocation EllipsisLoc) {
     143                 :     Designator D;
     144               10:     D.Kind = ArrayRangeDesignator;
     145               10:     D.ArrayRangeInfo.Start = Start;
     146               10:     D.ArrayRangeInfo.End = End;
     147               10:     D.ArrayRangeInfo.LBracketLoc = LBracketLoc.getRawEncoding();
     148               10:     D.ArrayRangeInfo.EllipsisLoc = EllipsisLoc.getRawEncoding();
     149               10:     D.ArrayRangeInfo.RBracketLoc = 0;
     150                 :     return D;
     151                 :   }
     152                 : 
     153               73:   void setRBracketLoc(SourceLocation RBracketLoc) const {
     154                 :     assert((isArrayDesignator() || isArrayRangeDesignator()) &&
                        8: branch 1 taken
                       65: branch 2 taken
                        8: branch 4 taken
                        0: branch 5 not taken
     155               73:            "Invalid accessor");
                       65: branch 1 taken
                        8: branch 2 taken
     156               73:     if (isArrayDesignator())
     157               65:       ArrayInfo.RBracketLoc = RBracketLoc.getRawEncoding();
     158                 :     else
     159                8:       ArrayRangeInfo.RBracketLoc = RBracketLoc.getRawEncoding();
     160               73:   }
     161                 : 
     162                 :   /// ClearExprs - Null out any expression references, which prevents them from
     163                 :   /// being 'delete'd later.
     164              217:   void ClearExprs(Action &Actions) {
                      144: branch 0 taken
                       64: branch 1 taken
                        9: branch 2 taken
                        0: branch 3 not taken
     165              217:     switch (Kind) {
     166              144:     case FieldDesignator: return;
     167                 :     case ArrayDesignator:
     168               64:       ArrayInfo.Index = 0;
     169               64:       return;
     170                 :     case ArrayRangeDesignator:
     171                9:       ArrayRangeInfo.Start = 0;
     172                9:       ArrayRangeInfo.End = 0;
     173                9:       return;
     174                 :     }
     175                 :   }
     176                 : 
     177                 :   /// FreeExprs - Release any unclaimed memory for the expressions in this
     178                 :   /// designator.
     179                 :   void FreeExprs(Action &Actions) {
     180                 :     switch (Kind) {
     181                 :     case FieldDesignator: return; // nothing to free.
     182                 :     case ArrayDesignator:
     183                 :       Actions.DeleteExpr(getArrayIndex());
     184                 :       return;
     185                 :     case ArrayRangeDesignator:
     186                 :       Actions.DeleteExpr(getArrayRangeStart());
     187                 :       Actions.DeleteExpr(getArrayRangeEnd());
     188                 :       return;
     189                 :     }
     190                 :   }
     191                 : };
     192                 : 
     193                 : 
     194                 : /// Designation - Represent a full designation, which is a sequence of
     195                 : /// designators.  This class is mostly a helper for InitListDesignations.
     196              200: class Designation {
     197                 :   /// InitIndex - The index of the initializer expression this is for.  For
     198                 :   /// example, if the initializer were "{ A, .foo=B, C }" a Designation would
     199                 :   /// exist with InitIndex=1, because element #1 has a designation.
     200                 :   unsigned InitIndex;
     201                 : 
     202                 :   /// Designators - The actual designators for this initializer.
     203                 :   llvm::SmallVector<Designator, 2> Designators;
     204                 : 
     205                 :   Designation(unsigned Idx) : InitIndex(Idx) {}
     206                 : public:
     207              200:   Designation() : InitIndex(4000) {}
     208                 : 
     209                 :   /// AddDesignator - Add a designator to the end of this list.
     210              231:   void AddDesignator(Designator D) {
     211              231:     Designators.push_back(D);
     212              231:   }
     213                 : 
     214              156:   bool empty() const { return Designators.empty(); }
     215                 : 
     216              519:   unsigned getNumDesignators() const { return Designators.size(); }
     217              304:   const Designator &getDesignator(unsigned Idx) const {
                        0: branch 1 not taken
                      304: branch 2 taken
     218              304:     assert(Idx < Designators.size());
     219              304:     return Designators[Idx];
     220                 :   }
     221                 : 
     222                 :   /// ClearExprs - Null out any expression references, which prevents them from
     223                 :   /// being 'delete'd later.
     224              164:   void ClearExprs(Action &Actions) {
                      217: branch 1 taken
                      164: branch 2 taken
     225              381:     for (unsigned i = 0, e = Designators.size(); i != e; ++i)
     226              217:       Designators[i].ClearExprs(Actions);
     227              164:   }
     228                 : 
     229                 :   /// FreeExprs - Release any unclaimed memory for the expressions in this
     230                 :   /// designation.
     231                 :   void FreeExprs(Action &Actions) {
     232                 :     for (unsigned i = 0, e = Designators.size(); i != e; ++i)
     233                 :       Designators[i].FreeExprs(Actions);
     234                 :   }
     235                 : };
     236                 : 
     237                 : } // end namespace clang
     238                 : 
     239                 : #endif

Generated: 2010-02-10 01:31 by zcov