Computer Laboratory

Parser.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2012-2013 Jonathan Anderson
4  * All rights reserved.
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef PARSER_H
33 #define PARSER_H
34 
35 #include <tesla.pb.h>
36 
37 #include <llvm/ADT/APInt.h>
38 #include <llvm/ADT/OwningPtr.h>
39 
40 #include <string>
41 #include <vector>
42 
43 namespace clang {
44  class ASTContext;
45  class BinaryOperator;
46  class CallExpr;
47  class CompoundStmt;
48  class Decl;
49  class DeclRefExpr;
50  class Expr;
51  class FunctionDecl;
52  class MemberExpr;
53  class SourceLocation;
54  class SourceRange;
55  class Stmt;
56  class UnaryOperator;
57  class ValueDecl;
58 }
59 
60 namespace llvm {
61  class APSInt;
62 }
63 
64 
65 namespace tesla {
66 
68 class Parser {
69 public:
71  typedef std::vector<const clang::ValueDecl*> RefVector;
72 
74  static Parser* AssertionParser(clang::CallExpr*, clang::ASTContext&);
75 
77  static Parser* AutomatonParser(clang::FunctionDecl*, clang::ASTContext&);
78 
80  static Parser* MappingParser(clang::FunctionDecl*, clang::ASTContext&);
81 
90  bool Parse(llvm::OwningPtr<AutomatonDescription>& Descrip,
91  llvm::OwningPtr<Usage>& Usage);
92 
93 
94 private:
95  class Flags {
96  public:
98  FunctionEvent::CallContext FnInstrContext;
99 
101  BooleanExpr::Operation OrOperator;
102 
107  bool StrictMode;
108  };
109 
110  Parser(clang::ASTContext& Ctx, Identifier ID = Identifier(),
112  clang::Expr* Begin = NULL, clang::Expr* End = NULL,
113  clang::Stmt *Root = NULL, Flags InitialFlags = Flags())
114  : Ctx(Ctx), ID(ID), TeslaContext(C), Beginning(Begin), End(End),
115  Root(Root), RootFlags(InitialFlags)
116  {
117  }
118 
119  bool Parse(Location*, clang::Expr*, clang::Expr*, clang::Expr*);
120  bool Parse(AutomatonDescription::Context*, clang::Expr*);
121 
122  bool Parse(Expression*, const clang::CompoundStmt*, Flags);
123  bool Parse(Expression*, const clang::Expr*, Flags);
124  bool Parse(Expression*, const clang::BinaryOperator*, Flags);
125  bool Parse(Expression*, const clang::CallExpr*, Flags);
126  bool Parse(Expression*, const clang::DeclRefExpr*, Flags);
127  bool Parse(Expression*, const clang::UnaryOperator*, Flags);
128 
129  bool Parse(FunctionRef*, const clang::FunctionDecl*, Flags);
130  bool Parse(Argument*, const clang::Expr*, Flags);
131  bool Parse(Argument*, const clang::ValueDecl*, bool AllowAny, Flags);
132 
133  bool ParseStructField(StructField*, const clang::MemberExpr*, Flags);
134  bool ParseSubAutomaton(Expression*, const clang::CallExpr*, Flags);
135  bool ParsePredicate(Expression*, const clang::CallExpr*, Flags);
136 
137  // TESLA predicates:
139  typedef bool (Parser::*CallParser)(Expression*, const clang::CallExpr*,
140  Flags);
141 
142  bool ParseFunctionCall(Expression*, const clang::CallExpr*, Flags);
143  bool ParseFunctionReturn(Expression*, const clang::CallExpr*, Flags);
144  bool ParseCallee(Expression*, const clang::CallExpr*, Flags);
145  bool ParseCaller(Expression*, const clang::CallExpr*, Flags);
146  bool ParseStrictMode(Expression*, const clang::CallExpr*, Flags);
147  bool ParseConditional(Expression*, const clang::CallExpr*, Flags);
148  bool ParseOptional(Expression*, const clang::CallExpr*, Flags);
149  bool ParseSequence(Expression*, const clang::CallExpr*, Flags);
150 
152  bool ParseFunctionPredicate(FunctionEvent*, const clang::CallExpr*,
153  bool ParseRetVal, Flags);
154 
156  bool ParseFunctionCall(Expression*, const clang::BinaryOperator*, Flags);
157 
159  bool ParseFieldAssign(Expression*, const clang::BinaryOperator*, Flags);
160 
161 
163  bool CheckIgnore(const clang::Expr*);
164 
166  bool CheckAssignmentKind(const clang::ValueDecl*, const clang::Expr*);
167 
169  std::string ParseStringLiteral(const clang::Expr*);
170 
172  llvm::APInt ParseIntegerLiteral(const clang::Expr*);
173 
174 
176  size_t ReferenceIndex(const clang::ValueDecl*);
177 
178 
180  void ReportError(llvm::StringRef Message, const clang::Decl*);
181  void ReportError(llvm::StringRef Message, const clang::Stmt*);
182  void ReportError(llvm::StringRef Message, const clang::SourceLocation&,
183  const clang::SourceRange&);
184 
185 
186 
187  clang::ASTContext& Ctx;
188 
189  const Identifier ID;
190  const AutomatonDescription::Context TeslaContext;
191  const clang::Expr *Beginning;
192  const clang::Expr *End;
193  const clang::Stmt *Root;
194  const Flags RootFlags;
195 
196  std::map<const clang::ValueDecl*, const clang::Expr*> FieldAssignments;
197  RefVector References;
198 };
199 
200 }
201 
202 #endif // PARSERS_H
203