File: Compiler.h

package info (click to toggle)
guavac 1.2-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,728 kB
  • ctags: 2,387
  • sloc: cpp: 20,367; yacc: 1,664; makefile: 504; lex: 348; ansic: 286; sh: 263
file content (299 lines) | stat: -rw-r--r-- 12,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (c) 1995  David Engberg  All rights reserved
// $Id: Compiler.h,v 1.20 1998/05/23 18:51:15 geppetto Exp $
#ifndef _Compiler_h
#define _Compiler_h
#pragma interface

#include "JavaFieldSignature.h"
#include "JavaMethodSignature.h"
#include "unicode_string.h"
#include "parser_decls.h"
#include "JavaDirectory.h"
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <list>
class CJavaMethodInfo;
class CJavaClassFile;
class CJavaAccessFlags;
class CIntermediateClass;
class CIntermediateFunction;
class CCompoundStatement;
class CDeclarationStatement;
class COuterLocalExpression;
class CStatement;
class CCompileError;
class CJavaFieldInfo;
class CLocalVariableRecord;
class ostream;


//
//  Class name : CCompiler
//  Description : This class is used to encapsulate the operations used in
//    compiling a Java source file.  This manages all of the table-driven
//    parser code produced by flex and bison.
//    To use this interface, call the public static member function
//    'CompileValue' which takes in a string and parses it into a list of
//    CJavaClassFiles, if possible.
//
class CCompiler {
public:
  typedef vector<CJavaClassFile*> ClassList;
  static bool CompileFile(const string& fileName,
			  const deque<string>& classPath,
			  ClassList& resultClasses,
			  deque<string>& dependencies);

  const CJavaClassFile* ImportClass(const unicode_string& className,
				    bool aliasBase = true);
  bool ImportPackage(const unicode_string& packageName);
  void SetPackage(const unicode_string& packageName);
  void NoPackage();

  void AddClassImport(const unicode_string& className);
  void AddPackageImport(const unicode_string& package);
  void StartClass(const unicode_string& className,
		  CJavaAccessFlags* adoptModifiers = 0,
		  unicode_string* adoptExtends = 0,
		  deque<unicode_string>* adoptInterfaces = 0,
                  bool deprecated = false);
  void StartInterface(const unicode_string& className,
		      CJavaAccessFlags* adoptModifiers = 0,
		      deque<unicode_string>* adoptInterfaceExtends = 0,
		      bool deprecated = false);
  unicode_string StartAnonymousClass(unicode_string* adoptParentName);
			   
  void EndClass();
  void StartFunction(const CJavaTypeSignature& returnType,
		     const unicode_string& functionName,
		     const CJavaAccessFlags& modifiers,
		     deque<CVariableDeclaration>* adoptParameters = 0,
		     deque<unicode_string>* adoptThrows = 0,
		     bool deprecated = false);
  void StartConstructor(const CJavaAccessFlags& modifiers,
			deque<CVariableDeclaration>* adoptParameters = 0,
			deque<unicode_string>* adoptThrows = 0,
			bool deprecated = false);
  void EndFunction(CCompoundStatement* adoptStatementBlock = 0);
  bool InStatementBlock() const;
  void PushLocalScope();
  void PopLocalScope();
  unsigned short AddLocalVariable(const CVariableDeclaration& signature);
  typedef unsigned short LocalVariableIndex;
  const CLocalVariableRecord*
    LookupLocalVariable(const unicode_string& name) const;
  const COuterLocalExpression*
    LookupOuterLocalVariable(const unicode_string& name) const;
  unsigned short AddUnnamedVariable(unsigned long width = 1);
  void AddField(CDeclarationStatement* adoptDeclaration);
  bool FieldExists(const unicode_string& name);
  void AddStaticCode(CCompoundStatement* adoptStatement);
  void ParseError(unsigned long lineNumber, const string& errorMessage,
		  const string& input);
  void ParseWarning(unsigned long lineNumber, const string& errorMessage,
		    const string& input);

  CIntermediateClass* GetCurrentClass();
  const CIntermediateClass* GetCurrentClass() const;

  const CJavaClassFile* LookupClass(const unicode_string& name,
		    const CIntermediateClass* classContext = 0,
		    const CIntermediateFunction* methodContext = 0) const;
  const CJavaClassFile* LookupClass(const unicode_string& name,
				    const CCompileContext& context) const;

  bool ValidClass(const unicode_string& name) const;
  bool ValidParent(const CJavaClassFile& child,
		   const CJavaClassFile& parent) const;
  bool ValidInterface(const CJavaClassFile& child,
		      const CJavaClassFile& interface) const;
  bool DescendsFrom(const CJavaClassFile& child,
		    const CJavaClassFile& ancestor) const;
  bool AssignableSubtype(const CJavaTypeSignature& childType,
			 const CJavaTypeSignature& ancestorType) const;
  bool CastableType(const CJavaTypeSignature& currentType,
		    const CJavaTypeSignature& toType) const;
  bool SameType(const CJavaTypeSignature& first,
		const CJavaTypeSignature& second) const;
  bool SameType(const CJavaMethodSignature& first,
		const CJavaMethodSignature& second) const;
  bool ImplicitCastTo(const CJavaTypeSignature& from,
		      const CJavaTypeSignature& to) const;
  bool SameField(const CJavaFieldSignature& first,
		 const CJavaFieldSignature& second) const;
  CCompileError* CheckType(const CJavaTypeSignature& type) const;
  CJavaTypeSignature FixType(const CJavaTypeSignature& oldType,
		    const CIntermediateClass* classContext = 0,
		    const CIntermediateFunction* methodContext = 0) const;
  CJavaTypeSignature FixType(const CJavaTypeSignature& oldType,
			     const CCompileContext& context) const;
  unicode_string NameClassConstant(const CJavaTypeSignature& type) const;
  const CJavaClassFile* FindField(const unicode_string& field,
				  const CJavaClassFile& fromClass,
				  CJavaFieldInfo& setFieldInfo) const;
  CCompileError* CheckValidOverride(const CJavaClassFile& parentClass,
				    const CIntermediateFunction& method);
  CCompileError* FindConstantField(CExpression*& result,
      const unicode_string& className, const CJavaFieldSignature& field,
      const CCompileContext& context, unsigned long lineNumber) const;
  CCompileError* FindIntermediateConstantField(CExpression*& result,
				      const CIntermediateClass& intermediate,
				      const CJavaFieldInfo& fieldInfo) const;
  static CExpression* CreateLiteralFromConstant(unsigned short index,
		const CJavaClassFile& onClass, const CJavaTypeSignature& type);
  static unsigned short CreateConstantFromLiteral(const CExpression* literal,
						  CJavaClassFile& onClass);
  pair<const CJavaClassFile*, const CJavaMethodInfo*>*
    MatchMethod(const unicode_string& methodName,
  		const CJavaClassFile& fromClass,
		const deque<CJavaTypeSignature>& arguments,
		string*& errorString);
  pair<const CJavaClassFile*, const CJavaMethodInfo*>*
    MatchConstructor(const CJavaClassFile& fromClass,
		     const deque<CJavaTypeSignature>& arguments,
		     ExpressionList::const_iterator& visibleBegin,
		     ExpressionList::const_iterator& visibleEnd,
		     string*& errorString);
  const CJavaMethodInfo*
    MatchClassMethod(const CJavaClassFile& onClass,
		     const unicode_string& name,
		     const deque<CJavaTypeSignature>& arguments,
		     string*& errorString) const;
  pair<const CJavaClassFile*, const CJavaMethodInfo*>*
    ExactMatchMethod(const CJavaMethodSignature& method,
		     const CJavaClassFile& fromClass);
  const CJavaMethodInfo*
    ExactMatchClassMethod(const CJavaMethodSignature& method,
			  const CJavaClassFile& onClass) const;
  bool MoreSpecific(const CJavaMethodSignature& method1,
		    const CJavaMethodSignature& method2) const;

  typedef list<CJavaMethodSignature> MethodList;
  unsigned long UnimplementedMethods(const CJavaClassFile& onClass,
				     MethodList& methods);
  unsigned long UndeclaredInterfaceMethods(const CJavaClassFile& onClass,
					   MethodList& methods);

  bool IsThrowable(const CJavaTypeSignature& type) const;
  bool InClassInitializers() const { return fInClassInitializers; }

  void AddStaticClassMethod();
  void AddStaticClassField(const unicode_string& fieldName,
			   CJavaClassFile& classFile);

  typedef vector<unsigned long> FinallyHandlerStack;
  void PushFinallyHandler(unsigned long instruction);
  unsigned long PopFinallyHandler();
  FinallyHandlerStack::iterator GetHandlerBegin();
  FinallyHandlerStack::iterator GetHandlerEnd();
  
  void PushStatementContext(CStatement* statement);
  void PopStatementContext();
  StatementList::iterator GetContextBegin();
  StatementList::iterator GetContextEnd();

  void GenerateSyntheticConstructorArguments(const unicode_string& className,
					     ExpressionList& into) const;

  void WarnDeprecatedField(const CJavaFieldInfo& field,
			   const CJavaClassFile& fieldClass,
			   const CCompileContext& context,
			   unsigned long line = 0) const;
  void WarnDeprecatedMethod(const CJavaMethodInfo& method,
			   const CJavaClassFile& methodClass,
			   const CCompileContext& context,
			   unsigned long line = 0) const;
  void WarnDeprecatedClass(const CJavaClassFile& usedClass) const;

  static const unsigned long kVersionID;
  static const unicode_string kConstructorName;
  static const unicode_string kStaticName;
  static const unicode_string kObjectName;
  static const unicode_string kClassName;
  static const unicode_string kThrowableName;
  static const unicode_string kErrorName;
  static const unicode_string kRuntimeExceptionName;
  static const unicode_string kClassNotFoundExceptionName;
  static const unicode_string kNoClassDefFoundErrorName;

  static const unicode_string kSyntheticDivider;
  static const unicode_string kOuterThisName;
  static const unicode_string kSyntheticFieldPrefix;
  static const unicode_string kSyntheticClassPrefix;

  static const string kDefaultImport;
protected:
  CCompiler();
  CCompiler(const deque<string>& classPath);
  ~CCompiler();

  bool CheckJavaLangObject();
  bool ParsedFile(const string& fileName) const;
  bool ParseFile(const string& fileName);
  bool CheckImports(const string& fileName);
  typedef deque<CIntermediateClass*> IntermediateList;
  bool PartialCompileFile(const string& fileName,
			  IntermediateList& resultClasses);

  typedef list<CJavaDirectory> ClassPathList;
  CJavaClassFile* ImportOneClassPath(const unicode_string& className,
				     const CJavaDirectory& classPath);

  void PrepareIntermediateClass(CIntermediateClass& intermediate,
				const unicode_string& fileName);
  void RemoveIntermediateClass(const CIntermediateClass* intermediate);
  bool PrepareClassDeclarations(CIntermediateClass& intermediate);
  void AddSyntheticConstructorParams(CIntermediateClass& inClass,
				     CIntermediateFunction& intermediate);
  bool GenerateClass(CIntermediateClass& intermediate);
  CCompileError* GenerateCode(CIntermediateClass& intermediate,
			      CIntermediateFunction& intermediateMethod,
			      CJavaMethodInfo& realMethod);
  bool GenerateFieldConstants(CIntermediateClass& intermediate);
  CCompileError* GenerateMethod(CIntermediateClass& intermediateClass,
				CIntermediateFunction& intermediateMethod);
  void GenerateSyntheticCode(CCompileContext& context,
			     CCodeSequence& code);
  void FixAnonymousSyntheticConstructor(CIntermediateClass& intermediateClass,
					CIntermediateFunction& constructor,
					const CJavaMethodInfo& parentInfo);

  const CIntermediateClass*
                LookupIntermediateClass(const unicode_string& name) const;
  const CJavaClassFile* LookupInnerClass(const unicode_string& name,
			 const CIntermediateClass* classContext,
			 const CIntermediateFunction* methodContext) const;

  void PrintCompileError(const string& fileName, const string& message,
			 unsigned long line = 0) const;
  void PrintCompileError(const string& fileName, const unicode_string& message,
			 unsigned long line = 0) const;
			 

  string fFileName;
  deque<string> fParsedFiles;
  ClassPathList fClassPath;
  ClassPathList fImportedPackages;
  ClassList fImportedClasses;
  typedef map<unicode_string, const CJavaClassFile*, less<unicode_string> >
     ImportTable;
  ImportTable fImportAliases;
  unicode_string fPackageName;
  IntermediateList fIntermediateClasses;
  bool fParseError;
  deque<string> fDependencies;
  deque<unicode_string> fClassImports;
  vector<unsigned long> fClassImportLocations;
  deque<unicode_string> fPackageImports;
  vector<unsigned long> fPackageImportLocations;
  bool fInClassInitializers;
  StatementList fStatementContexts;
  FinallyHandlerStack fFinallyHandlers;

  deque<CIntermediateClass*> fParsingClasses;

};

#endif