Intel SPMD Program Compiler  1.11.0
decl.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2013, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file decl.h
35  @brief Declarations related to type declarations; the parser basically
36  creates instances of these classes, which are then turned into actual
37  Types.
38 
39  Three classes work together to represent declarations. As an example,
40  consider a declaration like:
41 
42  static uniform int foo, bar[10];
43 
44  An instance of the Declaration class represents this entire declaration
45  of two variables, 'foo' and 'bar'. It holds a single instance of the
46  DeclSpecs class represents the common specifiers for all of the
47  variables--here, that the declaration has the 'static' and 'uniform'
48  qualifiers, and that it's basic type is 'int'. Then for each variable
49  declaration, the Declaraiton class holds an instance of a Declarator,
50  which in turn records the per-variable information like the name, array
51  size (if any), initializer expression, etc.
52 */
53 
54 #ifndef ISPC_DECL_H
55 #define ISPC_DECL_H
56 
57 #include "ispc.h"
58 #include <llvm/ADT/SmallVector.h>
59 
60 struct VariableDeclaration;
61 
62 class Declaration;
63 class Declarator;
64 
65 /* Multiple qualifiers can be provided with types in declarations;
66  therefore, they are set up so that they can be ANDed together into an
67  int. */
68 #define TYPEQUAL_NONE 0
69 #define TYPEQUAL_CONST (1 << 0)
70 #define TYPEQUAL_UNIFORM (1 << 1)
71 #define TYPEQUAL_VARYING (1 << 2)
72 #define TYPEQUAL_TASK (1 << 3)
73 #define TYPEQUAL_SIGNED (1 << 4)
74 #define TYPEQUAL_UNSIGNED (1 << 5)
75 #define TYPEQUAL_INLINE (1 << 6)
76 #define TYPEQUAL_EXPORT (1 << 7)
77 #define TYPEQUAL_UNMASKED (1 << 8)
78 
79 /** @brief Representation of the declaration specifiers in a declaration.
80 
81  In other words, this represents all of the stuff that applies to all of
82  the (possibly multiple) variables in a declaration.
83  */
84 class DeclSpecs {
85  public:
86  DeclSpecs(const Type *t = NULL, StorageClass sc = SC_NONE, int tq = TYPEQUAL_NONE);
87 
88  void Print() const;
89 
91 
92  /** Zero or more of the TYPEQUAL_* values, ANDed together. */
94 
95  /** The basic type provided in the declaration; this should be an
96  AtomicType, EnumType, StructType, or VectorType; other types (like
97  ArrayTypes) will end up being created if a particular declaration
98  has an array size, etc.
99  */
100  const Type *baseType;
101 
102  const Type *GetBaseType(SourcePos pos) const;
103 
104  /** If this is a declaration with a vector type, this gives the vector
105  width. For non-vector types, this is zero.
106  */
108 
109  /** If this is a declaration with an "soa<n>" qualifier, this gives the
110  SOA width specified. Otherwise this is zero.
111  */
112  int soaWidth;
113 
114  std::vector<std::pair<std::string, SourcePos>> declSpecList;
115 };
116 
118 
119 /** @brief Representation of the declaration of a single variable.
120 
121  In conjunction with an instance of the DeclSpecs, this gives us
122  everything we need for a full variable declaration.
123  */
124 class Declarator {
125  public:
127 
128  /** Once a DeclSpecs instance is available, this method completes the
129  initialization of the type member.
130  */
131  void InitFromDeclSpecs(DeclSpecs *ds);
132 
133  void InitFromType(const Type *base, DeclSpecs *ds);
134 
135  void Print(int indent) const;
136 
137  /** Position of the declarator in the source program. */
138  const SourcePos pos;
139 
140  /** The kind of this declarator; complex declarations are assembled as
141  a hierarchy of Declarators. (For example, a pointer to an int
142  would have a root declarator with kind DK_POINTER and with the
143  Declarator::child member pointing to a DK_BASE declarator for the
144  int). */
146 
147  /** Child pointer if needed; this can only be non-NULL if the
148  declarator's kind isn't DK_BASE. */
150 
151  /** Type qualifiers provided with the declarator. */
153 
155 
156  /** For array declarators, this gives the declared size of the array.
157  Unsized arrays have arraySize == 0. */
159 
160  /** Name associated with the declarator. */
161  std::string name;
162 
163  /** Initialization expression for the variable. May be NULL. */
165 
166  /** Type of the declarator. This is NULL until InitFromDeclSpecs() or
167  InitFromType() is called. */
168  const Type *type;
169 
170  /** For function declarations, this holds the Declaration *s for the
171  function's parameters. */
172  std::vector<Declaration *> functionParams;
173 };
174 
175 /** @brief Representation of a full declaration of one or more variables,
176  including the shared DeclSpecs as well as the per-variable Declarators.
177  */
178 class Declaration {
179  public:
180  Declaration(DeclSpecs *ds, std::vector<Declarator *> *dlist = NULL);
182 
183  void Print(int indent) const;
184 
185  /** This method walks through all of the Declarators in a declaration
186  and returns a fully-initialized Symbol and (possibly) and
187  initialization expression for each one. (This allows the rest of
188  the system to not have to worry about the mess of the general
189  Declarator representation.) */
190  std::vector<VariableDeclaration> GetVariableDeclarations() const;
191 
192  /** For any function declarations in the Declaration, add the
193  declaration to the module. */
194  void DeclareFunctions();
195 
197  std::vector<Declarator *> declarators;
198 };
199 
200 /** The parser creates instances of StructDeclaration for the members of
201  structs as it's parsing their declarations. */
203  StructDeclaration(const Type *t, std::vector<Declarator *> *d) : type(t), declarators(d) {}
204 
205  const Type *type;
206  std::vector<Declarator *> *declarators;
207 };
208 
209 /** Given a set of StructDeclaration instances, this returns the types of
210  the elements of the corresponding struct and their names. */
211 extern void GetStructTypesNamesPositions(const std::vector<StructDeclaration *> &sd,
212  llvm::SmallVector<const Type *, 8> *elementTypes,
213  llvm::SmallVector<std::string, 8> *elementNames,
214  llvm::SmallVector<SourcePos, 8> *elementPositions);
215 
216 #endif // ISPC_DECL_H
const Type * baseType
Definition: decl.h:100
void InitFromType(const Type *base, DeclSpecs *ds)
Definition: decl.cpp:335
void Print(int indent) const
Definition: decl.cpp:282
const Type * type
Definition: decl.h:168
int arraySize
Definition: decl.h:158
Definition: decl.h:117
DeclSpecs * declSpecs
Definition: decl.h:196
void GetStructTypesNamesPositions(const std::vector< StructDeclaration *> &sd, llvm::SmallVector< const Type *, 8 > *elementTypes, llvm::SmallVector< std::string, 8 > *elementNames, llvm::SmallVector< SourcePos, 8 > *elementPositions)
Definition: decl.cpp:685
std::vector< VariableDeclaration > GetVariableDeclarations() const
Definition: decl.cpp:630
std::vector< std::pair< std::string, SourcePos > > declSpecList
Definition: decl.h:114
Declaration(DeclSpecs *ds, std::vector< Declarator *> *dlist=NULL)
Definition: decl.cpp:613
int soaWidth
Definition: decl.h:112
void InitFromDeclSpecs(DeclSpecs *ds)
Definition: decl.cpp:262
StorageClass storageClass
Definition: decl.h:154
const Type * GetBaseType(SourcePos pos) const
Definition: decl.cpp:147
Declarator(DeclaratorKind dk, SourcePos p)
Definition: decl.cpp:253
int vectorSize
Definition: decl.h:107
std::vector< Declarator * > * declarators
Definition: decl.h:206
Expr * initExpr
Definition: decl.h:164
const SourcePos pos
Definition: decl.h:138
StructDeclaration(const Type *t, std::vector< Declarator *> *d)
Definition: decl.h:203
std::string name
Definition: decl.h:161
DeclSpecs(const Type *t=NULL, StorageClass sc=SC_NONE, int tq=TYPEQUAL_NONE)
Definition: decl.cpp:129
std::vector< Declaration * > functionParams
Definition: decl.h:172
Representation of a range of positions in a source file.
Definition: ispc.h:129
StorageClass storageClass
Definition: decl.h:90
Representation of a full declaration of one or more variables, including the shared DeclSpecs as well...
Definition: decl.h:178
StorageClass
Definition: ispc.h:120
DeclaratorKind
Definition: decl.h:117
Representation of the declaration of a single variable.
Definition: decl.h:124
void Print() const
Definition: decl.cpp:237
std::vector< Declarator * > declarators
Definition: decl.h:197
Definition: ispc.h:120
Declarator * child
Definition: decl.h:149
const DeclaratorKind kind
Definition: decl.h:145
Interface class that defines the type abstraction.
Definition: type.h:95
#define TYPEQUAL_NONE
Definition: decl.h:68
Expr is the abstract base class that defines the interface that all expression types must implement...
Definition: expr.h:48
int typeQualifiers
Definition: decl.h:93
void DeclareFunctions()
Definition: decl.cpp:655
void Print(int indent) const
Definition: decl.cpp:675
Main ispc.header file. Defines Target, Globals and Opt classes.
const Type * type
Definition: decl.h:205
Definition: decl.h:117
Representation of the declaration specifiers in a declaration.
Definition: decl.h:84
int typeQualifiers
Definition: decl.h:152