73 | 73 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
74 | 74 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
75 | 75 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
76 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
76 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
77 | 77 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
78 | 78 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
79 | 79 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
80 | 80 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
81 | 81 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
82 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
83 | 82 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
84 | 83 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
85 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
86 | 84 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
87 | 85 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
88 | 86 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1534 | 1532 | function, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1535 | 1533 | pointer, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1536 | 1534 | structure, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1537 | packed structure, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1538 | 1535 | vector, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1539 | 1536 | opaque. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1540 | 1537 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1702 | 1699 | possible to have a two dimensional array, using an array as the element type | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1703 | 1700 | of another array. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1704 | 1701 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1705 | ⏎ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1702 | ⏎ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1703 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1704 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1706 | 1705 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1707 | 1706 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1708 | 1707 | Aggregate Types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1841 | 1840 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1842 | 1841 |
Overview: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1843 | 1842 |
The structure type is used to represent a collection of data members together |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1844 | in memory. The packing of the field types is defined to match the ABI of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1845 | underlying processor. The elements of a structure may be any type that has a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1846 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1843 | in memory. The elements of a structure may be any type that has a size.⏎ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1847 | 1844 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1848 | 1845 |
Structures in memory are accessed using 'load' |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1849 | 1846 | and 'store' by getting a pointer to a field | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1851 | 1848 | Structures in registers are accessed using the | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1852 | 1849 | 'extractvalue' and | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1853 | 1850 | 'insertvalue' instructions. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1854 |
Syntax: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1855 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1856 | { <type list> } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1857 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1858 | ⏎ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1851 | ⏎ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1852 |
Structures may optionally be "packed" structures, which indicate that the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1853 | alignment of the struct is one byte, and that there is no padding between | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1854 | the elements. In non-packed structs, padding between field types is defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1855 | by the target data string to match the underlying processor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1856 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1857 |
Structures can either be "anonymous" or "named". An anonymous structure is |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1858 | defined inline with other types (e.g. {i32, i32}*) and a named types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1859 | are always defined at the top level with a name. Anonmyous types are uniqued | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1860 | by their contents and can never be recursive since there is no way to write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1861 | one. Named types can be recursive. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1862 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1863 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1864 |
Syntax: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1865 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1866 | %T1 = type { <type list> } ; Named normal struct type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1867 | %T2 = type <{ <type list> }> ; Named packed struct type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1868 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1869 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1859 | 1870 |
Examples: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1860 | 1871 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1872 | 1888 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1873 | 1889 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1874 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1875 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1876 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1877 | Packed Structure Type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1878 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1879 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1880 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1881 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1882 |
Overview: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1883 |
The packed structure type is used to represent a collection of data members |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1884 | together in memory. There is no padding between fields. Further, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1885 | alignment of a packed structure is 1 byte. The elements of a packed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1886 | structure may be any type that has a size. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1887 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1888 |
Structures are accessed using 'load and |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1889 | 'store' by getting a pointer to a field with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1890 | the 'getelementptr' instruction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1891 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1892 |
Syntax: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1893 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1894 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1890 | ⏎ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1891 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1892 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1893 | Opaque Type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1894 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1895 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1896 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1897 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1898 |
Overview: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1899 |
Opaque types are used to represent named structure types that do not have a |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1900 | body specified. This corresponds (for example) to the C notion of a forward | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1901 | declared structure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1902 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1903 |
Syntax: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1904 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1905 | %X = type opaque | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1906 | %52 = type opaque | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1895 | 1907 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1896 | 1908 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1897 | 1909 |
Examples: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1898 | 1910 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1911 | 1916 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1912 | 1917 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1918 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1919 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1913 | 1920 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1914 | 1921 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1915 | 1922 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1995 | 2002 | Vector of 2 64-bit integer values. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1996 | 2003 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1997 | 2004 |
Overview:
Opaque types are used to represent unknown types in the system. This
Syntax:
Examples:
2021 |
2022 |
| opaque |
2023 |
| An opaque type. |
2024 |
| |
||||
2025 |
Overview:
An "up reference" allows you to refer to a lexically enclosing type without
An up reference is needed by the asmprinter for printing out cyclic types
Syntax:
The level is the count of the lexical type that is being referred to.
Examples:
2066 |
2067 |
| \1* |
2068 |
| Self-referential pointer. |
2069 |
| |
|||||||
2070 |
2071 |
| { { \3*, i8 }, i32 } |
2072 |
| Recursive structure where the upref refers to the out-most
|
2073 |
structure. |
2074 |
| |
|||||
2075 |
159 | 159 | |
160 | 160 |
|
161 | 161 |
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 | ||
169 | ||
170 |
|
|
162 | ⏎ | |
163 |
|
|
171 | 164 |
|
172 | 165 | |
173 | 166 | |
2644 | 2637 | LLVM system, and only need to be accessed in unusual circumstances. |
2645 | 2638 | |
2646 | 2639 | |
2640 | ||
2647 | 2641 | |
2648 | 2642 |
|
2649 | LLVM Type Resolution | |
2650 | ||
2651 | ||
2652 |
|
|
2653 | ||
2654 |
|
|
2655 | The LLVM type system has a very simple goal: allow clients to compare types for | |
2656 | structural equality with a simple pointer comparison (aka a shallow compare). | |
2657 | This goal makes clients much simpler and faster, and is used throughout the LLVM | |
2658 | system. | |
2659 | ||
2660 | ||
2661 |
|
|
2662 | Unfortunately achieving this goal is not a simple matter. In particular, | |
2663 | recursive types and late resolution of opaque types makes the situation very | |
2664 | difficult to handle. Fortunately, for the most part, our implementation makes | |
2665 | most clients able to be completely unaware of the nasty internal details. The | |
2666 | primary case where clients are exposed to the inner workings of it are when | |
2667 | building a recursive type. In addition to this case, the LLVM bitcode reader, | |
2668 | assembly parser, and linker also have to be aware of the inner workings of this | |
2669 | system. | |
2670 | ||
2671 | ||
2672 |
|
|
2673 | For our purposes below, we need three concepts. First, an "Opaque Type" is | |
2674 | exactly as defined in the language | |
2675 | reference. Second an "Abstract Type" is any type which includes an | |
2676 | opaque type as part of its type graph (for example "{ opaque, i32 }"). | |
2677 | Third, a concrete type is a type that is not an abstract type (e.g. "{ i32, | |
2678 | float }"). | |
2679 | ||
2680 | ||
2681 | ||
2682 |
|
|
2683 | Basic Recursive Type Construction | |
2684 | ||
2685 | ||
2686 |
|
|
2687 | ||
2688 |
|
|
2689 | Because the most common question is "how do I build a recursive type with LLVM", | |
2690 | we answer it now and explain it as we go. Here we include enough to cause this | |
2691 | to be emitted to an output .ll file: | |
2692 | ||
2693 | ||
2694 |
|
|
2695 |
|
|
2696 | %mylist = type { %mylist*, i32 } | |
2697 | ||
2698 | ||
2699 | ||
2700 |
|
|
2701 | To build this, use the following LLVM APIs: | |
2702 | ||
2703 | ||
2704 |
|
|
2705 |
|
|
2706 | // Create the initial outer struct | |
2707 | PATypeHolder StructTy = OpaqueType::get(); | |
2708 | std::vector<const Type*> Elts; | |
2709 | Elts.push_back(PointerType::getUnqual(StructTy)); | |
2710 | Elts.push_back(Type::Int32Ty); | |
2711 | StructType *NewSTy = StructType::get(Elts); | |
2712 | ||
2713 | // At this point, NewSTy = "{ opaque*, i32 }". Tell VMCore that | |
2714 | // the struct and the opaque type are actually the same. | |
2715 | cast<OpaqueType>(StructTy.get())->refineAbstractTypeTo(NewSTy); | |
2716 | ||
2717 | // NewSTy is potentially invalidated, but StructTy (a PATypeHolder) is | |
2718 | // kept up-to-date | |
2719 | NewSTy = cast<StructType>(StructTy.get()); | |
2720 | ||
2721 | // Add a name for the type to the module symbol table (optional) | |
2722 | MyModule->addTypeName("mylist", NewSTy); | |
2723 | ||
2724 | ||
2725 | ||
2726 |
|
|
2727 | This code shows the basic approach used to build recursive types: build a | |
2728 | non-recursive type using 'opaque', then use type unification to close the cycle. | |
2729 | The type unification step is performed by the | |
2730 | href="#refineAbstractTypeTo">refineAbstractTypeTo method, which is | |
2731 | described next. After that, we describe the | |
2732 | href="#PATypeHolder">PATypeHolder class. | |
2733 | ||
2734 | ||
2735 | ||
2736 | ||
2737 | ||
2738 |
|
|
2739 | The refineAbstractTypeTo method | |
2740 | ||
2741 | ||
2742 |
|
|
2743 |
|
|
2744 | The refineAbstractTypeTo method starts the type unification process. | |
2745 | While this method is actually a member of the DerivedType class, it is most | |
2746 | often used on OpaqueType instances. Type unification is actually a recursive | |
2747 | process. After unification, types can become structurally isomorphic to | |
2748 | existing types, and all duplicates are deleted (to preserve pointer equality). | |
2749 | ||
2750 | ||
2751 |
|
|
2752 | In the example above, the OpaqueType object is definitely deleted. | |
2753 | Additionally, if there is an "{ \2*, i32}" type already created in the system, | |
2754 | the pointer and struct type created are also deleted. Obviously whenever | |
2755 | a type is deleted, any "Type*" pointers in the program are invalidated. As | |
2756 | such, it is safest to avoid having any "Type*" pointers to abstract types | |
2757 | live across a call to refineAbstractTypeTo (note that non-abstract | |
2758 | types can never move or be deleted). To deal with this, the | |
2759 | href="#PATypeHolder">PATypeHolder class is used to maintain a stable | |
2760 | reference to a possibly refined type, and the | |
2761 | href="#AbstractTypeUser">AbstractTypeUser class is used to update more | |
2762 | complex datastructures. | |
2763 | ||
2764 | ||
2765 | ||
2766 | ||
2767 | ||
2768 |
|
|
2769 | The PATypeHolder Class | |
2770 | ||
2771 | ||
2772 |
|
|
2773 |
|
|
2774 | PATypeHolder is a form of a "smart pointer" for Type objects. When VMCore | |
2775 | happily goes about nuking types that become isomorphic to existing types, it | |
2776 | automatically updates all PATypeHolder objects to point to the new type. In the | |
2777 | example above, this allows the code to maintain a pointer to the resultant | |
2778 | resolved recursive type, even though the Type*'s are potentially invalidated. | |
2779 | ||
2780 | ||
2781 |
|
|
2782 | PATypeHolder is an extremely light-weight object that uses a lazy union-find | |
2783 | implementation to update pointers. For example the pointer from a Value to its | |
2784 | Type is maintained by PATypeHolder objects. | |
2785 | ||
2786 | ||
2787 | ||
2788 | ||
2789 | ||
2790 |
|
|
2791 | The AbstractTypeUser Class | |
2792 | ||
2793 | ||
2794 |
|
|
2795 | ||
2796 |
|
|
2797 | Some data structures need more to perform more complex updates when types get | |
2798 | resolved. To support this, a class can derive from the AbstractTypeUser class. | |
2799 | This class | |
2800 | allows it to get callbacks when certain types are resolved. To register to get | |
2801 | callbacks for a particular type, the DerivedType::{add/remove}AbstractTypeUser | |
2802 | methods can be called on a type. Note that these methods only work for | |
2803 | abstract types. Concrete types (those that do not include any opaque | |
2804 | objects) can never be refined. | |
2805 | ||
2806 | ||
2807 | ||
2808 | ||
2809 | ||
2810 | ||
2811 |
|
|
2812 | The ValueSymbolTable and | |
2813 |
|
|
2643 | The ValueSymbolTable class⏎ | |
2814 | 2644 | |
2815 | 2645 | |
2816 | 2646 |
|
2819 | 2649 | href="#Function">Function and |
2820 | 2650 | Module classes use for naming value definitions. The symbol table |
2821 | 2651 | can provide a name for any Value. |
2822 | The | |
2823 | TypeSymbolTable class is used by the Module class to store | |
2824 |
|
|
2652 | ⏎ | |
2825 | 2653 | |
2826 | 2654 |
Note that the SymbolTable class should not be directly accessed |
2827 | 2655 | by most clients. It should only be used when iteration over the symbol table |
2831 | 2659 | an empty name) do not exist in the symbol table. |
2832 | 2660 | |
2833 | 2661 | |
2834 |
|
|
2662 |
Symbol tables support iteration over the values in the symbol⏎ |
|
2835 | 2663 | table with begin/end/iterator and supports querying to see if a |
2836 | 2664 | specific name is in the symbol table (with lookup). The |
2837 | 2665 | ValueSymbolTable class exposes no public mutator methods, instead, |
2838 | 2666 | simply call setName on a value, which will autoinsert it into the |
2839 | appropriate symbol table. For types, use the Module::addTypeName method to | |
2840 |
|
|
2667 | appropriate symbol table.⏎ | |
2841 | 2668 | |
2842 | 2669 | |
2843 | 2670 | |
3126 | 2953 | |
3127 | 2954 |
|
3128 | 2955 | floating point types. |
3129 | ||
3130 |
|
|
3131 | an OpaqueType anywhere in its definition). | |
3132 | 2956 | |
3133 | 2957 |
|
3134 | 2958 | that don't have a size are abstract types, labels and void. |
3191 | 3015 | number of formal parameters. |
3192 | 3016 | |
3193 | 3017 | |
3194 |
|
|
3195 |
|
|
3196 | defines no content and is used as a placeholder for some other type. Note | |
3197 | that OpaqueType is used (temporarily) during type resolution for forward | |
3198 | references of types. Once the referenced type is resolved, the OpaqueType | |
3199 | is replaced with the actual type. OpaqueType can also be used for data | |
3200 | abstraction. At link time opaque types can be resolved to actual types | |
3201 | of the same name. | |
3202 | 3018 | |
3203 | 3019 | |
3204 | 3020 |
None | //===-- llvm/AbstractTypeUser.h - AbstractTypeUser Interface ----*- C++ -*-===// | |
1 | // | |
2 | // The LLVM Compiler Infrastructure | |
3 | // | |
4 | // This file is distributed under the University of Illinois Open Source | |
5 | // License. See LICENSE.TXT for details. | |
6 | // | |
7 | //===----------------------------------------------------------------------===// | |
8 | // | |
9 | // This file declares the AbstractTypeUser class. | |
10 | // | |
11 | //===----------------------------------------------------------------------===// | |
12 | ||
13 | #ifndef LLVM_ABSTRACT_TYPE_USER_H | |
14 | #define LLVM_ABSTRACT_TYPE_USER_H | |
15 | ||
16 | #if !defined(LLVM_TYPE_H) && !defined(LLVM_VALUE_H) | |
17 | #error Do not include this file directly. Include Type.h instead. | |
18 | #error Some versions of GCC (e.g. 3.4 and 4.1) can not handle the inlined method | |
19 | #error PATypeHolder::dropRef() correctly otherwise. | |
20 | #endif | |
21 | ||
22 |
// This is the "master" include for |
|
23 |
// it must always include |
|
24 | // llvm/AbstractTypeUser.h | |
25 | // | |
26 | // In this way, most every LLVM source file will have access to the assert() | |
27 |
// macro without having to #include |
|
28 | // | |
29 |
#include |
|
30 | ||
31 | namespace llvm { | |
32 | ||
33 | class Value; | |
34 | class Type; | |
35 | class DerivedType; | |
36 |
template |
|
37 | ||
38 | /// The AbstractTypeUser class is an interface to be implemented by classes who | |
39 | /// could possibly use an abstract type. Abstract types are denoted by the | |
40 | /// isAbstract flag set to true in the Type class. These are classes that | |
41 | /// contain an Opaque type in their structure somewhere. | |
42 | /// | |
43 | /// Classes must implement this interface so that they may be notified when an | |
44 | /// abstract type is resolved. Abstract types may be resolved into more | |
45 | /// concrete types through: linking, parsing, and bitcode reading. When this | |
46 | /// happens, all of the users of the type must be updated to reference the new, | |
47 | /// more concrete type. They are notified through the AbstractTypeUser | |
48 | /// interface. | |
49 | /// | |
50 | /// In addition to this, AbstractTypeUsers must keep the use list of the | |
51 | /// potentially abstract type that they reference up-to-date. To do this in a | |
52 | /// nice, transparent way, the PATypeHandle class is used to hold "Potentially | |
53 | /// Abstract Types", and keep the use list of the abstract types up-to-date. | |
54 | /// @brief LLVM Abstract Type User Representation | |
55 | class AbstractTypeUser { | |
56 | protected: | |
57 | virtual ~AbstractTypeUser(); // Derive from me | |
58 | ||
59 | /// setType - It's normally not possible to change a Value's type in place, | |
60 | /// but an AbstractTypeUser subclass that knows what its doing can be | |
61 | /// permitted to do so with care. | |
62 | void setType(Value *V, const Type *NewTy); | |
63 | ||
64 | public: | |
65 | ||
66 | /// refineAbstractType - The callback method invoked when an abstract type is | |
67 | /// resolved to another type. An object must override this method to update | |
68 | /// its internal state to reference NewType instead of OldType. | |
69 | /// | |
70 | virtual void refineAbstractType(const DerivedType *OldTy, | |
71 | const Type *NewTy) = 0; | |
72 | ||
73 | /// The other case which AbstractTypeUsers must be aware of is when a type | |
74 | /// makes the transition from being abstract (where it has clients on its | |
75 | /// AbstractTypeUsers list) to concrete (where it does not). This method | |
76 | /// notifies ATU's when this occurs for a type. | |
77 | /// | |
78 | virtual void typeBecameConcrete(const DerivedType *AbsTy) = 0; | |
79 | ||
80 | // for debugging... | |
81 | virtual void dump() const = 0; | |
82 | }; | |
83 | ||
84 | ||
85 | /// PATypeHandle - Handle to a Type subclass. This class is used to keep the | |
86 | /// use list of abstract types up-to-date. | |
87 | /// | |
88 | class PATypeHandle { | |
89 | const Type *Ty; | |
90 | AbstractTypeUser * const User; | |
91 | ||
92 | // These functions are defined at the bottom of Type.h. See the comment there | |
93 | // for justification. | |
94 | void addUser(); | |
95 | void removeUser(); | |
96 | public: | |
97 | // ctor - Add use to type if abstract. Note that Ty must not be null | |
98 | inline PATypeHandle(const Type *ty, AbstractTypeUser *user) | |
99 | : Ty(ty), User(user) { | |
100 | addUser(); | |
101 | } | |
102 | ||
103 | // ctor - Add use to type if abstract. | |
104 | inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) { | |
105 | addUser(); | |
106 | } | |
107 | ||
108 | // dtor - Remove reference to type... | |
109 | inline ~PATypeHandle() { removeUser(); } | |
110 | ||
111 | // Automatic casting operator so that the handle may be used naturally | |
112 |
inline operator Type *() const { return const_cast |
|
113 |
inline Type *get() const { return const_cast |
|
114 | ||
115 | // operator= - Allow assignment to handle | |
116 | inline Type *operator=(const Type *ty) { | |
117 | if (Ty != ty) { // Ensure we don't accidentally drop last ref to Ty | |
118 | removeUser(); | |
119 | Ty = ty; | |
120 | addUser(); | |
121 | } | |
122 | return get(); | |
123 | } | |
124 | ||
125 | // operator= - Allow assignment to handle | |
126 | inline const Type *operator=(const PATypeHandle &T) { | |
127 | return operator=(T.Ty); | |
128 | } | |
129 | ||
130 | inline bool operator==(const Type *ty) { | |
131 | return Ty == ty; | |
132 | } | |
133 | ||
134 | // operator-> - Allow user to dereference handle naturally... | |
135 | inline const Type *operator->() const { return Ty; } | |
136 | }; | |
137 | ||
138 | ||
139 | /// PATypeHolder - Holder class for a potentially abstract type. This uses | |
140 | /// efficient union-find techniques to handle dynamic type resolution. Unless | |
141 | /// you need to do custom processing when types are resolved, you should always | |
142 | /// use PATypeHolders in preference to PATypeHandles. | |
143 | /// | |
144 | class PATypeHolder { | |
145 | mutable const Type *Ty; | |
146 | void destroy(); | |
147 | public: | |
148 | PATypeHolder() : Ty(0) {} | |
149 | PATypeHolder(const Type *ty) : Ty(ty) { | |
150 | addRef(); | |
151 | } | |
152 | PATypeHolder(const PATypeHolder &T) : Ty(T.Ty) { | |
153 | addRef(); | |
154 | } | |
155 | ||
156 | ~PATypeHolder() { dropRef(); } | |
157 | ||
158 | operator Type *() const { return get(); } | |
159 | Type *get() const; | |
160 | ||
161 | // operator-> - Allow user to dereference handle naturally... | |
162 | Type *operator->() const { return get(); } | |
163 | ||
164 | // operator= - Allow assignment to handle | |
165 | Type *operator=(const Type *ty) { | |
166 | if (Ty != ty) { // Don't accidentally drop last ref to Ty. | |
167 | dropRef(); | |
168 | Ty = ty; | |
169 | addRef(); | |
170 | } | |
171 | return get(); | |
172 | } | |
173 | Type *operator=(const PATypeHolder &H) { | |
174 | return operator=(H.Ty); | |
175 | } | |
176 | ||
177 | /// getRawType - This should only be used to implement the vmcore library. | |
178 | /// | |
179 | const Type *getRawType() const { return Ty; } | |
180 | ||
181 | private: | |
182 | void addRef(); | |
183 | void dropRef(); | |
184 | friend class TypeMapBase; | |
185 | }; | |
186 | ||
187 | // simplify_type - Allow clients to treat uses just like values when using | |
188 | // casting operators. | |
189 |
template<> struct simplify_type |
|
190 | typedef const Type* SimpleType; | |
191 | static SimpleType getSimplifiedValue(const PATypeHolder &Val) { | |
192 |
return static_cast |
|
193 | } | |
194 | }; | |
195 |
template<> struct simplify_type |
|
196 | typedef const Type* SimpleType; | |
197 | static SimpleType getSimplifiedValue(const PATypeHolder &Val) { | |
198 |
return static_cast |
|
199 | } | |
200 | }; | |
201 | ||
202 | } // End llvm namespace | |
203 | ||
204 | #endif |
28 | 28 | |
29 | 29 | // Module sub-block id's. |
30 | 30 | PARAMATTR_BLOCK_ID, |
31 |
|
|
31 | ⏎ | |
32 | /// TYPE_BLOCK_ID_OLD - This is the type descriptor block in LLVM 2.9 and | |
33 | /// earlier, replaced with TYPE_BLOCK_ID2. FIXME: Remove in LLVM 3.1. | |
34 | TYPE_BLOCK_ID_OLD, | |
35 | ||
32 | 36 | CONSTANTS_BLOCK_ID, |
33 | 37 | FUNCTION_BLOCK_ID, |
34 |
|
|
38 | ⏎ | |
39 | /// TYPE_SYMTAB_BLOCK_ID_OLD - This type descriptor is from LLVM 2.9 and | |
40 | /// earlier bitcode files. FIXME: Remove in LLVM 3.1 | |
41 | TYPE_SYMTAB_BLOCK_ID_OLD, | |
42 | ||
35 | 43 | VALUE_SYMTAB_BLOCK_ID, |
36 | 44 | METADATA_BLOCK_ID, |
37 |
METADATA_ATTACHMENT_ID |
|
45 | METADATA_ATTACHMENT_ID,⏎ | |
46 | ||
47 | TYPE_BLOCK_ID_NEW | |
38 | 48 | }; |
39 | 49 | |
40 | 50 | |
71 | 81 | |
72 | 82 | /// TYPE blocks have codes for each type primitive they use. |
73 | 83 | enum TypeCodes { |
74 |
TYPE_CODE_NUMENTRY = 1, |
|
84 | TYPE_CODE_NUMENTRY = 1, // NUMENTRY: [numentries]⏎ | |
75 | 85 | |
76 | 86 | // Type Codes |
77 | TYPE_CODE_VOID = 2, // VOID | |
78 | TYPE_CODE_FLOAT = 3, // FLOAT | |
79 | TYPE_CODE_DOUBLE = 4, // DOUBLE | |
80 | TYPE_CODE_LABEL = 5, // LABEL | |
81 | TYPE_CODE_OPAQUE = 6, // OPAQUE | |
82 | TYPE_CODE_INTEGER = 7, // INTEGER: [width] | |
83 | TYPE_CODE_POINTER = 8, // POINTER: [pointee type] | |
84 | TYPE_CODE_FUNCTION = 9, // FUNCTION: [vararg, retty, paramty x N] | |
85 | TYPE_CODE_STRUCT = 10, // STRUCT: [ispacked, eltty x N] | |
86 | TYPE_CODE_ARRAY = 11, // ARRAY: [numelts, eltty] | |
87 |
TYPE_CODE_V |
|
87 | TYPE_CODE_VOID = 2, // VOID⏎ | |
88 | TYPE_CODE_FLOAT = 3, // FLOAT | |
89 | TYPE_CODE_DOUBLE = 4, // DOUBLE | |
90 | TYPE_CODE_LABEL = 5, // LABEL | |
91 | TYPE_CODE_OPAQUE = 6, // OPAQUE | |
92 | TYPE_CODE_INTEGER = 7, // INTEGER: [width] | |
93 | TYPE_CODE_POINTER = 8, // POINTER: [pointee type] | |
94 | TYPE_CODE_FUNCTION = 9, // FUNCTION: [vararg, retty, paramty x N] | |
95 | ||
96 | // FIXME: This is the encoding used for structs in LLVM 2.9 and earlier. | |
97 | // REMOVE this in LLVM 3.1 | |
98 | TYPE_CODE_STRUCT_OLD = 10, // STRUCT: [ispacked, eltty x N] | |
99 | TYPE_CODE_ARRAY = 11, // ARRAY: [numelts, eltty] | |
100 | TYPE_CODE_VECTOR = 12, // VECTOR: [numelts, eltty] | |
88 | 101 | |
89 | 102 | // These are not with the other floating point types because they're |
90 | 103 | // a late addition, and putting them in the right place breaks |
91 | 104 | // binary compatibility. |
92 | TYPE_CODE_X86_FP80 = 13, // X86 LONG DOUBLE | |
93 | TYPE_CODE_FP128 = 14, // LONG DOUBLE (112 bit mantissa) | |
94 | TYPE_CODE_PPC_FP128= 15, // PPC LONG DOUBLE (2 doubles) | |
95 | ||
96 | TYPE_CODE_METADATA = 16, // METADATA | |
97 | ||
98 |
TYPE_CODE_X86_ |
|
105 | TYPE_CODE_X86_FP80 = 13, // X86 LONG DOUBLE⏎ | |
106 | TYPE_CODE_FP128 = 14, // LONG DOUBLE (112 bit mantissa) | |
107 | TYPE_CODE_PPC_FP128= 15, // PPC LONG DOUBLE (2 doubles) | |
108 | ||
109 | TYPE_CODE_METADATA = 16, // METADATA | |
110 | ||
111 | TYPE_CODE_X86_MMX = 17, // X86 MMX | |
112 | ||
113 | TYPE_CODE_STRUCT_ANON = 18, // STRUCT_ANON: [ispacked, eltty x N] | |
114 | TYPE_CODE_STRUCT_NAME = 19, // STRUCT_NAME: [strchr x N] | |
115 | TYPE_CODE_STRUCT_NAMED = 20 // STRUCT_NAMED: [ispacked, eltty x N] | |
99 | 116 | }; |
100 | 117 | |
101 | 118 | // The type symbol table only has one code (TST_ENTRY_CODE). |
911 | 911 | Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const; |
912 | 912 | |
913 | 913 | /// getWithOperands - This returns the current constant expression with the |
914 | /// operands replaced with the specified values. The specified operands must | |
915 | /// match count and type with the existing ones. | |
916 |
Constant *getWithOperands(ArrayRef |
|
917 |
|
|
914 | /// operands replaced with the specified values. The specified array must⏎ | |
915 | /// have the same number of operands as our current one. | |
916 |
Constant *getWithOperands(ArrayRef |
|
917 | return getWithOperands(Ops, getType()); | |
918 | } | |
919 | ||
920 | /// getWithOperands - This returns the current constant expression with the | |
921 | /// operands replaced with the specified values and with the specified result | |
922 | /// type. The specified array must have the same number of operands as our | |
923 | /// current one. | |
924 |
Constant *getWithOperands(ArrayRef |
|
925 | ||
918 | 926 | virtual void destroyConstant(); |
919 | 927 | virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U); |
920 | 928 |
28 | 28 | extern unsigned char CorrelatedValuePropagationID; |
29 | 29 | extern unsigned char DeadArgEliminationID; |
30 | 30 | extern unsigned char DeadStoreEliminationID; |
31 | extern unsigned char DeadTypeEliminationID; | |
32 | 31 | extern unsigned char EarlyCSEID; |
33 | 32 | extern unsigned char FunctionAttrsID; |
34 | 33 | extern unsigned char FunctionInliningID; |
23 | 23 | namespace llvm { |
24 | 24 | |
25 | 25 | class Value; |
26 |
template |
|
27 | class FunctionValType; | |
28 | class ArrayValType; | |
29 | class StructValType; | |
30 | class PointerValType; | |
31 | class VectorValType; | |
32 | class IntegerValType; | |
33 | 26 | class APInt; |
34 | 27 | class LLVMContext; |
35 | 28 |
template |
29 | class StringRef; | |
36 | 30 | |
37 | 31 | class DerivedType : public Type { |
38 | friend class Type; | |
39 | ||
40 | 32 | protected: |
41 | 33 | explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {} |
42 | ||
43 | /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type | |
44 | /// that the current type has transitioned from being abstract to being | |
45 | /// concrete. | |
46 | /// | |
47 | void notifyUsesThatTypeBecameConcrete(); | |
48 | ||
49 | /// dropAllTypeUses - When this (abstract) type is resolved to be equal to | |
50 | /// another (more concrete) type, we must eliminate all references to other | |
51 | /// types, to avoid some circular reference problems. | |
52 | /// | |
53 | void dropAllTypeUses(); | |
54 | ||
55 | public: | |
56 | ||
57 | //===--------------------------------------------------------------------===// | |
58 | // Abstract Type handling methods - These types have special lifetimes, which | |
59 | // are managed by (add|remove)AbstractTypeUser. See comments in | |
60 | // AbstractTypeUser.h for more information. | |
61 | ||
62 | /// refineAbstractTypeTo - This function is used to when it is discovered that | |
63 | /// the 'this' abstract type is actually equivalent to the NewType specified. | |
64 | /// This causes all users of 'this' to switch to reference the more concrete | |
65 | /// type NewType and for 'this' to be deleted. | |
66 | /// | |
67 | void refineAbstractTypeTo(const Type *NewType); | |
68 | ||
69 | void dump() const { Type::dump(); }⏎ | |
34 | public:⏎ | |
70 | 35 | |
71 | 36 | // Methods for support type inquiry through isa, cast, and dyn_cast. |
72 | 37 | static inline bool classof(const DerivedType *) { return true; } |
87 | 52 | DerivedType(C, IntegerTyID) { |
88 | 53 | setSubclassData(NumBits); |
89 | 54 | } |
90 |
friend class TypeMap |
|
91 | 55 | public: |
92 | 56 | /// This enum is just used to hold constants we need for IntegerType. |
93 | 57 | enum { |
102 | 66 | /// that instance will be returned. Otherwise a new one will be created. Only |
103 | 67 | /// one instance with a given NumBits value is ever created. |
104 | 68 | /// @brief Get or create an IntegerType instance. |
105 |
static |
|
69 | static IntegerType *get(LLVMContext &C, unsigned NumBits);⏎ | |
106 | 70 | |
107 | 71 | /// @brief Get the number of bits in this IntegerType |
108 | 72 | unsigned getBitWidth() const { return getSubclassData(); } |
142 | 106 | /// FunctionType - Class to represent function types |
143 | 107 | /// |
144 | 108 | class FunctionType : public DerivedType { |
145 |
friend class TypeMap |
|
146 | 109 | FunctionType(const FunctionType &); // Do not implement |
147 | 110 | const FunctionType &operator=(const FunctionType &); // Do not implement |
148 |
FunctionType(const Type *Result, ArrayRef |
|
149 |
|
|
111 |
FunctionType(const Type *Result, ArrayRef |
|
150 | 112 | |
151 | 113 | public: |
152 | 114 | /// FunctionType::get - This static method is the primary way of constructing |
154 | 116 | /// |
155 | 117 | static FunctionType *get(const Type *Result, |
156 | 118 |
ArrayRef |
119 | static FunctionType *get(const Type *Result, | |
120 |
ArrayRef |
|
157 | 121 | |
158 | 122 | /// FunctionType::get - Create a FunctionType taking no parameters. |
159 | 123 | /// |
168 | 132 | static bool isValidArgumentType(const Type *ArgTy); |
169 | 133 | |
170 | 134 | bool isVarArg() const { return getSubclassData(); } |
171 |
|
|
135 | Type *getReturnType() const { return ContainedTys[0]; }⏎ | |
172 | 136 | |
173 | 137 | typedef Type::subtype_iterator param_iterator; |
174 | 138 | param_iterator param_begin() const { return ContainedTys + 1; } |
175 | 139 | param_iterator param_end() const { return &ContainedTys[NumContainedTys]; } |
176 | 140 | |
177 | 141 | // Parameter type accessors. |
178 |
|
|
142 | Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }⏎ | |
179 | 143 | |
180 | 144 | /// getNumParams - Return the number of fixed parameters this function type |
181 | 145 | /// requires. This does not consider varargs. |
182 | 146 | /// |
183 | 147 | unsigned getNumParams() const { return NumContainedTys - 1; } |
184 | ||
185 | // Implement the AbstractTypeUser interface. | |
186 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); | |
187 | virtual void typeBecameConcrete(const DerivedType *AbsTy); | |
188 | 148 | |
189 | 149 | // Methods for support type inquiry through isa, cast, and dyn_cast. |
190 | 150 | static inline bool classof(const FunctionType *) { return true; } |
204 | 164 | /// getTypeAtIndex - Given an index value into the type, return the type of |
205 | 165 | /// the element. |
206 | 166 | /// |
207 | const Type *getTypeAtIndex(const Value *V) const; | |
208 |
|
|
167 | Type *getTypeAtIndex(const Value *V) const;⏎ | |
168 | Type *getTypeAtIndex(unsigned Idx) const; | |
209 | 169 | bool indexValid(const Value *V) const; |
210 | 170 | bool indexValid(unsigned Idx) const; |
211 | 171 | |
221 | 181 | |
222 | 182 | |
223 | 183 | /// StructType - Class to represent struct types, both normal and packed. |
184 | /// Besides being optionally packed, structs can be either "anonymous" or may | |
185 | /// have an identity. Anonymous structs are uniqued by structural equivalence, | |
186 | /// but types are each unique when created, and optionally have a name. | |
224 | 187 | /// |
225 | 188 | class StructType : public CompositeType { |
226 |
friend class TypeMap |
|
227 | 189 | StructType(const StructType &); // Do not implement |
228 | 190 | const StructType &operator=(const StructType &); // Do not implement |
229 |
StructType(LLVMContext &C, ArrayRef |
|
230 | public:⏎ | |
191 | StructType(LLVMContext &C)⏎ | |
192 | : CompositeType(C, StructTyID), SymbolTableEntry(0) {} | |
193 | enum { | |
194 | // This is the contents of the SubClassData field. | |
195 | SCDB_HasBody = 1, | |
196 | SCDB_Packed = 2, | |
197 | SCDB_IsAnonymous = 4 | |
198 | }; | |
199 | ||
200 | /// SymbolTableEntry - For a named struct that actually has a name, this is a | |
201 | /// pointer to the symbol table entry (maintained by LLVMContext) for the | |
202 | /// struct. This is null if the type is an anonymous struct or if it is | |
203 | /// | |
204 | void *SymbolTableEntry; | |
205 | public: | |
206 | /// StructType::createNamed - This creates a named struct with no body | |
207 | /// specified. If the name is empty, it creates an unnamed struct, which has | |
208 | /// a unique identity but no actual name. | |
209 | static StructType *createNamed(LLVMContext &Context, StringRef Name); | |
210 | ||
211 |
static StructType *createNamed(StringRef Name, ArrayRef |
|
212 | bool isPacked = false); | |
213 | static StructType *createNamed(LLVMContext &Context, StringRef Name, | |
214 |
ArrayRef |
|
215 | bool isPacked = false); | |
216 | static StructType *createNamed(StringRef Name, Type *elt1, ...) END_WITH_NULL; | |
217 | ||
231 | 218 | /// StructType::get - This static method is the primary way to create a |
232 | 219 | /// StructType. |
233 | 220 | /// |
221 | /// FIXME: Remove the 'const Type*' version of this when types are pervasively | |
222 | /// de-constified. | |
234 | 223 |
static StructType *get(LLVMContext &Context, ArrayRef |
224 | bool isPacked = false); | |
225 |
static StructType *get(LLVMContext &Context, ArrayRef |
|
235 | 226 | bool isPacked = false); |
236 | 227 | |
237 | 228 | /// StructType::get - Create an empty structure type. |
244 | 235 | /// element type. |
245 | 236 | static StructType *get(const Type *elt1, ...) END_WITH_NULL; |
246 | 237 | |
238 | bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; } | |
239 | ||
240 | /// isAnonymous - Return true if this type is uniqued by structural | |
241 | /// equivalence, false if it has an identity. | |
242 | bool isAnonymous() const {return (getSubclassData() & SCDB_IsAnonymous) != 0;} | |
243 | ||
244 | /// isOpaque - Return true if this is a type with an identity that has no body | |
245 | /// specified yet. These prints as 'opaque' in .ll files. | |
246 | bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; } | |
247 | ||
248 | /// hasName - Return true if this is a named struct that has a non-empty name. | |
249 | bool hasName() const { return SymbolTableEntry != 0; } | |
250 | ||
251 | /// getName - Return the name for this struct type if it has an identity. | |
252 | /// This may return an empty string for an unnamed struct type. Do not call | |
253 | /// this on an anonymous type. | |
254 | StringRef getName() const; | |
255 | ||
256 | /// setName - Change the name of this type to the specified name, or to a name | |
257 | /// with a suffix if there is a collision. Do not call this on an anonymous | |
258 | /// type. | |
259 | void setName(StringRef Name); | |
260 | ||
261 | /// setBody - Specify a body for an opaque type. | |
262 |
void setBody(ArrayRef |
|
263 | void setBody(Type *elt1, ...) END_WITH_NULL; | |
264 | ||
247 | 265 | /// isValidElementType - Return true if the specified type is valid as a |
248 | 266 | /// element type. |
249 | 267 | static bool isValidElementType(const Type *ElemTy); |
250 | ||
251 |
|
|
268 | ⏎ | |
252 | 269 | |
253 | 270 | // Iterator access to the elements. |
254 | 271 | typedef Type::subtype_iterator element_iterator; |
257 | 274 | |
258 | 275 | /// isLayoutIdentical - Return true if this is layout identical to the |
259 | 276 | /// specified struct. |
260 | bool isLayoutIdentical(const StructType *Other) const { | |
261 | return this == Other; | |
262 | } | |
263 |
|
|
277 | bool isLayoutIdentical(const StructType *Other) const; ⏎ | |
264 | 278 | |
265 | 279 | // Random access to the elements |
266 | 280 | unsigned getNumElements() const { return NumContainedTys; } |
267 |
|
|
281 | Type *getElementType(unsigned N) const {⏎ | |
268 | 282 | assert(N < NumContainedTys && "Element number out of range!"); |
269 | 283 | return ContainedTys[N]; |
270 | 284 | } |
271 | ||
272 | // Implement the AbstractTypeUser interface. | |
273 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); | |
274 | virtual void typeBecameConcrete(const DerivedType *AbsTy); | |
275 | 285 | |
276 | 286 | // Methods for support type inquiry through isa, cast, and dyn_cast. |
277 | 287 | static inline bool classof(const StructType *) { return true; } |
289 | 299 | /// components out in memory identically. |
290 | 300 | /// |
291 | 301 | class SequentialType : public CompositeType { |
292 |
|
|
302 | Type *ContainedType; ///< Storage for the single contained type.⏎ | |
293 | 303 | SequentialType(const SequentialType &); // Do not implement! |
294 | 304 | const SequentialType &operator=(const SequentialType &); // Do not implement! |
295 | 305 | |
296 | // avoiding warning: 'this' : used in base member initializer list | |
297 | SequentialType *this_() { return this; } | |
298 | 306 | protected: |
299 | SequentialType(TypeID TID, const Type *ElType) | |
300 |
|
|
307 | SequentialType(TypeID TID, Type *ElType)⏎ | |
308 | : CompositeType(ElType->getContext(), TID), ContainedType(ElType) { | |
301 | 309 | ContainedTys = &ContainedType; |
302 | 310 | NumContainedTys = 1; |
303 | 311 | } |
304 | 312 | |
305 | 313 | public: |
306 |
|
|
314 | Type *getElementType() const { return ContainedTys[0]; }⏎ | |
307 | 315 | |
308 | 316 | // Methods for support type inquiry through isa, cast, and dyn_cast. |
309 | 317 | static inline bool classof(const SequentialType *) { return true; } |
318 | 326 | /// ArrayType - Class to represent array types. |
319 | 327 | /// |
320 | 328 | class ArrayType : public SequentialType { |
321 |
friend class TypeMap |
|
322 | 329 | uint64_t NumElements; |
323 | 330 | |
324 | 331 | ArrayType(const ArrayType &); // Do not implement |
325 | 332 | const ArrayType &operator=(const ArrayType &); // Do not implement |
326 |
ArrayType( |
|
333 | ArrayType(Type *ElType, uint64_t NumEl);⏎ | |
327 | 334 | public: |
328 | 335 | /// ArrayType::get - This static method is the primary way to construct an |
329 | 336 | /// ArrayType |
336 | 343 | |
337 | 344 | uint64_t getNumElements() const { return NumElements; } |
338 | 345 | |
339 | // Implement the AbstractTypeUser interface. | |
340 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); | |
341 | virtual void typeBecameConcrete(const DerivedType *AbsTy); | |
342 | ||
343 | 346 | // Methods for support type inquiry through isa, cast, and dyn_cast. |
344 | 347 | static inline bool classof(const ArrayType *) { return true; } |
345 | 348 | static inline bool classof(const Type *T) { |
350 | 353 | /// VectorType - Class to represent vector types. |
351 | 354 | /// |
352 | 355 | class VectorType : public SequentialType { |
353 |
friend class TypeMap |
|
354 | 356 | unsigned NumElements; |
355 | 357 | |
356 | 358 | VectorType(const VectorType &); // Do not implement |
357 | 359 | const VectorType &operator=(const VectorType &); // Do not implement |
358 |
VectorType( |
|
360 | VectorType(Type *ElType, unsigned NumEl);⏎ | |
359 | 361 | public: |
360 | 362 | /// VectorType::get - This static method is the primary way to construct an |
361 | 363 | /// VectorType. |
368 | 370 | /// |
369 | 371 | static VectorType *getInteger(const VectorType *VTy) { |
370 | 372 | unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); |
371 |
|
|
373 | Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);⏎ | |
372 | 374 | return VectorType::get(EltTy, VTy->getNumElements()); |
373 | 375 | } |
374 | 376 | |
378 | 380 | /// |
379 | 381 | static VectorType *getExtendedElementVectorType(const VectorType *VTy) { |
380 | 382 | unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); |
381 |
|
|
383 | Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);⏎ | |
382 | 384 | return VectorType::get(EltTy, VTy->getNumElements()); |
383 | 385 | } |
384 | 386 | |
390 | 392 | unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); |
391 | 393 | assert((EltBits & 1) == 0 && |
392 | 394 | "Cannot truncate vector element with odd bit-width"); |
393 |
|
|
395 | Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);⏎ | |
394 | 396 | return VectorType::get(EltTy, VTy->getNumElements()); |
395 | 397 | } |
396 | 398 | |
406 | 408 | return NumElements * getElementType()->getPrimitiveSizeInBits(); |
407 | 409 | } |
408 | 410 | |
409 | // Implement the AbstractTypeUser interface. | |
410 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); | |
411 | virtual void typeBecameConcrete(const DerivedType *AbsTy); | |
412 | ||
413 | 411 | // Methods for support type inquiry through isa, cast, and dyn_cast. |
414 | 412 | static inline bool classof(const VectorType *) { return true; } |
415 | 413 | static inline bool classof(const Type *T) { |
421 | 419 | /// PointerType - Class to represent pointers. |
422 | 420 | /// |
423 | 421 | class PointerType : public SequentialType { |
424 |
friend class TypeMap |
|
425 | ||
426 | 422 | PointerType(const PointerType &); // Do not implement |
427 | 423 | const PointerType &operator=(const PointerType &); // Do not implement |
428 |
explicit PointerType( |
|
424 | explicit PointerType(Type *ElType, unsigned AddrSpace);⏎ | |
429 | 425 | public: |
430 | 426 | /// PointerType::get - This constructs a pointer to an object of the specified |
431 | 427 | /// type in a numbered address space. |
444 | 440 | /// @brief Return the address space of the Pointer type. |
445 | 441 | inline unsigned getAddressSpace() const { return getSubclassData(); } |
446 | 442 | |
447 | // Implement the AbstractTypeUser interface. | |
448 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); | |
449 | virtual void typeBecameConcrete(const DerivedType *AbsTy); | |
450 | ||
451 | 443 | // Implement support type inquiry through isa, cast, and dyn_cast. |
452 | 444 | static inline bool classof(const PointerType *) { return true; } |
453 | 445 | static inline bool classof(const Type *T) { |
455 | 447 | } |
456 | 448 | }; |
457 | 449 | |
458 | ||
459 | /// OpaqueType - Class to represent opaque types. | |
460 | /// | |
461 | class OpaqueType : public DerivedType { | |
462 | friend class LLVMContextImpl; | |
463 | OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT | |
464 | const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT | |
465 | OpaqueType(LLVMContext &C); | |
466 | public: | |
467 | /// OpaqueType::get - Static factory method for the OpaqueType class. | |
468 | /// | |
469 | static OpaqueType *get(LLVMContext &C); | |
470 | ||
471 | // Implement support for type inquiry through isa, cast, and dyn_cast. | |
472 | static inline bool classof(const OpaqueType *) { return true; } | |
473 | static inline bool classof(const Type *T) { | |
474 | return T->getTypeID() == OpaqueTyID; | |
475 | } | |
476 | }; | |
477 | ||
478 | 450 | } // End llvm namespace |
479 | 451 | |
480 | 452 | #endif |
127 | 127 | |
128 | 128 | ~Function(); |
129 | 129 | |
130 | const Type *getReturnType() const; // Return the type of the ret val | |
131 |
|
|
130 | Type *getReturnType() const; // Return the type of the ret val⏎ | |
131 | FunctionType *getFunctionType() const; // Return the FunctionType for me | |
132 | 132 | |
133 | 133 | /// getContext - Return a pointer to the LLVMContext associated with this |
134 | 134 | /// function, or NULL if this function is not bound to a context yet. |
62 | 62 | virtual void eraseFromParent(); |
63 | 63 | |
64 | 64 | /// set/getAliasee - These methods retrive and set alias target. |
65 | void setAliasee(Constant* GV); | |
66 |
|
|
65 | void setAliasee(Constant *GV);⏎ | |
66 | const Constant *getAliasee() const { | |
67 | 67 |
return cast_or_null |
68 | 68 | } |
69 |
Constant |
|
69 | Constant *getAliasee() {⏎ | |
70 | 70 |
return cast_or_null |
71 | 71 | } |
72 | 72 | /// getAliasedGlobal() - Aliasee can be either global or bitcast of |
73 | 73 | /// global. This method retrives the global for both aliasee flavours. |
74 |
const GlobalValue |
|
74 | const GlobalValue *getAliasedGlobal() const;⏎ | |
75 | 75 | |
76 | 76 | /// resolveAliasedGlobal() - This method tries to ultimately resolve the alias |
77 | 77 | /// by going through the aliasing chain and trying to find the very last |
78 | 78 | /// global. Returns NULL if a cycle was found. If stopOnWeak is false, then |
79 | 79 | /// the whole chain aliasing chain is traversed, otherwise - only strong |
80 | 80 | /// aliases. |
81 |
const GlobalValue |
|
81 | const GlobalValue *resolveAliasedGlobal(bool stopOnWeak = true) const;⏎ | |
82 | 82 | |
83 | 83 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
84 | 84 | static inline bool classof(const GlobalAlias *) { return true; } |
105 | 105 | bool use_empty_except_constants(); |
106 | 106 | |
107 | 107 | /// getType - Global values are always pointers. |
108 | inline const PointerType *getType() const { | |
109 |
|
|
108 | inline PointerType *getType() const {⏎ | |
109 |
return reinterpret_cast |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | static LinkageTypes getLinkOnceLinkage(bool ODR) { |
83 | 83 | void initializeDAHPass(PassRegistry&); |
84 | 84 | void initializeDCEPass(PassRegistry&); |
85 | 85 | void initializeDSEPass(PassRegistry&); |
86 | void initializeDTEPass(PassRegistry&); | |
87 | 86 | void initializeDeadInstEliminationPass(PassRegistry&); |
88 | 87 | void initializeDeadMachineInstructionElimPass(PassRegistry&); |
89 | 88 | void initializeDomOnlyPrinterPass(PassRegistry&); |
75 | 75 | /// getAllocatedType - Return the type that is being allocated by the |
76 | 76 | /// instruction. |
77 | 77 | /// |
78 |
|
|
78 | Type *getAllocatedType() const;⏎ | |
79 | 79 | |
80 | 80 | /// getAlignment - Return the alignment of the memory that is being allocated |
81 | 81 | /// by the instruction. |
270 | 270 | // GetElementPtrInst Class |
271 | 271 | //===----------------------------------------------------------------------===// |
272 | 272 | |
273 |
// check |
|
273 | // checkGEPType - Simple wrapper function to give a better assertion failure⏎ | |
274 | 274 | // message on bad indexes for a gep instruction. |
275 | 275 | // |
276 |
static inline const Type *check |
|
276 | static inline const Type *checkGEPType(const Type *Ty) {⏎ | |
277 | 277 | assert(Ty && "Invalid GetElementPtrInst indices for type!"); |
278 | 278 | return Ty; |
279 | 279 | } |
314 | 314 | /// pointer type. |
315 | 315 | /// |
316 | 316 |
template |
317 | static const Type *getIndexedType(const Type *Ptr, | |
318 | RandomAccessIterator IdxBegin, | |
319 | RandomAccessIterator IdxEnd, | |
320 | // This argument ensures that we | |
321 | // have an iterator we can do | |
322 | // arithmetic on in constant time | |
323 |
|
|
317 | static Type *getIndexedType(const Type *Ptr,⏎ | |
318 | RandomAccessIterator IdxBegin, | |
319 | RandomAccessIterator IdxEnd, | |
320 | // This argument ensures that we | |
321 | // have an iterator we can do | |
322 | // arithmetic on in constant time | |
323 | std::random_access_iterator_tag) { | |
324 | 324 |
unsigned NumIdx = static_cast |
325 | 325 | |
326 | 326 | if (NumIdx > 0) |
445 | 445 | /// pointer type. |
446 | 446 | /// |
447 | 447 |
template |
448 | static const Type *getIndexedType(const Type *Ptr, | |
449 | RandomAccessIterator IdxBegin, | |
450 |
|
|
448 | static Type *getIndexedType(const Type *Ptr, RandomAccessIterator IdxBegin,⏎ | |
449 | RandomAccessIterator IdxEnd) { | |
451 | 450 | return getIndexedType(Ptr, IdxBegin, IdxEnd, |
452 | 451 |
typename std::iterator_traits |
453 | 452 | iterator_category()); |
454 | 453 | } |
455 | 454 | |
456 | static const Type *getIndexedType(const Type *Ptr, | |
457 | Value* const *Idx, unsigned NumIdx); | |
458 | ||
459 | static const Type *getIndexedType(const Type *Ptr, | |
460 | Constant* const *Idx, unsigned NumIdx); | |
461 | ||
462 | static const Type *getIndexedType(const Type *Ptr, | |
463 | uint64_t const *Idx, unsigned NumIdx); | |
464 | ||
465 |
|
|
455 | // FIXME: Use ArrayRef⏎ | |
456 | static Type *getIndexedType(const Type *Ptr, | |
457 | Value* const *Idx, unsigned NumIdx); | |
458 | static Type *getIndexedType(const Type *Ptr, | |
459 | Constant* const *Idx, unsigned NumIdx); | |
460 | ||
461 | static Type *getIndexedType(const Type *Ptr, | |
462 | uint64_t const *Idx, unsigned NumIdx); | |
463 | static Type *getIndexedType(const Type *Ptr, Value *Idx); | |
466 | 464 | |
467 | 465 | inline op_iterator idx_begin() { return op_begin()+1; } |
468 | 466 | inline const_op_iterator idx_begin() const { return op_begin()+1; } |
537 | 535 | unsigned Values, |
538 | 536 | const Twine &NameStr, |
539 | 537 | Instruction *InsertBefore) |
540 |
: Instruction(PointerType::get(check |
|
538 | : Instruction(PointerType::get(checkGEPType(⏎ | |
541 | 539 | getIndexedType(Ptr->getType(), |
542 | 540 | IdxBegin, IdxEnd)), |
543 | 541 |
cast |
556 | 554 | unsigned Values, |
557 | 555 | const Twine &NameStr, |
558 | 556 | BasicBlock *InsertAtEnd) |
559 |
: Instruction(PointerType::get(check |
|
557 | : Instruction(PointerType::get(checkGEPType(⏎ | |
560 | 558 | getIndexedType(Ptr->getType(), |
561 | 559 | IdxBegin, IdxEnd)), |
562 | 560 |
cast |
1458 | 1456 | /// |
1459 | 1457 | /// Null is returned if the indices are invalid for the specified type. |
1460 | 1458 | /// |
1461 | static const Type *getIndexedType(const Type *Agg, | |
1462 |
|
|
1459 | /// FIXME: Use ArrayRef⏎ | |
1460 | static Type *getIndexedType(const Type *Agg, | |
1461 | const unsigned *Idx, unsigned NumIdx); | |
1463 | 1462 | |
1464 | 1463 |
template |
1465 | static const Type *getIndexedType(const Type *Ptr, | |
1466 | RandomAccessIterator IdxBegin, | |
1467 | RandomAccessIterator IdxEnd, | |
1468 | // This argument ensures that we | |
1469 | // have an iterator we can do | |
1470 | // arithmetic on in constant time | |
1471 |
|
|
1464 | static Type *getIndexedType(const Type *Ptr,⏎ | |
1465 | RandomAccessIterator IdxBegin, | |
1466 | RandomAccessIterator IdxEnd, | |
1467 | // This argument ensures that we | |
1468 | // have an iterator we can do | |
1469 | // arithmetic on in constant time | |
1470 | std::random_access_iterator_tag) { | |
1472 | 1471 |
unsigned NumIdx = static_cast |
1473 | 1472 | |
1474 | 1473 | if (NumIdx > 0) |
1541 | 1540 | /// |
1542 | 1541 | /// Null is returned if the indices are invalid for the specified type. |
1543 | 1542 | /// |
1543 | /// FIXME: Remove the templates and just use ArrayRef. | |
1544 | 1544 |
template |
1545 | static const Type *getIndexedType(const Type *Ptr, | |
1546 | RandomAccessIterator IdxBegin, | |
1547 |
|
|
1545 | static Type *getIndexedType(const Type *Ptr,⏎ | |
1546 | RandomAccessIterator IdxBegin, | |
1547 | RandomAccessIterator IdxEnd) { | |
1548 | 1548 | return getIndexedType(Ptr, IdxBegin, IdxEnd, |
1549 | 1549 |
typename std::iterator_traits |
1550 | 1550 | iterator_category()); |
1551 | 1551 | } |
1552 |
static |
|
1552 | static Type *getIndexedType(const Type *Ptr, unsigned Idx);⏎ | |
1553 | 1553 | |
1554 | 1554 | typedef const unsigned* idx_iterator; |
1555 | 1555 | inline idx_iterator idx_begin() const { return Indices.begin(); } |
1589 | 1589 | RandomAccessIterator IdxEnd, |
1590 | 1590 | const Twine &NameStr, |
1591 | 1591 | Instruction *InsertBefore) |
1592 | : UnaryInstruction(checkType(getIndexedType(Agg->getType(), | |
1593 |
|
|
1592 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(),⏎ | |
1593 | IdxBegin, IdxEnd)), | |
1594 | 1594 | ExtractValue, Agg, InsertBefore) { |
1595 | 1595 | init(IdxBegin, IdxEnd, NameStr, |
1596 | 1596 |
typename std::iterator_traits |
1602 | 1602 | RandomAccessIterator IdxEnd, |
1603 | 1603 | const Twine &NameStr, |
1604 | 1604 | BasicBlock *InsertAtEnd) |
1605 | : UnaryInstruction(checkType(getIndexedType(Agg->getType(), | |
1606 |
|
|
1605 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(),⏎ | |
1606 | IdxBegin, IdxEnd)), | |
1607 | 1607 | ExtractValue, Agg, InsertAtEnd) { |
1608 | 1608 | init(IdxBegin, IdxEnd, NameStr, |
1609 | 1609 |
typename std::iterator_traits |
61 | 61 | (void) llvm::createDeadCodeEliminationPass(); |
62 | 62 | (void) llvm::createDeadInstEliminationPass(); |
63 | 63 | (void) llvm::createDeadStoreEliminationPass(); |
64 | (void) llvm::createDeadTypeEliminationPass(); | |
65 | 64 | (void) llvm::createDomOnlyPrinterPass(); |
66 | 65 | (void) llvm::createDomPrinterPass(); |
67 | 66 | (void) llvm::createDomOnlyViewerPass(); |
27 | 27 | class FunctionType; |
28 | 28 | class GVMaterializer; |
29 | 29 | class LLVMContext; |
30 | class StructType; | |
31 |
template |
|
32 |
template |
|
33 | typename KeyInfoT, typename ValueInfoT> class DenseMap; | |
30 | 34 | |
31 | 35 |
template<> struct ilist_traits |
32 | 36 |
: public SymbolTableListTraits |
144 | 148 | NamedMDListType NamedMDList; ///< The named metadata in the module |
145 | 149 | std::string GlobalScopeAsm; ///< Inline Asm at global scope. |
146 | 150 | ValueSymbolTable *ValSymTab; ///< Symbol table for values |
147 | TypeSymbolTable *TypeSymTab; ///< Symbol table for types | |
148 | 151 |
OwningPtr |
149 | 152 | std::string ModuleID; ///< Human readable identifier for the module |
150 | 153 | std::string TargetTriple; ///< Platform target triple Module compiled on |
230 | 233 | /// @name Generic Value Accessors |
231 | 234 | /// @{ |
232 | 235 | |
233 |
/// getNamedValue - Return the |
|
236 | /// getNamedValue - Return the global value in the module with⏎ | |
234 | 237 | /// the specified name, of arbitrary type. This method returns null |
235 | 238 | /// if a global with the specified name is not found. |
236 | 239 | GlobalValue *getNamedValue(StringRef Name) const; |
242 | 245 | /// getMDKindNames - Populate client supplied SmallVector with the name for |
243 | 246 | /// custom metadata IDs registered in this LLVMContext. |
244 | 247 |
void getMDKindNames(SmallVectorImpl |
248 | ||
249 | ||
250 |
typedef DenseMap |
|
251 |
DenseMapInfo |
|
252 | ||
253 | /// findUsedStructTypes - Walk the entire module and find all of the | |
254 | /// struct types that are in use, returning them in a vector. | |
255 |
void findUsedStructTypes(std::vector |
|
256 | ||
257 | /// getTypeByName - Return the type with the specified name, or null if there | |
258 | /// is none by that name. | |
259 | StructType *getTypeByName(StringRef Name) const; | |
245 | 260 | |
246 | 261 | /// @} |
247 | 262 | /// @name Function Accessors |
295 | 310 | GlobalVariable *getGlobalVariable(StringRef Name, |
296 | 311 | bool AllowInternal = false) const; |
297 | 312 | |
298 |
/// getNamedGlobal - Return the |
|
313 | /// getNamedGlobal - Return the global variable in the module with the⏎ | |
299 | 314 | /// specified name, of arbitrary type. This method returns null if a global |
300 | 315 | /// with the specified name is not found. |
301 | 316 | GlobalVariable *getNamedGlobal(StringRef Name) const { |
315 | 330 | /// @name Global Alias Accessors |
316 | 331 | /// @{ |
317 | 332 | |
318 |
/// getNamedAlias - Return the |
|
333 | /// getNamedAlias - Return the global alias in the module with the⏎ | |
319 | 334 | /// specified name, of arbitrary type. This method returns null if a global |
320 | 335 | /// with the specified name is not found. |
321 | 336 | GlobalAlias *getNamedAlias(StringRef Name) const; |
324 | 339 | /// @name Named Metadata Accessors |
325 | 340 | /// @{ |
326 | 341 | |
327 |
/// getNamedMetadata - Return the |
|
342 | /// getNamedMetadata - Return the NamedMDNode in the module with the⏎ | |
328 | 343 | /// specified name. This method returns null if a NamedMDNode with the |
329 | 344 | /// specified name is not found. |
330 | 345 | NamedMDNode *getNamedMetadata(const Twine &Name) const; |
331 | 346 | |
332 |
/// getOrInsertNamedMetadata - Return the |
|
347 | /// getOrInsertNamedMetadata - Return the named MDNode in the module⏎ | |
333 | 348 | /// with the specified name. This method returns a new NamedMDNode if a |
334 | 349 | /// NamedMDNode with the specified name is not found. |
335 | 350 | NamedMDNode *getOrInsertNamedMetadata(StringRef Name); |
337 | 352 | /// eraseNamedMetadata - Remove the given NamedMDNode from this module |
338 | 353 | /// and delete it. |
339 | 354 | void eraseNamedMetadata(NamedMDNode *NMD); |
340 | ||
341 | /// @} | |
342 | /// @name Type Accessors | |
343 | /// @{ | |
344 | ||
345 | /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If | |
346 | /// there is already an entry for this name, true is returned and the symbol | |
347 | /// table is not modified. | |
348 | bool addTypeName(StringRef Name, const Type *Ty); | |
349 | ||
350 | /// getTypeName - If there is at least one entry in the symbol table for the | |
351 | /// specified type, return it. | |
352 | std::string getTypeName(const Type *Ty) const; | |
353 | ||
354 | /// getTypeByName - Return the type with the specified name in this module, or | |
355 | /// null if there is none by that name. | |
356 | const Type *getTypeByName(StringRef Name) const; | |
357 | 355 | |
358 | 356 | /// @} |
359 | 357 | /// @name Materialization |
428 | 426 | const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; } |
429 | 427 | /// Get the Module's symbol table of global variable and function identifiers. |
430 | 428 | ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; } |
431 | /// Get the symbol table of types | |
432 | const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; } | |
433 | /// Get the Module's symbol table of types | |
434 | TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; } | |
435 | 429 | |
436 | 430 | /// @} |
437 | 431 | /// @name Global Variable Iteration |
438 | 432 | /// @{ |
439 | 433 | |
440 | /// Get an iterator to the first global variable | |
441 | 434 | global_iterator global_begin() { return GlobalList.begin(); } |
442 | /// Get a constant iterator to the first global variable | |
443 | 435 | const_global_iterator global_begin() const { return GlobalList.begin(); } |
444 | /// Get an iterator to the last global variable | |
445 | 436 | global_iterator global_end () { return GlobalList.end(); } |
446 | /// Get a constant iterator to the last global variable | |
447 | 437 | const_global_iterator global_end () const { return GlobalList.end(); } |
448 | /// Determine if the list of globals is empty. | |
449 | 438 | bool global_empty() const { return GlobalList.empty(); } |
450 | 439 | |
451 | 440 | /// @} |
452 | 441 | /// @name Function Iteration |
453 | 442 | /// @{ |
454 | 443 | |
455 | /// Get an iterator to the first function. | |
456 | 444 | iterator begin() { return FunctionList.begin(); } |
457 | /// Get a constant iterator to the first function. | |
458 | 445 | const_iterator begin() const { return FunctionList.begin(); } |
459 | /// Get an iterator to the last function. | |
460 | 446 | iterator end () { return FunctionList.end(); } |
461 | /// Get a constant iterator to the last function. | |
462 | 447 | const_iterator end () const { return FunctionList.end(); } |
463 | /// Determine how many functions are in the Module's list of functions. | |
464 | 448 | size_t size() const { return FunctionList.size(); } |
465 | /// Determine if the list of functions is empty. | |
466 | 449 | bool empty() const { return FunctionList.empty(); } |
467 | 450 | |
468 | 451 | /// @} |
486 | 469 | /// @name Alias Iteration |
487 | 470 | /// @{ |
488 | 471 | |
489 | /// Get an iterator to the first alias. | |
490 | 472 | alias_iterator alias_begin() { return AliasList.begin(); } |
491 | /// Get a constant iterator to the first alias. | |
492 | 473 | const_alias_iterator alias_begin() const { return AliasList.begin(); } |
493 | /// Get an iterator to the last alias. | |
494 | 474 | alias_iterator alias_end () { return AliasList.end(); } |
495 | /// Get a constant iterator to the last alias. | |
496 | 475 | const_alias_iterator alias_end () const { return AliasList.end(); } |
497 | /// Determine how many aliases are in the Module's list of aliases. | |
498 | 476 | size_t alias_size () const { return AliasList.size(); } |
499 | /// Determine if the list of aliases is empty. | |
500 | 477 | bool alias_empty() const { return AliasList.empty(); } |
501 | 478 | |
502 | 479 | |
504 | 481 | /// @name Named Metadata Iteration |
505 | 482 | /// @{ |
506 | 483 | |
507 | /// Get an iterator to the first named metadata. | |
508 | 484 | named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); } |
509 | /// Get a constant iterator to the first named metadata. | |
510 | 485 | const_named_metadata_iterator named_metadata_begin() const { |
511 | 486 | return NamedMDList.begin(); |
512 | 487 | } |
513 | 488 | |
514 | /// Get an iterator to the last named metadata. | |
515 | 489 | named_metadata_iterator named_metadata_end() { return NamedMDList.end(); } |
516 | /// Get a constant iterator to the last named metadata. | |
517 | 490 | const_named_metadata_iterator named_metadata_end() const { |
518 | 491 | return NamedMDList.end(); |
519 | 492 | } |
520 | 493 | |
521 | /// Determine how many NamedMDNodes are in the Module's list of named | |
522 | /// metadata. | |
523 | 494 | size_t named_metadata_size() const { return NamedMDList.size(); } |
524 | /// Determine if the list of named metadata is empty. | |
525 | 495 | bool named_metadata_empty() const { return NamedMDList.empty(); } |
526 | 496 | |
527 | 497 | |
529 | 499 | /// @name Utility functions for printing and dumping Module objects |
530 | 500 | /// @{ |
531 | 501 | |
532 |
/// Print the module to an output stream with |
|
502 | /// Print the module to an output stream with an optional⏎ | |
503 | /// AssemblyAnnotationWriter. | |
533 | 504 | void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const; |
534 | 505 | |
535 | 506 | /// Dump the module to stderr (for debugging). |
536 | 507 | void dump() const; |
508 | ||
537 | 509 | /// This function causes all the subinstructions to "let go" of all references |
538 | 510 | /// that they are maintaining. This allows one to 'delete' a whole class at |
539 | 511 | /// a time, even though there may be circular references... first all |
236 | 236 | MPM.add(createInstructionCombiningPass()); // Clean up after everything. |
237 | 237 | |
238 | 238 | if (!DisableUnitAtATime) { |
239 | // FIXME: We shouldn't bother with this anymore. | |
239 | 240 | MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes |
240 | MPM.add(createDeadTypeEliminationPass()); // Eliminate dead types | |
241 | 241 | |
242 | 242 | // GlobalOpt already deletes dead functions and globals, at -O3 try a |
243 | 243 | // late pass of GlobalDCE. It is capable of deleting dead cycles. |
70 | 70 | /// non-address taken internal globals. |
71 | 71 | /// |
72 | 72 | ModulePass *createGlobalOptimizerPass(); |
73 | ||
74 | ||
75 | //===----------------------------------------------------------------------===// | |
76 | /// createDeadTypeEliminationPass - Return a new pass that eliminates symbol | |
77 | /// table entries for types that are never used. | |
78 | /// | |
79 | ModulePass *createDeadTypeEliminationPass(); | |
80 | 73 | |
81 | 74 | |
82 | 75 | //===----------------------------------------------------------------------===// |
21 | 21 | class Instruction; |
22 | 22 |
typedef ValueMap |
23 | 23 | |
24 | /// ValueMapTypeRemapper - This is a class that can be implemented by clients | |
25 | /// to remap types when cloning constants and instructions. | |
26 | class ValueMapTypeRemapper { | |
27 | virtual void Anchor(); // Out of line method. | |
28 | public: | |
29 | ~ValueMapTypeRemapper() {} | |
30 | ||
31 | /// remapType - The client should implement this method if they want to | |
32 | /// remap types while mapping values. | |
33 | virtual Type *remapType(Type *SrcTy) = 0; | |
34 | }; | |
35 | ||
24 | 36 | /// RemapFlags - These are flags that the value mapping APIs allow. |
25 | 37 | enum RemapFlags { |
26 | 38 | RF_None = 0, |
41 | 53 | } |
42 | 54 | |
43 | 55 | Value *MapValue(const Value *V, ValueToValueMapTy &VM, |
44 |
RemapFlags Flags = RF_None |
|
56 | RemapFlags Flags = RF_None,⏎ | |
57 | ValueMapTypeRemapper *TypeMapper = 0); | |
58 | ||
45 | 59 | void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, |
46 |
RemapFlags Flags = RF_None |
|
60 | RemapFlags Flags = RF_None,⏎ | |
61 | ValueMapTypeRemapper *TypeMapper = 0); | |
62 | ||
63 | /// MapValue - provide versions that preserve type safety for MDNode and | |
64 | /// Constants. | |
65 | inline MDNode *MapValue(const MDNode *V, ValueToValueMapTy &VM, | |
66 | RemapFlags Flags = RF_None, | |
67 | ValueMapTypeRemapper *TypeMapper = 0) { | |
68 | return (MDNode*)MapValue((const Value*)V, VM, Flags, TypeMapper); | |
69 | } | |
70 | inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM, | |
71 | RemapFlags Flags = RF_None, | |
72 | ValueMapTypeRemapper *TypeMapper = 0) { | |
73 | return (Constant*)MapValue((const Value*)V, VM, Flags, TypeMapper); | |
74 | } | |
75 | ||
76 | ||
47 | 77 | } // End llvm namespace |
48 | 78 | |
49 | 79 | #endif |
14 | 14 | #ifndef LLVM_TYPE_H |
15 | 15 | #define LLVM_TYPE_H |
16 | 16 | |
17 | #include "llvm/AbstractTypeUser.h" | |
18 | 17 | #include "llvm/Support/Casting.h" |
19 |
#include |
|
20 | 18 | |
21 | 19 | namespace llvm { |
22 | 20 | |
23 | 21 | class DerivedType; |
24 | 22 | class PointerType; |
25 | 23 | class IntegerType; |
26 | class TypeMapBase; | |
27 | 24 | class raw_ostream; |
28 | 25 | class Module; |
29 | 26 | class LLVMContext; |
27 | class LLVMContextImpl; | |
30 | 28 |
template |
31 | 29 | |
32 | 30 | /// The instances of the Type class are immutable: once they are created, |
34 | 32 | /// type is ever created. Thus seeing if two types are equal is a matter of |
35 | 33 | /// doing a trivial pointer comparison. To enforce that no two equal instances |
36 | 34 | /// are created, Type instances can only be created via static factory methods |
37 |
/// in class Type and in derived classes. |
|
35 | /// in class Type and in derived classes. Once allocated, Types are never⏎ | |
36 | /// free'd. | |
38 | 37 | /// |
39 | /// Once allocated, Types are never free'd, unless they are an abstract type | |
40 | /// that is resolved to a more concrete type. | |
41 | /// | |
42 | /// Types themself don't have a name, and can be named either by: | |
43 | /// - using SymbolTable instance, typically from some Module, | |
44 | /// - using convenience methods in the Module class (which uses module's | |
45 | /// SymbolTable too). | |
46 | /// | |
47 | /// Opaque types are simple derived types with no state. There may be many | |
48 | /// different Opaque type objects floating around, but two are only considered | |
49 | /// identical if they are pointer equals of each other. This allows us to have | |
50 | /// two opaque types that end up resolving to different concrete types later. | |
51 | /// | |
52 | /// Opaque types are also kinda weird and scary and different because they have | |
53 | /// to keep a list of uses of the type. When, through linking, parsing, or | |
54 | /// bitcode reading, they become resolved, they need to find and update all | |
55 | /// users of the unknown type, causing them to reference a new, more concrete | |
56 | /// type. Opaque types are deleted when their use list dwindles to zero users. | |
57 | /// | |
58 | /// @brief Root of type hierarchy | |
59 |
class Type |
|
38 | class Type {⏎ | |
60 | 39 | public: |
61 | 40 | //===--------------------------------------------------------------------===// |
62 | 41 | /// Definitions of all of the base types for the Type system. Based on this |
84 | 63 | StructTyID, ///< 11: Structures |
85 | 64 | ArrayTyID, ///< 12: Arrays |
86 | 65 | PointerTyID, ///< 13: Pointers |
87 | OpaqueTyID, ///< 14: Opaque: type with unknown structure | |
88 |
VectorTyID, ///< 1 |
|
66 | VectorTyID, ///< 14: SIMD 'packed' format, or other vector type⏎ | |
89 | 67 | |
90 | 68 | NumTypeIDs, // Must remain as last defined ID |
91 | 69 | LastPrimitiveTyID = X86_MMXTyID, |
93 | 71 | }; |
94 | 72 | |
95 | 73 | private: |
96 | TypeID ID : 8; // The current base type of this type. | |
97 | bool Abstract : 1; // True if type contains an OpaqueType | |
98 | unsigned SubclassData : 23; //Space for subclasses to store data | |
99 | ||
100 | /// RefCount - This counts the number of PATypeHolders that are pointing to | |
101 | /// this type. When this number falls to zero, if the type is abstract and | |
102 | /// has no AbstractTypeUsers, the type is deleted. This is only sensical for | |
103 | /// derived types. | |
104 | /// | |
105 | mutable unsigned RefCount; | |
106 | ||
107 | 74 | /// Context - This refers to the LLVMContext in which this type was uniqued. |
108 | 75 | LLVMContext &Context; |
76 | ||
77 | TypeID ID : 8; // The current base type of this type. | |
78 | unsigned SubclassData : 24; // Space for subclasses to store data | |
79 | ||
80 | protected: | |
109 | 81 | friend class LLVMContextImpl; |
110 | ||
111 | const Type *getForwardedTypeInternal() const; | |
112 | ||
113 | // When the last reference to a forwarded type is removed, it is destroyed. | |
114 | void destroy() const; | |
115 | ||
116 | protected: | |
117 | explicit Type(LLVMContext &C, TypeID id) : | |
118 | ID(id), Abstract(false), SubclassData(0), | |
119 | RefCount(0), Context(C), | |
120 | ForwardType(0), NumContainedTys(0), | |
121 | ContainedTys(0) {} | |
122 | virtual ~Type() { | |
123 | assert(AbstractTypeUsers.empty() && "Abstract types remain"); | |
124 | } | |
125 | ||
126 | /// Types can become nonabstract later, if they are refined. | |
127 | /// | |
128 | inline void setAbstract(bool Val) { Abstract = Val; } | |
129 | ||
130 |
|
|
82 | explicit Type(LLVMContext &C, TypeID tid)⏎ | |
83 | : Context(C), ID(tid), SubclassData(0), | |
84 | NumContainedTys(0), ContainedTys(0) {} | |
85 | ~Type() {} | |
131 | 86 | |
132 | 87 | unsigned getSubclassData() const { return SubclassData; } |
133 | void setSubclassData(unsigned val) { SubclassData = val; } | |
134 | ||
135 | /// ForwardType - This field is used to implement the union find scheme for | |
136 | /// abstract types. When types are refined to other types, this field is set | |
137 | /// to the more refined type. Only abstract types can be forwarded. | |
138 | mutable const Type *ForwardType; | |
139 | ||
140 | ||
141 | /// AbstractTypeUsers - Implement a list of the users that need to be notified | |
142 | /// if I am a type, and I get resolved into a more concrete type. | |
143 | /// | |
144 |
mutable std::vector |
|
145 | ||
146 | /// NumContainedTys - Keeps track of how many PATypeHandle instances there | |
147 | /// are at the end of this type instance for the list of contained types. It | |
148 | /// is the subclasses responsibility to set this up. Set to 0 if there are no | |
149 |
|
|
88 | void setSubclassData(unsigned val) {⏎ | |
89 | SubclassData = val; | |
90 | // Ensure we don't have any accidental truncation. | |
91 | assert(SubclassData == val && "Subclass data too large for field"); | |
92 | } | |
93 | ||
94 | /// NumContainedTys - Keeps track of how many Type*'s there are in the | |
95 | /// ContainedTys list. | |
150 | 96 | unsigned NumContainedTys; |
151 | 97 | |
152 | /// ContainedTys - A pointer to the array of Types (PATypeHandle) contained | |
153 | /// by this Type. For example, this includes the arguments of a function | |
154 | /// type, the elements of a structure, the pointee of a pointer, the element | |
155 | /// type of an array, etc. This pointer may be 0 for types that don't | |
156 | /// contain other types (Integer, Double, Float). In general, the subclass | |
157 | /// should arrange for space for the PATypeHandles to be included in the | |
158 | /// allocation of the type object and set this pointer to the address of the | |
159 | /// first element. This allows the Type class to manipulate the ContainedTys | |
160 | /// without understanding the subclass's placement for this array. keeping | |
161 | /// it here also allows the subtype_* members to be implemented MUCH more | |
162 | /// efficiently, and dynamically very few types do not contain any elements. | |
163 |
|
|
98 | /// ContainedTys - A pointer to the array of Types contained by this Type.⏎ | |
99 | /// For example, this includes the arguments of a function type, the elements | |
100 | /// of a structure, the pointee of a pointer, the element type of an array, | |
101 | /// etc. This pointer may be 0 for types that don't contain other types | |
102 | /// (Integer, Double, Float). | |
103 | Type * const *ContainedTys; | |
164 | 104 | |
165 | 105 | public: |
166 | 106 | void print(raw_ostream &O) const; |
167 | ||
168 | /// @brief Debugging support: print to stderr | |
169 | 107 | void dump() const; |
170 | 108 | |
171 | /// @brief Debugging support: print to stderr (use type names from context | |
172 | /// module). | |
173 | void dump(const Module *Context) const; | |
174 | ||
175 |
/// getContext - |
|
109 | /// getContext - Return the LLVMContext in which this type was uniqued.⏎ | |
176 | 110 | LLVMContext &getContext() const { return Context; } |
177 | 111 | |
178 | 112 | //===--------------------------------------------------------------------===// |
204 | 138 | |
205 | 139 | /// isFloatingPointTy - Return true if this is one of the five floating point |
206 | 140 | /// types |
207 | bool isFloatingPointTy() const { return ID == FloatTyID || ID == DoubleTyID || | |
208 |
|
|
141 | bool isFloatingPointTy() const {⏎ | |
142 | return ID == FloatTyID || ID == DoubleTyID || | |
143 | ID == X86_FP80TyID || ID == FP128TyID || ID == PPC_FP128TyID; | |
144 | } | |
209 | 145 | |
210 | 146 | /// isX86_MMXTy - Return true if this is X86 MMX. |
211 | 147 | bool isX86_MMXTy() const { return ID == X86_MMXTyID; } |
248 | 184 | /// |
249 | 185 | bool isPointerTy() const { return ID == PointerTyID; } |
250 | 186 | |
251 | /// isOpaqueTy - True if this is an instance of OpaqueType. | |
252 | /// | |
253 | bool isOpaqueTy() const { return ID == OpaqueTyID; } | |
254 | ||
255 | 187 | /// isVectorTy - True if this is an instance of VectorType. |
256 | 188 | /// |
257 | 189 | bool isVectorTy() const { return ID == VectorTyID; } |
258 | ||
259 | /// isAbstract - True if the type is either an Opaque type, or is a derived | |
260 | /// type that includes an opaque type somewhere in it. | |
261 | /// | |
262 | inline bool isAbstract() const { return Abstract; } | |
263 | 190 | |
264 | 191 | /// canLosslesslyBitCastTo - Return true if this type could be converted |
265 | 192 | /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts |
275 | 202 | /// Here are some useful little methods to query what type derived types are |
276 | 203 | /// Note that all other types can just compare to see if this == Type::xxxTy; |
277 | 204 | /// |
278 | inline bool isPrimitiveType() const { return ID <= LastPrimitiveTyID; } | |
279 |
|
|
205 | bool isPrimitiveType() const { return ID <= LastPrimitiveTyID; }⏎ | |
206 | bool isDerivedType() const { return ID >= FirstDerivedTyID; } | |
280 | 207 | |
281 | 208 | /// isFirstClassType - Return true if the type is "first class", meaning it |
282 | 209 | /// is a valid type for a Value. |
283 | 210 | /// |
284 | inline bool isFirstClassType() const { | |
285 | // There are more first-class kinds than non-first-class kinds, so a | |
286 | // negative test is simpler than a positive one. | |
287 |
|
|
211 | bool isFirstClassType() const {⏎ | |
212 | return ID != FunctionTyID && ID != VoidTyID; | |
288 | 213 | } |
289 | 214 | |
290 | 215 | /// isSingleValueType - Return true if the type is a valid type for a |
291 | /// virtual register in codegen. This includes all first-class types | |
292 | /// except struct and array types. | |
293 | /// | |
294 | inline bool isSingleValueType() const { | |
295 |
|
|
216 | /// register in codegen. This includes all first-class types except struct⏎ | |
217 | /// and array types. | |
218 | /// | |
219 | bool isSingleValueType() const { | |
220 | return (ID != VoidTyID && isPrimitiveType()) || | |
296 | 221 | ID == IntegerTyID || ID == PointerTyID || ID == VectorTyID; |
297 | 222 | } |
298 | 223 | |
301 | 226 | /// extractvalue instruction. This includes struct and array types, but |
302 | 227 | /// does not include vector types. |
303 | 228 | /// |
304 |
|
|
229 | bool isAggregateType() const {⏎ | |
305 | 230 | return ID == StructTyID || ID == ArrayTyID; |
306 | 231 | } |
307 | 232 | |
318 | 243 | // it doesn't have a size. |
319 | 244 | if (ID != StructTyID && ID != ArrayTyID && ID != VectorTyID) |
320 | 245 | return false; |
321 | // If it is something that can have a size and it's concrete, it definitely | |
322 | // has a size, otherwise we have to try harder to decide. | |
323 |
|
|
246 | // Otherwise we have to try harder to decide.⏎ | |
247 | return isSizedDerivedType(); | |
324 | 248 | } |
325 | 249 | |
326 | 250 | /// getPrimitiveSizeInBits - Return the basic size of this type if it is a |
345 | 269 | /// have a stable mantissa (e.g. ppc long double), this method returns -1. |
346 | 270 | int getFPMantissaWidth() const; |
347 | 271 | |
348 | /// getForwardedType - Return the type that this type has been resolved to if | |
349 | /// it has been resolved to anything. This is used to implement the | |
350 | /// union-find algorithm for type resolution, and shouldn't be used by general | |
351 | /// purpose clients. | |
352 | const Type *getForwardedType() const { | |
353 | if (!ForwardType) return 0; | |
354 | return getForwardedTypeInternal(); | |
355 | } | |
356 | ||
357 | 272 | /// getScalarType - If this is a vector type, return the element type, |
358 |
/// otherwise return |
|
273 | /// otherwise return 'this'.⏎ | |
359 | 274 | const Type *getScalarType() const; |
360 | 275 | |
361 | 276 | //===--------------------------------------------------------------------===// |
362 |
// Type Iteration support |
|
277 | // Type Iteration support.⏎ | |
363 | 278 | // |
364 |
typedef |
|
279 | typedef Type * const *subtype_iterator;⏎ | |
365 | 280 | subtype_iterator subtype_begin() const { return ContainedTys; } |
366 | 281 | subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];} |
367 | 282 | |
369 | 284 | /// (defined a the end of the file). For derived types, this returns the |
370 | 285 | /// types 'contained' in the derived type. |
371 | 286 | /// |
372 |
|
|
287 | Type *getContainedType(unsigned i) const {⏎ | |
373 | 288 | assert(i < NumContainedTys && "Index out of range!"); |
374 |
return ContainedTys[i] |
|
289 | return ContainedTys[i];⏎ | |
375 | 290 | } |
376 | 291 | |
377 | 292 | /// getNumContainedTypes - Return the number of types in the derived type. |
384 | 299 | // |
385 | 300 | |
386 | 301 | /// getPrimitiveType - Return a type based on an identifier. |
387 | static const Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber); | |
388 | ||
389 | //===--------------------------------------------------------------------===// | |
390 |
|
|
302 | static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);⏎ | |
303 | ||
304 | //===--------------------------------------------------------------------===// | |
305 | // These are the builtin types that are always available. | |
391 | 306 | // |
392 | static const Type *getVoidTy(LLVMContext &C); | |
393 | static const Type *getLabelTy(LLVMContext &C); | |
394 | static const Type *getFloatTy(LLVMContext &C); | |
395 | static const Type *getDoubleTy(LLVMContext &C); | |
396 | static const Type *getMetadataTy(LLVMContext &C); | |
397 | static const Type *getX86_FP80Ty(LLVMContext &C); | |
398 | static const Type *getFP128Ty(LLVMContext &C); | |
399 | static const Type *getPPC_FP128Ty(LLVMContext &C); | |
400 | static const Type *getX86_MMXTy(LLVMContext &C); | |
401 | static const IntegerType *getIntNTy(LLVMContext &C, unsigned N); | |
402 | static const IntegerType *getInt1Ty(LLVMContext &C); | |
403 | static const IntegerType *getInt8Ty(LLVMContext &C); | |
404 | static const IntegerType *getInt16Ty(LLVMContext &C); | |
405 | static const IntegerType *getInt32Ty(LLVMContext &C); | |
406 |
static |
|
307 | static Type *getVoidTy(LLVMContext &C);⏎ | |
308 | static Type *getLabelTy(LLVMContext &C); | |
309 | static Type *getFloatTy(LLVMContext &C); | |
310 | static Type *getDoubleTy(LLVMContext &C); | |
311 | static Type *getMetadataTy(LLVMContext &C); | |
312 | static Type *getX86_FP80Ty(LLVMContext &C); | |
313 | static Type *getFP128Ty(LLVMContext &C); | |
314 | static Type *getPPC_FP128Ty(LLVMContext &C); | |
315 | static Type *getX86_MMXTy(LLVMContext &C); | |
316 | static IntegerType *getIntNTy(LLVMContext &C, unsigned N); | |
317 | static IntegerType *getInt1Ty(LLVMContext &C); | |
318 | static IntegerType *getInt8Ty(LLVMContext &C); | |
319 | static IntegerType *getInt16Ty(LLVMContext &C); | |
320 | static IntegerType *getInt32Ty(LLVMContext &C); | |
321 | static IntegerType *getInt64Ty(LLVMContext &C); | |
407 | 322 | |
408 | 323 | //===--------------------------------------------------------------------===// |
409 | 324 | // Convenience methods for getting pointer types with one of the above builtin |
410 | 325 | // types as pointee. |
411 | 326 | // |
412 | static const PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0); | |
413 | static const PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); | |
414 | static const PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); | |
415 | static const PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); | |
416 | static const PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); | |
417 | static const PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0); | |
418 | static const PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, | |
419 | unsigned AS = 0); | |
420 | static const PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); | |
421 | static const PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); | |
422 | static const PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); | |
423 | static const PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); | |
424 |
static |
|
327 | static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);⏎ | |
328 | static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); | |
329 | static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); | |
330 | static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); | |
331 | static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); | |
332 | static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0); | |
333 | static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0); | |
334 | static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); | |
335 | static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); | |
336 | static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); | |
337 | static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); | |
338 | static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0); | |
425 | 339 | |
426 | 340 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
427 | 341 | static inline bool classof(const Type *) { return true; } |
428 | 342 | |
429 | void addRef() const { | |
430 | assert(isAbstract() && "Cannot add a reference to a non-abstract type!"); | |
431 | ++RefCount; | |
432 | } | |
433 | ||
434 | void dropRef() const { | |
435 | assert(isAbstract() && "Cannot drop a reference to a non-abstract type!"); | |
436 | assert(RefCount && "No objects are currently referencing this object!"); | |
437 | ||
438 | // If this is the last PATypeHolder using this object, and there are no | |
439 | // PATypeHandles using it, the type is dead, delete it now. | |
440 | if (--RefCount == 0 && AbstractTypeUsers.empty()) | |
441 | this->destroy(); | |
442 | } | |
443 | ||
444 | /// addAbstractTypeUser - Notify an abstract type that there is a new user of | |
445 | /// it. This function is called primarily by the PATypeHandle class. | |
446 | /// | |
447 | void addAbstractTypeUser(AbstractTypeUser *U) const; | |
448 | ||
449 | /// removeAbstractTypeUser - Notify an abstract type that a user of the class | |
450 | /// no longer has a handle to the type. This function is called primarily by | |
451 | /// the PATypeHandle class. When there are no users of the abstract type, it | |
452 | /// is annihilated, because there is no way to get a reference to it ever | |
453 | /// again. | |
454 | /// | |
455 | void removeAbstractTypeUser(AbstractTypeUser *U) const; | |
456 | ||
457 | 343 | /// getPointerTo - Return a pointer to the current type. This is equivalent |
458 | 344 | /// to PointerType::get(Foo, AddrSpace). |
459 |
|
|
345 | PointerType *getPointerTo(unsigned AddrSpace = 0) const;⏎ | |
460 | 346 | |
461 | 347 | private: |
462 | 348 | /// isSizedDerivedType - Derived types like structures and arrays are sized |
463 | 349 | /// iff all of the members of the type are sized as well. Since asking for |
464 | 350 | /// their size is relatively uncommon, move this operation out of line. |
465 | 351 | bool isSizedDerivedType() const; |
466 | ||
467 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); | |
468 | virtual void typeBecameConcrete(const DerivedType *AbsTy); | |
469 | ||
470 | protected: | |
471 | // PromoteAbstractToConcrete - This is an internal method used to calculate | |
472 | // change "Abstract" from true to false when types are refined. | |
473 | void PromoteAbstractToConcrete(); | |
474 | friend class TypeMapBase; | |
475 | 352 | }; |
476 | 353 | |
477 | //===----------------------------------------------------------------------===// | |
478 | // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class. | |
479 | // These are defined here because they MUST be inlined, yet are dependent on | |
480 | // the definition of the Type class. | |
481 | // | |
482 | inline void PATypeHandle::addUser() { | |
483 | assert(Ty && "Type Handle has a null type!"); | |
484 | if (Ty->isAbstract()) | |
485 | Ty->addAbstractTypeUser(User);⏎ | |
354 | // Printing of types.⏎ | |
355 | static inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) { | |
356 | T.print(OS); | |
357 | return OS; | |
486 | 358 | } |
487 | inline void PATypeHandle::removeUser() { | |
488 | if (Ty->isAbstract()) | |
489 | Ty->removeAbstractTypeUser(User); | |
490 | } | |
491 | ||
492 | // Define inline methods for PATypeHolder. | |
493 | ||
494 | /// get - This implements the forwarding part of the union-find algorithm for | |
495 | /// abstract types. Before every access to the Type*, we check to see if the | |
496 | /// type we are pointing to is forwarding to a new type. If so, we drop our | |
497 | /// reference to the type. | |
498 | /// | |
499 | inline Type *PATypeHolder::get() const { | |
500 | if (Ty == 0) return 0; | |
501 | const Type *NewTy = Ty->getForwardedType(); | |
502 |
if (!NewTy) return const_cast |
|
503 |
return *const_cast |
|
504 | } | |
505 | ||
506 | inline void PATypeHolder::addRef() { | |
507 | if (Ty && Ty->isAbstract()) | |
508 | Ty->addRef(); | |
509 | } | |
510 | ||
511 | inline void PATypeHolder::dropRef() { | |
512 | if (Ty && Ty->isAbstract()) | |
513 | Ty->dropRef(); | |
514 | } | |
515 | ||
516 | ⏎ | |
359 | ⏎ | |
360 |
// allow isa |
|
361 |
template <> struct isa_impl |
|
362 | static inline bool doit(const Type &Ty) { | |
363 | return Ty.getTypeID() == Type::PointerTyID; | |
364 | } | |
365 | }; | |
366 | ||
367 | ||
517 | 368 | //===----------------------------------------------------------------------===// |
518 | 369 | // Provide specializations of GraphTraits to be able to treat a type as a |
519 | 370 | // graph of sub types. |
520 | 371 | |
372 | ||
521 | 373 |
template <> struct GraphTraits |
522 | 374 | typedef Type NodeType; |
523 | 375 | typedef Type::subtype_iterator ChildIteratorType; |
544 | 396 | } |
545 | 397 | }; |
546 | 398 | |
547 |
template <> struct isa_impl |
|
548 | static inline bool doit(const Type &Ty) { | |
549 | return Ty.getTypeID() == Type::PointerTyID; | |
550 | } | |
551 | }; | |
552 | ||
553 | raw_ostream &operator<<(raw_ostream &OS, const Type &T); | |
554 | ||
555 | 399 | } // End llvm namespace |
556 | 400 | |
557 | 401 | #endif |
None | //===-- llvm/TypeSymbolTable.h - Implement a Type Symtab --------*- C++ -*-===// | |
1 | // | |
2 | // The LLVM Compiler Infrastructure | |
3 | // | |
4 | // This file is distributed under the University of Illinois Open Source | |
5 | // License. See LICENSE.TXT for details. | |
6 | // | |
7 | //===----------------------------------------------------------------------===// | |
8 | // | |
9 | // This file implements the name/type symbol table for LLVM. | |
10 | // | |
11 | //===----------------------------------------------------------------------===// | |
12 | ||
13 | #ifndef LLVM_TYPE_SYMBOL_TABLE_H | |
14 | #define LLVM_TYPE_SYMBOL_TABLE_H | |
15 | ||
16 | #include "llvm/Type.h" | |
17 | #include "llvm/ADT/StringRef.h" | |
18 | #include "llvm/Support/DataTypes.h" | |
19 | #include | |
20 | ||
21 | namespace llvm { | |
22 | ||
23 | /// This class provides a symbol table of name/type pairs with operations to | |
24 | /// support constructing, searching and iterating over the symbol table. The | |
25 | /// class derives from AbstractTypeUser so that the contents of the symbol | |
26 | /// table can be updated when abstract types become concrete. | |
27 | class TypeSymbolTable : public AbstractTypeUser { | |
28 | ||
29 | /// @name Types | |
30 | /// @{ | |
31 | public: | |
32 | ||
33 | /// @brief A mapping of names to types. | |
34 |
typedef std::map |
|
35 | ||
36 | /// @brief An iterator over the TypeMap. | |
37 | typedef TypeMap::iterator iterator; | |
38 | ||
39 | /// @brief A const_iterator over the TypeMap. | |
40 | typedef TypeMap::const_iterator const_iterator; | |
41 | ||
42 | /// @} | |
43 | /// @name Constructors | |
44 | /// @{ | |
45 | public: | |
46 | ||
47 | TypeSymbolTable():LastUnique(0) {} | |
48 | ~TypeSymbolTable(); | |
49 | ||
50 | /// @} | |
51 | /// @name Accessors | |
52 | /// @{ | |
53 | public: | |
54 | ||
55 | /// Generates a unique name for a type based on the \p BaseName by | |
56 | /// incrementing an integer and appending it to the name, if necessary | |
57 | /// @returns the unique name | |
58 | /// @brief Get a unique name for a type | |
59 | std::string getUniqueName(StringRef BaseName) const; | |
60 | ||
61 | /// This method finds the type with the given \p name in the type map | |
62 | /// and returns it. | |
63 | /// @returns null if the name is not found, otherwise the Type | |
64 | /// associated with the \p name. | |
65 | /// @brief Lookup a type by name. | |
66 | Type *lookup(StringRef name) const; | |
67 | ||
68 | /// Lookup the type associated with name. | |
69 | /// @returns end() if the name is not found, or an iterator at the entry for | |
70 | /// Type. | |
71 | iterator find(StringRef Name) { | |
72 | return tmap.find(Name); | |
73 | } | |
74 | ||
75 | /// Lookup the type associated with name. | |
76 | /// @returns end() if the name is not found, or an iterator at the entry for | |
77 | /// Type. | |
78 | const_iterator find(StringRef Name) const { | |
79 | return tmap.find(Name); | |
80 | } | |
81 | ||
82 | /// @returns true iff the symbol table is empty. | |
83 | /// @brief Determine if the symbol table is empty | |
84 | inline bool empty() const { return tmap.empty(); } | |
85 | ||
86 | /// @returns the size of the symbol table | |
87 | /// @brief The number of name/type pairs is returned. | |
88 | inline unsigned size() const { return unsigned(tmap.size()); } | |
89 | ||
90 | /// This function can be used from the debugger to display the | |
91 | /// content of the symbol table while debugging. | |
92 | /// @brief Print out symbol table on stderr | |
93 | void dump() const; | |
94 | ||
95 | /// @} | |
96 | /// @name Iteration | |
97 | /// @{ | |
98 | public: | |
99 | /// Get an iterator to the start of the symbol table | |
100 | inline iterator begin() { return tmap.begin(); } | |
101 | ||
102 | /// @brief Get a const_iterator to the start of the symbol table | |
103 | inline const_iterator begin() const { return tmap.begin(); } | |
104 | ||
105 | /// Get an iterator to the end of the symbol table. | |
106 | inline iterator end() { return tmap.end(); } | |
107 | ||
108 | /// Get a const_iterator to the end of the symbol table. | |
109 | inline const_iterator end() const { return tmap.end(); } | |
110 | ||
111 | /// @} | |
112 | /// @name Mutators | |
113 | /// @{ | |
114 | public: | |
115 | ||
116 | /// Inserts a type into the symbol table with the specified name. There can be | |
117 | /// a many-to-one mapping between names and types. This method allows a type | |
118 | /// with an existing entry in the symbol table to get a new name. | |
119 | /// @brief Insert a type under a new name. | |
120 | void insert(StringRef Name, const Type *Typ); | |
121 | ||
122 | /// Remove a type at the specified position in the symbol table. | |
123 | /// @returns the removed Type. | |
124 | /// @returns the Type that was erased from the symbol table. | |
125 | Type* remove(iterator TI); | |
126 | ||
127 | /// @} | |
128 | /// @name AbstractTypeUser Methods | |
129 | /// @{ | |
130 | private: | |
131 | /// This function is called when one of the types in the type plane | |
132 | /// is refined. | |
133 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); | |
134 | ||
135 | /// This function marks a type as being concrete (defined). | |
136 | virtual void typeBecameConcrete(const DerivedType *AbsTy); | |
137 | ||
138 | /// @} | |
139 | /// @name Internal Data | |
140 | /// @{ | |
141 | private: | |
142 | TypeMap tmap; ///< This is the mapping of names to types. | |
143 | mutable uint32_t LastUnique; ///< Counter for tracking unique names | |
144 | ||
145 | /// @} | |
146 | ||
147 | }; | |
148 | ||
149 | } // End llvm namespace | |
150 | ||
151 | #endif |
13 | 13 | #ifndef LLVM_VALUE_H |
14 | 14 | #define LLVM_VALUE_H |
15 | 15 | |
16 | #include "llvm/AbstractTypeUser.h" | |
17 | 16 | #include "llvm/Use.h" |
18 | 17 | #include "llvm/ADT/StringRef.h" |
19 | 18 | #include "llvm/Support/Casting.h" |
31 | 30 | class GlobalAlias; |
32 | 31 | class InlineAsm; |
33 | 32 | class ValueSymbolTable; |
34 | class TypeSymbolTable; | |
35 | 33 |
template |
36 | 34 |
template |
37 | 35 | class AssertingVH; |
42 | 40 | class LLVMContext; |
43 | 41 | class Twine; |
44 | 42 | class MDNode; |
43 | class Type; | |
45 | 44 | |
46 | 45 | //===----------------------------------------------------------------------===// |
47 | 46 | // Value Class |
76 | 75 | /// This field is initialized to zero by the ctor. |
77 | 76 | unsigned short SubclassData; |
78 | 77 | |
79 |
|
|
78 | Type *VTy;⏎ | |
80 | 79 | Use *UseList; |
81 | 80 | |
82 | 81 | friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name. |
83 | 82 | friend class ValueHandleBase; |
84 | friend class AbstractTypeUser; | |
85 | 83 | ValueName *Name; |
86 | 84 | |
87 | 85 | void operator=(const Value &); // Do not implement |
106 | 104 | |
107 | 105 | /// All values are typed, get the type of this value. |
108 | 106 | /// |
109 |
|
|
107 | Type *getType() const { return VTy; }⏎ | |
110 | 108 | |
111 | 109 | /// All values hold a context through their type. |
112 | 110 | LLVMContext &getContext() const; |
113 | 111 | |
114 | 112 | // All values can potentially be named... |
115 |
|
|
113 | bool hasName() const { return Name != 0; }⏎ | |
116 | 114 | ValueName *getValueName() const { return Name; } |
117 | 115 | |
118 | 116 | /// getName() - Return a constant reference to the value's name. This is cheap |
278 | 276 | return true; // Values are always values. |
279 | 277 | } |
280 | 278 | |
281 | /// getRawType - This should only be used to implement the vmcore library. | |
282 | /// | |
283 | const Type *getRawType() const { return VTy.getRawType(); } | |
284 | ||
285 | 279 | /// stripPointerCasts - This method strips off any unneeded pointer |
286 | 280 | /// casts from the specified value, returning the original uncasted value. |
287 | 281 | /// Note that the returned value has pointer type if the specified value does. |
308 | 302 | /// MaximumAlignment - This is the greatest alignment value supported by |
309 | 303 | /// load, store, and alloca instructions, and global values. |
310 | 304 | static const unsigned MaximumAlignment = 1u << 29; |
305 | ||
306 | /// mutateType - Mutate the type of this Value to be of the specified type. | |
307 | /// Note that this is an extremely dangerous operation which can create | |
308 | /// completely invalid IR very easily. It is strongly recommended that you | |
309 | /// recreate IR objects with the right types instead of mutating them in | |
310 | /// place. | |
311 | void mutateType(Type *Ty) { | |
312 | VTy = Ty; | |
313 | } | |
311 | 314 | |
312 | 315 | protected: |
313 | 316 | unsigned short getSubclassDataFromValue() const { return SubclassData; } |
67 | 67 | */ |
68 | 68 | typedef struct LLVMOpaqueType *LLVMTypeRef; |
69 | 69 | |
70 | /** | |
71 | * When building recursive types using LLVMRefineType, LLVMTypeRef values may | |
72 | * become invalid; use LLVMTypeHandleRef to resolve this problem. See the | |
73 | * llvm::AbstractTypeHolder class. | |
74 | */ | |
75 | typedef struct LLVMOpaqueTypeHandle *LLVMTypeHandleRef; | |
76 | ||
77 | 70 | typedef struct LLVMOpaqueValue *LLVMValueRef; |
78 | 71 | typedef struct LLVMOpaqueBasicBlock *LLVMBasicBlockRef; |
79 | 72 | typedef struct LLVMOpaqueBuilder *LLVMBuilderRef; |
205 | 198 | LLVMStructTypeKind, /**< Structures */ |
206 | 199 | LLVMArrayTypeKind, /**< Arrays */ |
207 | 200 | LLVMPointerTypeKind, /**< Pointers */ |
208 | LLVMOpaqueTypeKind, /**< Opaque: type with unknown structure */ | |
209 | 201 | LLVMVectorTypeKind, /**< SIMD 'packed' format, or other vector type */ |
210 | 202 | LLVMMetadataTypeKind, /**< Metadata */ |
211 | 203 | LLVMX86_MMXTypeKind /**< X86 MMX */ |
319 | 311 | const char *LLVMGetTarget(LLVMModuleRef M); |
320 | 312 | void LLVMSetTarget(LLVMModuleRef M, const char *Triple); |
321 | 313 | |
322 | /** See Module::addTypeName. */ | |
323 | LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty); | |
324 | void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name); | |
325 | LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name); | |
326 | const char *LLVMGetTypeName(LLVMModuleRef M, LLVMTypeRef Ty); | |
327 | ||
328 | 314 | /** See Module::dump. */ |
329 | 315 | void LLVMDumpModule(LLVMModuleRef M); |
330 | 316 | |
417 | 403 | /* Operations on other types */ |
418 | 404 | LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C); |
419 | 405 | LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C); |
420 | LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C); | |
421 | 406 | LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C); |
422 | 407 | |
423 | 408 | LLVMTypeRef LLVMVoidType(void); |
424 | 409 | LLVMTypeRef LLVMLabelType(void); |
425 | 410 | LLVMTypeRef LLVMOpaqueType(void); |
426 | 411 | LLVMTypeRef LLVMX86MMXType(void); |
427 | ||
428 | /* Operations on type handles */ | |
429 | LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy); | |
430 | void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy); | |
431 | LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle); | |
432 | void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle); | |
433 | ||
434 | 412 | |
435 | 413 | /*===-- Values ------------------------------------------------------------===*/ |
436 | 414 | |
1116 | 1094 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef ) |
1117 | 1095 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef ) |
1118 | 1096 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef ) |
1119 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder, LLVMTypeHandleRef ) | |
1120 | 1097 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef ) |
1121 | 1098 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef ) |
1122 | 1099 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef ) |
28 | 28 | |
29 | 29 | /** See llvm::createDeadArgEliminationPass function. */ |
30 | 30 | void LLVMAddDeadArgEliminationPass(LLVMPassManagerRef PM); |
31 | ||
32 | /** See llvm::createDeadTypeEliminationPass function. */ | |
33 | void LLVMAddDeadTypeEliminationPass(LLVMPassManagerRef PM); | |
34 | 31 | |
35 | 32 | /** See llvm::createFunctionAttrsPass function. */ |
36 | 33 | void LLVMAddFunctionAttrsPass(LLVMPassManagerRef PM); |
37 | 37 | lltok::Kind CurKind; |
38 | 38 | std::string StrVal; |
39 | 39 | unsigned UIntVal; |
40 |
|
|
40 | Type *TyVal;⏎ | |
41 | 41 | APFloat APFloatVal; |
42 | 42 | APSInt APSIntVal; |
43 | 43 | |
55 | 55 | LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); } |
56 | 56 | lltok::Kind getKind() const { return CurKind; } |
57 | 57 | const std::string &getStrVal() const { return StrVal; } |
58 |
|
|
58 | Type *getTyVal() const { return TyVal; }⏎ | |
59 | 59 | unsigned getUIntVal() const { return UIntVal; } |
60 | 60 | const APSInt &getAPSIntVal() const { return APSIntVal; } |
61 | 61 | const APFloat &getAPFloatVal() const { return APFloatVal; } |
88 | 88 | ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin()); |
89 | 89 | } |
90 | 90 | |
91 | ||
92 | if (!ForwardRefTypes.empty()) | |
93 | return Error(ForwardRefTypes.begin()->second.second, | |
94 | "use of undefined type named '" + | |
95 | ForwardRefTypes.begin()->first + "'"); | |
96 | if (!ForwardRefTypeIDs.empty()) | |
97 | return Error(ForwardRefTypeIDs.begin()->second.second, | |
98 | "use of undefined type '%" + | |
99 |
|
|
91 | for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i)⏎ | |
92 | if (NumberedTypes[i].second.isValid()) | |
93 | return Error(NumberedTypes[i].second, | |
94 | "use of undefined type '%" + Twine(i) + "'"); | |
95 | ||
96 |
for (StringMap |
|
97 | NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) | |
98 | if (I->second.second.isValid()) | |
99 | return Error(I->second.second, | |
100 | "use of undefined type named '" + I->getKey() + "'"); | |
100 | 101 | |
101 | 102 | if (!ForwardRefVals.empty()) |
102 | 103 | return Error(ForwardRefVals.begin()->second.second, |
292 | 293 | /// ::= LocalVarID '=' 'type' type |
293 | 294 | bool LLParser::ParseUnnamedType() { |
294 | 295 | LocTy TypeLoc = Lex.getLoc(); |
295 | unsigned TypeID = NumberedTypes.size(); | |
296 | if (Lex.getUIntVal() != TypeID) | |
297 | return Error(Lex.getLoc(), "type expected to be numbered '%" + | |
298 |
|
|
296 | unsigned TypeID = Lex.getUIntVal();⏎ | |
299 | 297 | Lex.Lex(); // eat LocalVarID; |
300 | 298 | |
301 | 299 | if (ParseToken(lltok::equal, "expected '=' after name") || |
302 | 300 | ParseToken(lltok::kw_type, "expected 'type' after '='")) |
303 | 301 | return true; |
304 | 302 | |
305 | PATypeHolder Ty(Type::getVoidTy(Context)); | |
306 | if (ParseType(Ty)) return true; | |
307 | ||
308 | // See if this type was previously referenced. | |
309 |
std::map |
|
310 | FI = ForwardRefTypeIDs.find(TypeID); | |
311 | if (FI != ForwardRefTypeIDs.end()) { | |
312 | if (FI->second.first.get() == Ty) | |
313 | return Error(TypeLoc, "self referential type is invalid"); | |
314 | ||
315 |
cast |
|
316 | Ty = FI->second.first.get(); | |
317 | ForwardRefTypeIDs.erase(FI); | |
318 | } | |
319 | ||
320 | NumberedTypes.push_back(Ty); | |
321 | ||
322 | return false; | |
323 | }⏎ | |
303 | if (TypeID >= NumberedTypes.size())⏎ | |
304 | NumberedTypes.resize(TypeID+1); | |
305 | ||
306 | Type *Result = 0; | |
307 | if (ParseStructDefinition(TypeLoc, "", | |
308 | NumberedTypes[TypeID], Result)) return true; | |
309 | ||
310 |
if (!isa |
|
311 |
std::pair |
|
312 | if (Entry.first) | |
313 | return Error(TypeLoc, "non-struct types may not be recursive"); | |
314 | Entry.first = Result; | |
315 | Entry.second = SMLoc(); | |
316 | } | |
317 | ||
318 | return false; | |
319 | } | |
320 | ||
324 | 321 | |
325 | 322 | /// toplevelentity |
326 | 323 | /// ::= LocalVar '=' 'type' type |
329 | 326 | LocTy NameLoc = Lex.getLoc(); |
330 | 327 | Lex.Lex(); // eat LocalVar. |
331 | 328 | |
332 | PATypeHolder Ty(Type::getVoidTy(Context)); | |
333 | ||
334 | 329 | if (ParseToken(lltok::equal, "expected '=' after name") || |
335 | ParseToken(lltok::kw_type, "expected 'type' after name") || | |
336 |
ParseT |
|
330 | ParseToken(lltok::kw_type, "expected 'type' after name"))⏎ | |
337 | 331 | return true; |
338 | ||
339 | // Set the type name, checking for conflicts as we do so. | |
340 | bool AlreadyExists = M->addTypeName(Name, Ty); | |
341 | if (!AlreadyExists) return false; | |
342 | ||
343 | // See if this type is a forward reference. We need to eagerly resolve | |
344 | // types to allow recursive type redefinitions below. | |
345 |
std::map |
|
346 | FI = ForwardRefTypes.find(Name); | |
347 | if (FI != ForwardRefTypes.end()) { | |
348 | if (FI->second.first.get() == Ty) | |
349 | return Error(NameLoc, "self referential type is invalid"); | |
350 | ||
351 |
cast |
|
352 | Ty = FI->second.first.get(); | |
353 | ForwardRefTypes.erase(FI); | |
354 | return false; | |
355 | } | |
356 | ||
357 | // Inserting a name that is already defined, get the existing name. | |
358 | assert(M->getTypeByName(Name) && "Conflict but no matching type?!"); | |
359 | ||
360 | // Otherwise, this is an attempt to redefine a type, report the error. | |
361 | return Error(NameLoc, "redefinition of type named '" + Name + "' of type '" + | |
362 |
|
|
332 | ⏎ | |
333 | Type *Result = 0; | |
334 | if (ParseStructDefinition(NameLoc, Name, | |
335 | NamedTypes[Name], Result)) return true; | |
336 | ||
337 |
if (!isa |
|
338 |
std::pair |
|
339 | if (Entry.first) | |
340 | return Error(NameLoc, "non-struct types may not be recursive"); | |
341 | Entry.first = Result; | |
342 | Entry.second = SMLoc(); | |
343 | } | |
344 | ||
345 | return false; | |
363 | 346 | } |
364 | 347 | |
365 | 348 | |
535 | 518 | unsigned MetadataID = 0; |
536 | 519 | |
537 | 520 | LocTy TyLoc; |
538 |
|
|
521 | Type *Ty = 0;⏎ | |
539 | 522 |
SmallVector |
540 | 523 | if (ParseUInt32(MetadataID) || |
541 | 524 | ParseToken(lltok::equal, "expected '=' here") || |
667 | 650 | LocTy UnnamedAddrLoc; |
668 | 651 | LocTy TyLoc; |
669 | 652 | |
670 |
|
|
653 | Type *Ty = 0;⏎ | |
671 | 654 | if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) || |
672 | 655 | ParseOptionalAddrSpace(AddrSpace) || |
673 | 656 | ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr, |
791 | 774 | |
792 | 775 | // Otherwise, create a new forward reference for this value and remember it. |
793 | 776 | GlobalValue *FwdVal; |
794 |
if (const FunctionType *FT = dyn_cast |
|
795 | // Function types can return opaque but functions can't. | |
796 | if (FT->getReturnType()->isOpaqueTy()) { | |
797 | Error(Loc, "function may not return opaque type"); | |
798 | return 0; | |
799 | } | |
800 | ⏎ | |
777 |
if (const FunctionType *FT = dyn_cast |
|
801 | 778 | FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); |
802 |
|
|
779 | else⏎ | |
803 | 780 | FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, |
804 | 781 | GlobalValue::ExternalWeakLinkage, 0, Name); |
805 | } | |
806 | 782 | |
807 | 783 | ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); |
808 | 784 | return FwdVal; |
836 | 812 | |
837 | 813 | // Otherwise, create a new forward reference for this value and remember it. |
838 | 814 | GlobalValue *FwdVal; |
839 |
if (const FunctionType *FT = dyn_cast |
|
840 | // Function types can return opaque but functions can't. | |
841 | if (FT->getReturnType()->isOpaqueTy()) { | |
842 | Error(Loc, "function may not return opaque type"); | |
843 | return 0; | |
844 |
|
|
815 |
if (const FunctionType *FT = dyn_cast |
|
845 | 816 | FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); |
846 |
|
|
817 | else⏎ | |
847 | 818 | FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, |
848 | 819 | GlobalValue::ExternalWeakLinkage, 0, ""); |
849 | } | |
850 | 820 | |
851 | 821 | ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); |
852 | 822 | return FwdVal; |
1227 | 1197 | // Type Parsing. |
1228 | 1198 | //===----------------------------------------------------------------------===// |
1229 | 1199 | |
1230 | /// ParseType - Parse and resolve a full type. | |
1231 | bool LLParser::ParseType(PATypeHolder &Result, bool AllowVoid) { | |
1232 | LocTy TypeLoc = Lex.getLoc(); | |
1233 | if (ParseTypeRec(Result)) return true; | |
1234 | ||
1235 | // Verify no unresolved uprefs. | |
1236 | if (!UpRefs.empty()) | |
1237 | return Error(UpRefs.back().Loc, "invalid unresolved type up reference"); | |
1238 | ||
1239 | if (!AllowVoid && Result.get()->isVoidTy()) | |
1240 | return Error(TypeLoc, "void type only allowed for function results"); | |
1241 | ||
1242 | return false; | |
1243 | } | |
1244 | ||
1245 | /// HandleUpRefs - Every time we finish a new layer of types, this function is | |
1246 | /// called. It loops through the UpRefs vector, which is a list of the | |
1247 | /// currently active types. For each type, if the up-reference is contained in | |
1248 | /// the newly completed type, we decrement the level count. When the level | |
1249 | /// count reaches zero, the up-referenced type is the type that is passed in: | |
1250 | /// thus we can complete the cycle. | |
1251 | /// | |
1252 | PATypeHolder LLParser::HandleUpRefs(const Type *ty) { | |
1253 | // If Ty isn't abstract, or if there are no up-references in it, then there is | |
1254 | // nothing to resolve here. | |
1255 | if (!ty->isAbstract() || UpRefs.empty()) return ty; | |
1256 | ||
1257 | PATypeHolder Ty(ty); | |
1258 | #if 0 | |
1259 | dbgs() << "Type '" << *Ty | |
1260 | << "' newly formed. Resolving upreferences.\n" | |
1261 | << UpRefs.size() << " upreferences active!\n"; | |
1262 | #endif | |
1263 | ||
1264 | // If we find any resolvable upreferences (i.e., those whose NestingLevel goes | |
1265 | // to zero), we resolve them all together before we resolve them to Ty. At | |
1266 | // the end of the loop, if there is anything to resolve to Ty, it will be in | |
1267 | // this variable. | |
1268 | OpaqueType *TypeToResolve = 0; | |
1269 | ||
1270 | for (unsigned i = 0; i != UpRefs.size(); ++i) { | |
1271 | // Determine if 'Ty' directly contains this up-references 'LastContainedTy'. | |
1272 | bool ContainsType = | |
1273 | std::find(Ty->subtype_begin(), Ty->subtype_end(), | |
1274 | UpRefs[i].LastContainedTy) != Ty->subtype_end(); | |
1275 | ||
1276 | #if 0 | |
1277 | dbgs() << " UR#" << i << " - TypeContains(" << *Ty << ", " | |
1278 | << *UpRefs[i].LastContainedTy << ") = " | |
1279 | << (ContainsType ? "true" : "false") | |
1280 | << " level=" << UpRefs[i].NestingLevel << "\n"; | |
1281 | #endif | |
1282 | if (!ContainsType) | |
1283 | continue; | |
1284 | ||
1285 | // Decrement level of upreference | |
1286 | unsigned Level = --UpRefs[i].NestingLevel; | |
1287 | UpRefs[i].LastContainedTy = Ty; | |
1288 | ||
1289 | // If the Up-reference has a non-zero level, it shouldn't be resolved yet. | |
1290 | if (Level != 0) | |
1291 | continue; | |
1292 | ||
1293 | #if 0 | |
1294 | dbgs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n"; | |
1295 | #endif | |
1296 | if (!TypeToResolve) | |
1297 | TypeToResolve = UpRefs[i].UpRefTy; | |
1298 | else | |
1299 | UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve); | |
1300 | UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list. | |
1301 | --i; // Do not skip the next element. | |
1302 | } | |
1303 | ||
1304 | if (TypeToResolve) | |
1305 | TypeToResolve->refineAbstractTypeTo(Ty); | |
1306 | ||
1307 | return Ty; | |
1308 | } | |
1309 | ||
1310 | ||
1311 | /// ParseTypeRec - The recursive function used to process the internal | |
1312 | /// implementation details of types. | |
1313 | bool LLParser::ParseTypeRec(PATypeHolder &Result) {⏎ | |
1200 | /// ParseType - Parse a type.⏎ | |
1201 | bool LLParser::ParseType(Type *&Result, bool AllowVoid) { | |
1202 | SMLoc TypeLoc = Lex.getLoc(); | |
1314 | 1203 | switch (Lex.getKind()) { |
1315 | 1204 | default: |
1316 | 1205 | return TokError("expected type"); |
1317 | 1206 | case lltok::Type: |
1318 |
// Type |
|
1207 | // Type ::= 'float' | 'void' (etc)⏎ | |
1319 | 1208 | Result = Lex.getTyVal(); |
1320 | 1209 | Lex.Lex(); |
1321 | 1210 | break; |
1322 | case lltok::kw_opaque: | |
1323 | // TypeRec ::= 'opaque' | |
1324 | Result = OpaqueType::get(Context); | |
1325 | Lex.Lex(); | |
1326 | break; | |
1327 | 1211 | case lltok::lbrace: |
1328 | // TypeRec ::= '{' ... '}' | |
1329 |
|
|
1212 | // Type ::= StructType⏎ | |
1213 | if (ParseAnonStructType(Result, false)) | |
1330 | 1214 | return true; |
1331 | 1215 | break; |
1332 | 1216 | case lltok::lsquare: |
1333 |
// Type |
|
1217 | // Type ::= '[' ... ']'⏎ | |
1334 | 1218 | Lex.Lex(); // eat the lsquare. |
1335 | 1219 | if (ParseArrayVectorType(Result, false)) |
1336 | 1220 | return true; |
1337 | 1221 | break; |
1338 | 1222 | case lltok::less: // Either vector or packed struct. |
1339 |
// Type |
|
1223 | // Type ::= '<' ... '>'⏎ | |
1340 | 1224 | Lex.Lex(); |
1341 | 1225 | if (Lex.getKind() == lltok::lbrace) { |
1342 |
if (Parse |
|
1226 | if (ParseAnonStructType(Result, true) ||⏎ | |
1343 | 1227 | ParseToken(lltok::greater, "expected '>' at end of packed struct")) |
1344 | 1228 | return true; |
1345 | 1229 | } else if (ParseArrayVectorType(Result, true)) |
1346 | 1230 | return true; |
1347 | 1231 | break; |
1348 | case lltok::LocalVar: | |
1349 | // TypeRec ::= %foo | |
1350 | if (const Type *T = M->getTypeByName(Lex.getStrVal())) { | |
1351 | Result = T; | |
1352 | } else { | |
1353 | Result = OpaqueType::get(Context); | |
1354 | ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(), | |
1355 | std::make_pair(Result, | |
1356 | Lex.getLoc()))); | |
1357 | M->addTypeName(Lex.getStrVal(), Result.get()); | |
1358 |
|
|
1232 | case lltok::LocalVar: {⏎ | |
1233 | // Type ::= %foo | |
1234 |
std::pair |
|
1235 | ||
1236 | // If the type hasn't been defined yet, create a forward definition and | |
1237 | // remember where that forward def'n was seen (in case it never is defined). | |
1238 | if (Entry.first == 0) { | |
1239 | Entry.first = StructType::createNamed(Context, Lex.getStrVal()); | |
1240 | Entry.second = Lex.getLoc(); | |
1241 | } | |
1242 | Result = Entry.first; | |
1359 | 1243 | Lex.Lex(); |
1360 | 1244 | break; |
1361 | ||
1362 | case lltok::LocalVarID: | |
1363 | // TypeRec ::= %4 | |
1364 | if (Lex.getUIntVal() < NumberedTypes.size()) | |
1365 | Result = NumberedTypes[Lex.getUIntVal()]; | |
1366 | else { | |
1367 |
std::map |
|
1368 | I = ForwardRefTypeIDs.find(Lex.getUIntVal()); | |
1369 | if (I != ForwardRefTypeIDs.end()) | |
1370 | Result = I->second.first; | |
1371 | else { | |
1372 | Result = OpaqueType::get(Context); | |
1373 | ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(), | |
1374 | std::make_pair(Result, | |
1375 | Lex.getLoc()))); | |
1376 | } | |
1377 |
|
|
1245 | }⏎ | |
1246 | ||
1247 | case lltok::LocalVarID: { | |
1248 | // Type ::= %4 | |
1249 | if (Lex.getUIntVal() >= NumberedTypes.size()) | |
1250 | NumberedTypes.resize(Lex.getUIntVal()+1); | |
1251 |
std::pair |
|
1252 | ||
1253 | // If the type hasn't been defined yet, create a forward definition and | |
1254 | // remember where that forward def'n was seen (in case it never is defined). | |
1255 | if (Entry.first == 0) { | |
1256 | Entry.first = StructType::createNamed(Context, ""); | |
1257 | Entry.second = Lex.getLoc(); | |
1258 | } | |
1259 | Result = Entry.first; | |
1378 | 1260 | Lex.Lex(); |
1379 | break; | |
1380 | case lltok::backslash: { | |
1381 | // TypeRec ::= '\' 4 | |
1382 | Lex.Lex(); | |
1383 | unsigned Val; | |
1384 | if (ParseUInt32(Val)) return true; | |
1385 | OpaqueType *OT = OpaqueType::get(Context); //Use temporary placeholder. | |
1386 | UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT)); | |
1387 | Result = OT; | |
1388 | 1261 | break; |
1389 | 1262 | } |
1390 | 1263 | } |
1393 | 1266 | while (1) { |
1394 | 1267 | switch (Lex.getKind()) { |
1395 | 1268 | // End of type. |
1396 | default: return false; | |
1397 | ||
1398 |
|
|
1269 | default:⏎ | |
1270 | if (!AllowVoid && Result->isVoidTy()) | |
1271 | return Error(TypeLoc, "void type only allowed for function results"); | |
1272 | return false; | |
1273 | ||
1274 | // Type ::= Type '*' | |
1399 | 1275 | case lltok::star: |
1400 |
if (Result |
|
1276 | if (Result->isLabelTy())⏎ | |
1401 | 1277 | return TokError("basic block pointers are invalid"); |
1402 | if (Result.get()->isVoidTy()) | |
1403 | return TokError("pointers to void are invalid; use i8* instead"); | |
1404 |
if ( |
|
1278 | if (Result->isVoidTy())⏎ | |
1279 | return TokError("pointers to void are invalid - use i8* instead"); | |
1280 | if (!PointerType::isValidElementType(Result)) | |
1405 | 1281 | return TokError("pointer to this type is invalid"); |
1406 |
Result = |
|
1282 | Result = PointerType::getUnqual(Result);⏎ | |
1407 | 1283 | Lex.Lex(); |
1408 | 1284 | break; |
1409 | 1285 | |
1410 |
// Type |
|
1286 | // Type ::= Type 'addrspace' '(' uint32 ')' '*'⏎ | |
1411 | 1287 | case lltok::kw_addrspace: { |
1412 |
if (Result |
|
1288 | if (Result->isLabelTy())⏎ | |
1413 | 1289 | return TokError("basic block pointers are invalid"); |
1414 |
if (Result |
|
1290 | if (Result->isVoidTy())⏎ | |
1415 | 1291 | return TokError("pointers to void are invalid; use i8* instead"); |
1416 |
if (!PointerType::isValidElementType(Result |
|
1292 | if (!PointerType::isValidElementType(Result))⏎ | |
1417 | 1293 | return TokError("pointer to this type is invalid"); |
1418 | 1294 | unsigned AddrSpace; |
1419 | 1295 | if (ParseOptionalAddrSpace(AddrSpace) || |
1420 | 1296 | ParseToken(lltok::star, "expected '*' in address space")) |
1421 | 1297 | return true; |
1422 | 1298 | |
1423 |
Result = |
|
1299 | Result = PointerType::get(Result, AddrSpace);⏎ | |
1424 | 1300 | break; |
1425 | 1301 | } |
1426 | 1302 | |
1451 | 1327 | |
1452 | 1328 | // Parse the argument. |
1453 | 1329 | LocTy ArgLoc; |
1454 |
|
|
1330 | Type *ArgTy = 0;⏎ | |
1455 | 1331 | unsigned ArgAttrs1 = Attribute::None; |
1456 | 1332 | unsigned ArgAttrs2 = Attribute::None; |
1457 | 1333 | Value *V; |
1471 | 1347 | |
1472 | 1348 | |
1473 | 1349 | /// ParseArgumentList - Parse the argument list for a function type or function |
1474 |
/// prototype. |
|
1350 | /// prototype.⏎ | |
1475 | 1351 | /// ::= '(' ArgTypeListI ')' |
1476 | 1352 | /// ArgTypeListI |
1477 | 1353 | /// ::= /*empty*/ |
1479 | 1355 | /// ::= ArgTypeList ',' '...' |
1480 | 1356 | /// ::= ArgType (',' ArgType)* |
1481 | 1357 | /// |
1482 |
bool LLParser::ParseArgumentList(std::vector |
|
1483 | bool &isVarArg, bool inType) {⏎ | |
1358 |
bool LLParser::ParseArgumentList(SmallVectorImpl |
|
1359 | bool &isVarArg){ | |
1484 | 1360 | isVarArg = false; |
1485 | 1361 | assert(Lex.getKind() == lltok::lparen); |
1486 | 1362 | Lex.Lex(); // eat the (. |
1492 | 1368 | Lex.Lex(); |
1493 | 1369 | } else { |
1494 | 1370 | LocTy TypeLoc = Lex.getLoc(); |
1495 |
|
|
1371 | Type *ArgTy = 0;⏎ | |
1496 | 1372 | unsigned Attrs; |
1497 | 1373 | std::string Name; |
1498 | 1374 | |
1499 | // If we're parsing a type, use ParseTypeRec, because we allow recursive | |
1500 | // types (such as a function returning a pointer to itself). If parsing a | |
1501 | // function prototype, we require fully resolved types. | |
1502 |
if ( |
|
1375 | if (ParseType(ArgTy) ||⏎ | |
1503 | 1376 | ParseOptionalAttrs(Attrs, 0)) return true; |
1504 | 1377 | |
1505 | 1378 | if (ArgTy->isVoidTy()) |
1524 | 1397 | |
1525 | 1398 | // Otherwise must be an argument type. |
1526 | 1399 | TypeLoc = Lex.getLoc(); |
1527 | if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) || | |
1528 |
|
|
1400 | if (ParseType(ArgTy) || ParseOptionalAttrs(Attrs, 0)) return true;⏎ | |
1529 | 1401 | |
1530 | 1402 | if (ArgTy->isVoidTy()) |
1531 | 1403 | return Error(TypeLoc, "argument can not have void type"); |
1537 | 1409 | Name = ""; |
1538 | 1410 | } |
1539 | 1411 | |
1540 |
if (!ArgTy->isFirstClassType() |
|
1412 | if (!ArgTy->isFirstClassType())⏎ | |
1541 | 1413 | return Error(TypeLoc, "invalid type for function argument"); |
1542 | 1414 | |
1543 | 1415 | ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); |
1549 | 1421 | |
1550 | 1422 | /// ParseFunctionType |
1551 | 1423 | /// ::= Type ArgumentList OptionalAttrs |
1552 |
bool LLParser::ParseFunctionType( |
|
1424 | bool LLParser::ParseFunctionType(Type *&Result) {⏎ | |
1553 | 1425 | assert(Lex.getKind() == lltok::lparen); |
1554 | 1426 | |
1555 | 1427 | if (!FunctionType::isValidReturnType(Result)) |
1556 | 1428 | return TokError("invalid function return type"); |
1557 | 1429 | |
1558 |
|
|
1430 |
SmallVector |
|
1559 | 1431 | bool isVarArg; |
1560 |
if (ParseArgumentList(ArgList, isVarArg |
|
1432 | if (ParseArgumentList(ArgList, isVarArg))⏎ | |
1561 | 1433 | return true; |
1562 | 1434 | |
1563 | 1435 | // Reject names on the arguments lists. |
1569 | 1441 | "argument attributes invalid in function type"); |
1570 | 1442 | } |
1571 | 1443 | |
1572 |
|
|
1444 |
SmallVector |
|
1573 | 1445 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
1574 | ArgListTy.push_back(ArgList[i].Type); | |
1575 | ||
1576 | Result = HandleUpRefs(FunctionType::get(Result.get(), | |
1577 | ArgListTy, isVarArg)); | |
1578 | return false; | |
1579 | }⏎ | |
1446 | ArgListTy.push_back(ArgList[i].Ty);⏎ | |
1447 | ||
1448 | Result = FunctionType::get(Result, ArgListTy, isVarArg); | |
1449 | return false; | |
1450 | } | |
1451 | ||
1452 | /// ParseAnonStructType - Parse an anonymous struct type, which is inlined into | |
1453 | /// other structs. | |
1454 | bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) { | |
1455 |
SmallVector |
|
1456 | if (ParseStructBody(Elts)) return true; | |
1457 | ||
1458 | Result = StructType::get(Context, Elts, Packed); | |
1459 | return false; | |
1460 | } | |
1461 | ||
1462 | /// ParseStructDefinition - Parse a struct in a 'type' definition. | |
1463 | bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name, | |
1464 |
std::pair |
|
1465 | Type *&ResultTy) { | |
1466 | // If the type was already defined, diagnose the redefinition. | |
1467 | if (Entry.first && !Entry.second.isValid()) | |
1468 | return Error(TypeLoc, "redefinition of type"); | |
1469 | ||
1470 | // If we have opaque, just return without filling in the definition for the | |
1471 | // struct. This counts as a definition as far as the .ll file goes. | |
1472 | if (EatIfPresent(lltok::kw_opaque)) { | |
1473 | // This type is being defined, so clear the location to indicate this. | |
1474 | Entry.second = SMLoc(); | |
1475 | ||
1476 | // If this type number has never been uttered, create it. | |
1477 | if (Entry.first == 0) | |
1478 | Entry.first = StructType::createNamed(Context, Name); | |
1479 | ResultTy = Entry.first; | |
1480 | return false; | |
1481 | } | |
1482 | ||
1483 | // If the type starts with '<', then it is either a packed struct or a vector. | |
1484 | bool isPacked = EatIfPresent(lltok::less); | |
1485 | ||
1486 | // If we don't have a struct, then we have a random type alias, which we | |
1487 | // accept for compatibility with old files. These types are not allowed to be | |
1488 | // forward referenced and not allowed to be recursive. | |
1489 | if (Lex.getKind() != lltok::lbrace) { | |
1490 | if (Entry.first) | |
1491 | return Error(TypeLoc, "forward references to non-struct type"); | |
1492 | ||
1493 | ResultTy = 0; | |
1494 | if (isPacked) | |
1495 | return ParseArrayVectorType(ResultTy, true); | |
1496 | return ParseType(ResultTy); | |
1497 | } | |
1498 | ||
1499 | // This type is being defined, so clear the location to indicate this. | |
1500 | Entry.second = SMLoc(); | |
1501 | ||
1502 | // If this type number has never been uttered, create it. | |
1503 | if (Entry.first == 0) | |
1504 | Entry.first = StructType::createNamed(Context, Name); | |
1505 | ||
1506 |
StructType *STy = cast |
|
1507 | ||
1508 |
SmallVector |
|
1509 | if (ParseStructBody(Body) || | |
1510 | (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct"))) | |
1511 | return true; | |
1512 | ||
1513 | STy->setBody(Body, isPacked); | |
1514 | ResultTy = STy; | |
1515 | return false; | |
1516 | } | |
1517 | ||
1580 | 1518 | |
1581 | 1519 | /// ParseStructType: Handles packed and unpacked types. > parsed elsewhere. |
1582 |
/// |
|
1520 | /// StructType⏎ | |
1583 | 1521 | /// ::= '{' '}' |
1584 |
/// ::= '{' Type |
|
1522 | /// ::= '{' Type (',' Type)* '}'⏎ | |
1585 | 1523 | /// ::= '<' '{' '}' '>' |
1586 | /// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>' | |
1587 | bool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) {⏎ | |
1524 | /// ::= '<' '{' Type (',' Type)* '}' '>'⏎ | |
1525 |
bool LLParser::ParseStructBody(SmallVectorImpl |
|
1588 | 1526 | assert(Lex.getKind() == lltok::lbrace); |
1589 | 1527 | Lex.Lex(); // Consume the '{' |
1590 | 1528 | |
1591 | if (EatIfPresent(lltok::rbrace)) { | |
1592 | Result = StructType::get(Context, Packed); | |
1593 | return false; | |
1594 | } | |
1595 | ||
1596 |
|
|
1529 | // Handle the empty struct.⏎ | |
1530 | if (EatIfPresent(lltok::rbrace)) | |
1531 | return false; | |
1532 | ||
1597 | 1533 | LocTy EltTyLoc = Lex.getLoc(); |
1598 | if (ParseTypeRec(Result)) return true; | |
1599 | ParamsList.push_back(Result); | |
1600 | ||
1601 | if (Result->isVoidTy()) | |
1602 | return Error(EltTyLoc, "struct element can not have void type"); | |
1603 |
|
|
1534 | Type *Ty = 0;⏎ | |
1535 | if (ParseType(Ty)) return true; | |
1536 | Body.push_back(Ty); | |
1537 | ||
1538 | if (!StructType::isValidElementType(Ty)) | |
1604 | 1539 | return Error(EltTyLoc, "invalid element type for struct"); |
1605 | 1540 | |
1606 | 1541 | while (EatIfPresent(lltok::comma)) { |
1607 | 1542 | EltTyLoc = Lex.getLoc(); |
1608 | if (ParseTypeRec(Result)) return true; | |
1609 | ||
1610 | if (Result->isVoidTy()) | |
1611 | return Error(EltTyLoc, "struct element can not have void type"); | |
1612 |
if ( |
|
1543 | if (ParseType(Ty)) return true;⏎ | |
1544 | ||
1545 | if (!StructType::isValidElementType(Ty)) | |
1613 | 1546 | return Error(EltTyLoc, "invalid element type for struct"); |
1614 | 1547 | |
1615 | ParamsList.push_back(Result); | |
1616 | } | |
1617 | ||
1618 | if (ParseToken(lltok::rbrace, "expected '}' at end of struct")) | |
1619 | return true; | |
1620 | ||
1621 |
std::vector |
|
1622 | for (unsigned i = 0, e = ParamsList.size(); i != e; ++i) | |
1623 | ParamsListTy.push_back(ParamsList[i].get()); | |
1624 | Result = HandleUpRefs(StructType::get(Context, ParamsListTy, Packed)); | |
1625 |
|
|
1548 | Body.push_back(Ty);⏎ | |
1549 | } | |
1550 | ||
1551 | return ParseToken(lltok::rbrace, "expected '}' at end of struct"); | |
1626 | 1552 | } |
1627 | 1553 | |
1628 | 1554 | /// ParseArrayVectorType - Parse an array or vector type, assuming the first |
1629 | 1555 | /// token has already been consumed. |
1630 |
/// Type |
|
1556 | /// Type⏎ | |
1631 | 1557 | /// ::= '[' APSINTVAL 'x' Types ']' |
1632 | 1558 | /// ::= '<' APSINTVAL 'x' Types '>' |
1633 |
bool LLParser::ParseArrayVectorType( |
|
1559 | bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {⏎ | |
1634 | 1560 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || |
1635 | 1561 | Lex.getAPSIntVal().getBitWidth() > 64) |
1636 | 1562 | return TokError("expected number in address space"); |
1643 | 1569 | return true; |
1644 | 1570 | |
1645 | 1571 | LocTy TypeLoc = Lex.getLoc(); |
1646 | PATypeHolder EltTy(Type::getVoidTy(Context)); | |
1647 | if (ParseTypeRec(EltTy)) return true; | |
1648 | ||
1649 | if (EltTy->isVoidTy()) | |
1650 |
|
|
1572 | Type *EltTy = 0;⏎ | |
1573 | if (ParseType(EltTy)) return true; | |
1651 | 1574 | |
1652 | 1575 | if (ParseToken(isVector ? lltok::greater : lltok::rsquare, |
1653 | 1576 | "expected end of sequential type")) |
1664 | 1587 | } else { |
1665 | 1588 | if (!ArrayType::isValidElementType(EltTy)) |
1666 | 1589 | return Error(TypeLoc, "invalid array element type"); |
1667 |
Result = |
|
1590 | Result = ArrayType::get(EltTy, Size);⏎ | |
1668 | 1591 | } |
1669 | 1592 | return false; |
1670 | 1593 | } |
1769 | 1692 | } |
1770 | 1693 | |
1771 | 1694 | // Don't make placeholders with invalid type. |
1772 |
if (!Ty->isFirstClassType() && !Ty->is |
|
1695 | if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {⏎ | |
1773 | 1696 | P.Error(Loc, "invalid use of a non-first-class type"); |
1774 | 1697 | return 0; |
1775 | 1698 | } |
1810 | 1733 | return 0; |
1811 | 1734 | } |
1812 | 1735 | |
1813 |
if (!Ty->isFirstClassType() && !Ty->is |
|
1736 | if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {⏎ | |
1814 | 1737 | P.Error(Loc, "invalid use of a non-first-class type"); |
1815 | 1738 | return 0; |
1816 | 1739 | } |
1986 | 1909 | ParseToken(lltok::rbrace, "expected end of struct constant")) |
1987 | 1910 | return true; |
1988 | 1911 | |
1989 | // FIXME: Get this type from context instead of reconstructing it! | |
1990 | ID.ConstantVal = ConstantStruct::getAnon(Context, Elts); | |
1991 |
ID. |
|
1912 | ID.ConstantStructElts = new Constant*[Elts.size()];⏎ | |
1913 | ID.UIntVal = Elts.size(); | |
1914 | memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0])); | |
1915 | ID.Kind = ValID::t_ConstantStruct; | |
1992 | 1916 | return false; |
1993 | 1917 | } |
1994 | 1918 | case lltok::less: { |
2006 | 1930 | return true; |
2007 | 1931 | |
2008 | 1932 | if (isPackedStruct) { |
2009 | // FIXME: Get this type from context instead of reconstructing it! | |
2010 | ID.ConstantVal = ConstantStruct::getAnon(Context, Elts, true); | |
2011 |
ID. |
|
1933 | ID.ConstantStructElts = new Constant*[Elts.size()];⏎ | |
1934 | memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0])); | |
1935 | ID.UIntVal = Elts.size(); | |
1936 | ID.Kind = ValID::t_PackedConstantStruct; | |
2012 | 1937 | return false; |
2013 | 1938 | } |
2014 | 1939 | |
2130 | 2055 | case lltok::kw_inttoptr: |
2131 | 2056 | case lltok::kw_ptrtoint: { |
2132 | 2057 | unsigned Opc = Lex.getUIntVal(); |
2133 |
|
|
2058 | Type *DestTy = 0;⏎ | |
2134 | 2059 | Constant *SrcVal; |
2135 | 2060 | Lex.Lex(); |
2136 | 2061 | if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") || |
2413 | 2338 | } |
2414 | 2339 | |
2415 | 2340 | bool LLParser::ParseGlobalTypeAndValue(Constant *&V) { |
2416 | PATypeHolder Type(Type::getVoidTy(Context)); | |
2417 | return ParseType(Type) || | |
2418 |
|
|
2341 | Type *Ty = 0;⏎ | |
2342 | return ParseType(Ty) || | |
2343 | ParseGlobalValue(Ty, V); | |
2419 | 2344 | } |
2420 | 2345 | |
2421 | 2346 | /// ParseGlobalValueVector |
2561 | 2486 | return false; |
2562 | 2487 | case ValID::t_Undef: |
2563 | 2488 | // FIXME: LabelTy should not be a first-class type. |
2564 | if ((!Ty->isFirstClassType() || Ty->isLabelTy()) && | |
2565 |
|
|
2489 | if (!Ty->isFirstClassType() || Ty->isLabelTy())⏎ | |
2566 | 2490 | return Error(ID.Loc, "invalid type for undef constant"); |
2567 | 2491 | V = UndefValue::get(Ty); |
2568 | 2492 | return false; |
2583 | 2507 | |
2584 | 2508 | V = ID.ConstantVal; |
2585 | 2509 | return false; |
2586 | } | |
2587 | } | |
2588 | ||
2589 | bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {⏎ | |
2510 | case ValID::t_ConstantStruct:⏎ | |
2511 | case ValID::t_PackedConstantStruct: | |
2512 |
if (const StructType *ST = dyn_cast |
|
2513 | if (ST->getNumElements() != ID.UIntVal) | |
2514 | return Error(ID.Loc, | |
2515 | "initializer with struct type has wrong # elements"); | |
2516 | if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct)) | |
2517 | return Error(ID.Loc, "packed'ness of initializer and type don't match"); | |
2518 | ||
2519 | // Verify that the elements are compatible with the structtype. | |
2520 | for (unsigned i = 0, e = ID.UIntVal; i != e; ++i) | |
2521 | if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i)) | |
2522 | return Error(ID.Loc, "element " + Twine(i) + | |
2523 | " of struct initializer doesn't match struct element type"); | |
2524 | ||
2525 |
V = ConstantStruct::get(ST, ArrayRef |
|
2526 | ID.UIntVal)); | |
2527 | } else | |
2528 | return Error(ID.Loc, "constant expression type mismatch"); | |
2529 | return false; | |
2530 | } | |
2531 | } | |
2532 | ||
2533 | bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState *PFS) { | |
2590 | 2534 | V = 0; |
2591 | 2535 | ValID ID; |
2592 | return ParseValID(ID, &PFS) || | |
2593 | ConvertValIDToValue(Ty, ID, V, &PFS); | |
2594 | } | |
2595 | ||
2596 | bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { | |
2597 | PATypeHolder T(Type::getVoidTy(Context)); | |
2598 | return ParseType(T) || | |
2599 |
|
|
2536 | return ParseValID(ID, PFS) ||⏎ | |
2537 | ConvertValIDToValue(Ty, ID, V, PFS); | |
2538 | } | |
2539 | ||
2540 | bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) { | |
2541 | Type *Ty = 0; | |
2542 | return ParseType(Ty) || | |
2543 | ParseValue(Ty, V, PFS); | |
2600 | 2544 | } |
2601 | 2545 | |
2602 | 2546 | bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, |
2622 | 2566 | |
2623 | 2567 | unsigned Visibility, RetAttrs; |
2624 | 2568 | CallingConv::ID CC; |
2625 |
|
|
2569 | Type *RetType = 0;⏎ | |
2626 | 2570 | LocTy RetTypeLoc = Lex.getLoc(); |
2627 | 2571 | if (ParseOptionalLinkage(Linkage) || |
2628 | 2572 | ParseOptionalVisibility(Visibility) || |
2659 | 2603 | return Error(LinkageLoc, "invalid function linkage type"); |
2660 | 2604 | } |
2661 | 2605 | |
2662 | if (!FunctionType::isValidReturnType(RetType) || | |
2663 |
|