Intel SPMD Program Compiler  1.9.1
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,
87  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 
118 
125 };
126 
127 /** @brief Representation of the declaration of a single variable.
128 
129  In conjunction with an instance of the DeclSpecs, this gives us
130  everything we need for a full variable declaration.
131  */
132 class Declarator {
133 public:
135 
136  /** Once a DeclSpecs instance is available, this method completes the
137  initialization of the type member.
138  */
139  void InitFromDeclSpecs(DeclSpecs *ds);
140 
141  void InitFromType(const Type *base, DeclSpecs *ds);
142 
143  void Print(int indent) const;
144 
145  /** Position of the declarator in the source program. */
146  const SourcePos pos;
147 
148  /** The kind of this declarator; complex declarations are assembled as
149  a hierarchy of Declarators. (For example, a pointer to an int
150  would have a root declarator with kind DK_POINTER and with the
151  Declarator::child member pointing to a DK_BASE declarator for the
152  int). */
154 
155  /** Child pointer if needed; this can only be non-NULL if the
156  declarator's kind isn't DK_BASE. */
158 
159  /** Type qualifiers provided with the declarator. */
161 
163 
164  /** For array declarators, this gives the declared size of the array.
165  Unsized arrays have arraySize == 0. */
167 
168  /** Name associated with the declarator. */
169  std::string name;
170 
171  /** Initialization expression for the variable. May be NULL. */
173 
174  /** Type of the declarator. This is NULL until InitFromDeclSpecs() or
175  InitFromType() is called. */
176  const Type *type;
177 
178  /** For function declarations, this holds the Declaration *s for the
179  function's parameters. */
180  std::vector<Declaration *> functionParams;
181 };
182 
183 
184 /** @brief Representation of a full declaration of one or more variables,
185  including the shared DeclSpecs as well as the per-variable Declarators.
186  */
187 class Declaration {
188 public:
189  Declaration(DeclSpecs *ds, std::vector<Declarator *> *dlist = NULL);
191 
192  void Print(int indent) const;
193 
194  /** This method walks through all of the Declarators in a declaration
195  and returns a fully-initialized Symbol and (possibly) and
196  initialization expression for each one. (This allows the rest of
197  the system to not have to worry about the mess of the general
198  Declarator representation.) */
199  std::vector<VariableDeclaration> GetVariableDeclarations() const;
200 
201  /** For any function declarations in the Declaration, add the
202  declaration to the module. */
203  void DeclareFunctions();
204 
206  std::vector<Declarator *> declarators;
207 };
208 
209 
210 /** The parser creates instances of StructDeclaration for the members of
211  structs as it's parsing their declarations. */
213  StructDeclaration(const Type *t, std::vector<Declarator *> *d)
214  : type(t), declarators(d) { }
215 
216  const Type *type;
217  std::vector<Declarator *> *declarators;
218 };
219 
220 
221 /** Given a set of StructDeclaration instances, this returns the types of
222  the elements of the corresponding struct and their names. */
223 extern void GetStructTypesNamesPositions(const std::vector<StructDeclaration *> &sd,
224  llvm::SmallVector<const Type *, 8> *elementTypes,
225  llvm::SmallVector<std::string, 8> *elementNames,
226  llvm::SmallVector<SourcePos, 8> *elementPositions);
227 
228 #endif // ISPC_DECL_H
const Type * baseType
Definition: decl.h:101
void InitFromType(const Type *base, DeclSpecs *ds)
Definition: decl.cpp:323
const Type * type
Definition: decl.h:176
int arraySize
Definition: decl.h:166
Definition: decl.h:123
DeclSpecs * declSpecs
Definition: decl.h:205
std::vector< std::pair< std::string, SourcePos > > declSpecList
Definition: decl.h:115
int soaWidth
Definition: decl.h:113
void InitFromDeclSpecs(DeclSpecs *ds)
Definition: decl.cpp:258
const Type * GetBaseType(SourcePos pos) const
Definition: decl.cpp:150
StorageClass storageClass
Definition: decl.h:162
Declarator(DeclaratorKind dk, SourcePos p)
Definition: decl.cpp:246
int vectorSize
Definition: decl.h:108
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:697
std::vector< Declarator * > * declarators
Definition: decl.h:217
Expr * initExpr
Definition: decl.h:172
const SourcePos pos
Definition: decl.h:146
std::string name
Definition: decl.h:169
StructDeclaration(const Type *t, std::vector< Declarator * > *d)
Definition: decl.h:213
Declaration(DeclSpecs *ds, std::vector< Declarator * > *dlist=NULL)
Definition: decl.cpp:613
void Print(int indent) const
Definition: decl.cpp:685
DeclSpecs(const Type *t=NULL, StorageClass sc=SC_NONE, int tq=TYPEQUAL_NONE)
Definition: decl.cpp:129
std::vector< Declaration * > functionParams
Definition: decl.h:180
Representation of a range of positions in a source file.
Definition: ispc.h:134
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:187
StorageClass
Definition: ispc.h:118
DeclaratorKind
Definition: decl.h:119
std::vector< VariableDeclaration > GetVariableDeclarations() const
Definition: decl.cpp:634
Representation of the declaration of a single variable.
Definition: decl.h:132
std::vector< Declarator * > declarators
Definition: decl.h:206
Definition: ispc.h:119
Declarator * child
Definition: decl.h:157
const DeclaratorKind kind
Definition: decl.h:153
Interface class that defines the type abstraction.
Definition: type.h:101
#define TYPEQUAL_NONE
Definition: decl.h:68
void Print() const
Definition: decl.cpp:231
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
void DeclareFunctions()
Definition: decl.cpp:662
void Print(int indent) const
Definition: decl.cpp:279
Main ispc.header file. Defines Target, Globals and Opt classes.
const Type * type
Definition: decl.h:216
Definition: decl.h:120
Representation of the declaration specifiers in a declaration.
Definition: decl.h:84
int typeQualifiers
Definition: decl.h:160