1// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_COMPILER_METHOD_RECOGNIZER_H_
6#define RUNTIME_VM_COMPILER_METHOD_RECOGNIZER_H_
7
8#include "vm/allocation.h"
9#include "vm/compiler/recognized_methods_list.h"
10#include "vm/growable_array.h"
11#include "vm/token.h"
12
13namespace dart {
14
15// Forward declarations.
16class Function;
17class Library;
18class Object;
19class String;
20class Zone;
21
22// Class that recognizes the name and owner of a function and returns the
23// corresponding enum. See RECOGNIZED_LIST above for list of recognizable
24// functions.
25class MethodRecognizer : public AllStatic {
26 public:
27 enum Kind {
28 kUnknown,
29#define DEFINE_ENUM_LIST(class_name, function_name, enum_name, fp) k##enum_name,
30 RECOGNIZED_LIST(DEFINE_ENUM_LIST)
31#undef DEFINE_ENUM_LIST
32 kNumRecognizedMethods
33 };
34
35 static intptr_t NumArgsCheckedForStaticCall(const Function& function);
36
37 // Try to find an annotation of the form
38 // @pragma("vm:exact-result-type", int)
39 // @pragma("vm:exact-result-type", "dart:core#_Smi")
40 // and return the exact cid if found or kDynamicCid otherwise.
41 //
42 // See [result_type_pragma.md].
43 static intptr_t ResultCidFromPragma(const Object& function_or_field);
44
45 // Try to find an annotation of the form
46 // @pragma("vm:non-nullable-result-type")
47 // and returns true iff `false` was specified in the annotation.
48 //
49 // See [pragmas.md].
50 static bool HasNonNullableResultTypeFromPragma(
51 const Object& function_or_field);
52
53 static intptr_t MethodKindToReceiverCid(Kind kind);
54 static const char* KindToCString(Kind kind);
55
56 static void InitializeState();
57
58 private:
59 static void Libraries(GrowableArray<Library*>* libs);
60};
61
62// Recognizes token corresponding to a method name.
63class MethodTokenRecognizer : public AllStatic {
64 public:
65 static Token::Kind RecognizeTokenKind(const String& name);
66};
67
68// Class that recognizes factories and returns corresponding result cid.
69class FactoryRecognizer : public AllStatic {
70 public:
71 // Return result cid of 'factory' if it is recognized.
72 // Return kDynamicCid if factory is not recognized.
73 static intptr_t ResultCid(const Function& factory);
74
75 // Return result cid of 'function' called with 'argument_count' arguments,
76 // if function is a recognized list factory constructor.
77 // Return kDynamicCid if function is not recognized.
78 static intptr_t GetResultCidOfListFactory(Zone* zone,
79 const Function& function,
80 intptr_t argument_count);
81};
82
83} // namespace dart
84
85#endif // RUNTIME_VM_COMPILER_METHOD_RECOGNIZER_H_
86