Intel SPMD Program Compiler  1.12.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 #define TYPEQUAL_NOINLINE (1 << 9)
79 
80 /** @brief Representation of the declaration specifiers in a declaration.
81 
82  In other words, this represents all of the stuff that applies to all of
83  the (possibly multiple) variables in a declaration.
84  */
85 class DeclSpecs {
86  public:
87  DeclSpecs(const Type *t = NULL, StorageClass sc = SC_NONE, int tq = TYPEQUAL_NONE);
88 
89  void Print() const;
90 
92 
93  /** Zero or more of the TYPEQUAL_* values, ANDed together. */
95 
96  /** The basic type provided in the declaration; this should be an
97  AtomicType, EnumType, StructType, or VectorType; other types (like
98  ArrayTypes) will end up being created if a particular declaration
99  has an array size, etc.
100  */
101  const Type *baseType;
102 
103  const Type *GetBaseType(SourcePos pos) const;
104 
105  /** If this is a declaration with a vector type, this gives the vector
106  width. For non-vector types, this is zero.
107  */
109 
110  /** If this is a declaration with an "soa<n>" qualifier, this gives the
111  SOA width specified. Otherwise this is zero.
112  */
113  int soaWidth;
114 
115  std::vector<std::pair<std::string, SourcePos>> declSpecList;
116 };
117 
119 
120 /** @brief Representation of the declaration of a single variable.
121 
122  In conjunction with an instance of the DeclSpecs, this gives us
123  everything we need for a full variable declaration.
124  */
125 class Declarator {
126  public:
128 
129  /** Once a DeclSpecs instance is available, this method completes the
130  initialization of the type member.
131  */
132  void InitFromDeclSpecs(DeclSpecs *ds);
133 
134  void InitFromType(const Type *base, DeclSpecs *ds);
135 
136  void Print(int indent) const;
137 
138  /** Position of the declarator in the source program. */
139  const SourcePos pos;
140 
141  /** The kind of this declarator; complex declarations are assembled as
142  a hierarchy of Declarators. (For example, a pointer to an int
143  would have a root declarator with kind DK_POINTER and with the
144  Declarator::child member pointing to a DK_BASE declarator for the
145  int). */
147 
148  /** Child pointer if needed; this can only be non-NULL if the
149  declarator's kind isn't DK_BASE. */
151 
152  /** Type qualifiers provided with the declarator. */
154 
156 
157  /** For array declarators, this gives the declared size of the array.
158  Unsized arrays have arraySize == 0. */
160 
161  /** Name associated with the declarator. */
162  std::string name;
163 
164  /** Initialization expression for the variable. May be NULL. */
166 
167  /** Type of the declarator. This is NULL until InitFromDeclSpecs() or
168  InitFromType() is called. */
169  const Type *type;
170 
171  /** For function declarations, this holds the Declaration *s for the
172  function's parameters. */
173  std::vector<Declaration *> functionParams;
174 };
175 
176 /** @brief Representation of a full declaration of one or more variables,
177  including the shared DeclSpecs as well as the per-variable Declarators.
178  */
179 class Declaration {
180  public:
181  Declaration(DeclSpecs *ds, std::vector<Declarator *> *dlist = NULL);
183 
184  void Print(int indent) const;
185 
186  /** This method walks through all of the Declarators in a declaration
187  and returns a fully-initialized Symbol and (possibly) and
188  initialization expression for each one. (This allows the rest of
189  the system to not have to worry about the mess of the general
190  Declarator representation.) */
191  std::vector<VariableDeclaration> GetVariableDeclarations() const;
192 
193  /** For any function declarations in the Declaration, add the
194  declaration to the module. */
195  void DeclareFunctions();
196 
198  std::vector<Declarator *> declarators;
199 };
200 
201 /** The parser creates instances of StructDeclaration for the members of
202  structs as it's parsing their declarations. */
204  StructDeclaration(const Type *t, std::vector<Declarator *> *d) : type(t), declarators(d) {}
205 
206  const Type *type;
207  std::vector<Declarator *> *declarators;
208 };
209 
210 /** Given a set of StructDeclaration instances, this returns the types of
211  the elements of the corresponding struct and their names. */
212 extern void GetStructTypesNamesPositions(const std::vector<StructDeclaration *> &sd,
213  llvm::SmallVector<const Type *, 8> *elementTypes,
214  llvm::SmallVector<std::string, 8> *elementNames,
215  llvm::SmallVector<SourcePos, 8> *elementPositions);
216 
217 #endif // ISPC_DECL_H
const Type * baseType
Definition: decl.h:101
const Type * type
Definition: decl.h:169
int arraySize
Definition: decl.h:159
Definition: decl.h:118
DeclSpecs * declSpecs
Definition: decl.h:197
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:686
std::vector< std::pair< std::string, SourcePos > > declSpecList
Definition: decl.h:115
int soaWidth
Definition: decl.h:113
StorageClass storageClass
Definition: decl.h:155
const Type * GetBaseType(SourcePos pos) const
Definition: decl.cpp:147
int vectorSize
Definition: decl.h:108
std::vector< Declarator * > * declarators
Definition: decl.h:207
Expr * initExpr
Definition: decl.h:165
const SourcePos pos
Definition: decl.h:139
StructDeclaration(const Type *t, std::vector< Declarator *> *d)
Definition: decl.h:204
std::string name
Definition: decl.h:162
DeclSpecs(const Type *t=NULL, StorageClass sc=SC_NONE, int tq=TYPEQUAL_NONE)
Definition: decl.cpp:129
std::vector< Declaration * > functionParams
Definition: decl.h:173
Representation of a range of positions in a source file.
Definition: ispc.h:131
StorageClass storageClass
Definition: decl.h:91
Representation of a full declaration of one or more variables, including the shared DeclSpecs as well...
Definition: decl.h:179
StorageClass
Definition: ispc.h:117
DeclaratorKind
Definition: decl.h:118
Representation of the declaration of a single variable.
Definition: decl.h:125
void Print() const
Definition: decl.cpp:237
std::vector< Declarator * > declarators
Definition: decl.h:198
Definition: ispc.h:117
Declarator * child
Definition: decl.h:150
const DeclaratorKind kind
Definition: decl.h:146
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:94
Main ispc.header file. Defines Target, Globals and Opt classes.
const Type * type
Definition: decl.h:206
Definition: decl.h:118
Representation of the declaration specifiers in a declaration.
Definition: decl.h:85
int typeQualifiers
Definition: decl.h:153