From 3340a9e8a54edc6c8586a26a30c2fe6c5785f335 Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Fri, 6 May 2016 05:04:21 -0700 Subject: [PATCH] Create initial vector header for models Summary: Create "empty" vector model header. The actual model implementation will come in next diffs to simplify review process. Reviewed By: dulmarod Differential Revision: D3240683 fb-gh-sync-id: 03ee002 fbshipit-source-id: 03ee002 --- .../cpp/include/infer_model/portability.h | 3 + infer/models/cpp/include/infer_model/vector.h | 518 ++++++++++++++++++ infer/models/cpp/include/vector | 10 + 3 files changed, 531 insertions(+) create mode 100644 infer/models/cpp/include/infer_model/vector.h create mode 100644 infer/models/cpp/include/vector diff --git a/infer/models/cpp/include/infer_model/portability.h b/infer/models/cpp/include/infer_model/portability.h index fc2e559e8..6ff293cef 100644 --- a/infer/models/cpp/include/infer_model/portability.h +++ b/infer/models/cpp/include/infer_model/portability.h @@ -40,11 +40,14 @@ // llvm's libc++. This way folly can forward declare decls from std library // even when they are infer models // https://github.com/facebook/folly/blob/b1eb6819f3ffe6b645f39d505ca8ace3116b7873/folly/Portability.h#L253-L255 +// On top of that, define platform-dependent STL iterators. #if INFER_USE_LIBCPP #include <__config> #define INFER_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD #define INFER_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD +#define STD_ITER(T, C) __wrap_iter #else #define INFER_NAMESPACE_STD_BEGIN namespace std { #define INFER_NAMESPACE_STD_END } +#define STD_ITER(T, C) __gnu_cxx::__normal_iterator #endif diff --git a/infer/models/cpp/include/infer_model/vector.h b/infer/models/cpp/include/infer_model/vector.h new file mode 100644 index 000000000..127b26457 --- /dev/null +++ b/infer/models/cpp/include/infer_model/vector.h @@ -0,0 +1,518 @@ +/* + * Copyright (c) 2016 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#ifdef INFER_USE_LIBCPP +// libc++ vector header includes it, but it breaks +// compilation with stdlibc++ implementation +#include +#endif + +INFER_NAMESPACE_STD_BEGIN + +struct bool_ref { + bool_ref& operator=(bool x) {} + bool_ref& operator=(const bool_ref& x) {} + + operator bool() const noexcept {} + void flip() noexcept {} + + // not part of C++ std, but required for model implementation to compile + explicit bool_ref(bool x) {} +}; + +template +struct vector_ref { + typedef T& ref; +}; + +template <> +struct vector_ref { + typedef bool_ref ref; +}; + +// WARNING: do not add any new fields to std::vector model. sizeof(std::vector) +// = 24 !! +template > +class vector { + + public: + typedef vector __self; + typedef _Tp value_type; + typedef _Allocator allocator_type; + typedef allocator_traits __alloc_traits; + typedef typename vector_ref::ref reference; + typedef const value_type& const_reference; + typedef typename __alloc_traits::size_type size_type; + typedef typename __alloc_traits::difference_type difference_type; + typedef typename __alloc_traits::pointer pointer; + typedef typename __alloc_traits::const_pointer const_pointer; + + typedef STD_ITER(pointer, vector) iterator; + typedef STD_ITER(const_pointer, vector) const_iterator; + typedef std::reverse_iterator reverse_iterator; + typedef std::reverse_iterator const_reverse_iterator; + + bool isEmpty = true; + mutable value_type* modelPtr = nullptr; + // intended to keep semantics of empty vector + // vector.begin() == vector.end() <=> vector.empty() + // also required to keep sizeof(std::vector) same. + value_type* endPtr = nullptr; + + ~vector() {} + + vector() noexcept(is_nothrow_default_constructible::value) {} + explicit vector(const allocator_type& __a) noexcept {} + explicit vector(size_type __n); + // introduced in C++14 + explicit vector(size_type __n, const allocator_type& __a); + vector(size_type __n, const_reference __x); + vector(size_type __n, const_reference __x, const allocator_type& __a); + template + vector(_ForwardIterator __first, + typename enable_if< + is_constructible< + value_type, + typename iterator_traits<_ForwardIterator>::reference>::value, + _ForwardIterator>::type __last); + template + vector(_ForwardIterator __first, + _ForwardIterator __last, + const allocator_type& __a, + typename enable_if::reference>::value>:: + type* = 0); + + vector(initializer_list __il); + + vector(initializer_list __il, const allocator_type& __a); + + vector(const vector& __x); + vector(const vector& __x, const allocator_type& __a); + + vector& operator=(const vector& __x); + + vector(vector&& __x) noexcept; + + vector(vector&& __x, const allocator_type& __a); + + vector& operator=(vector&& __x) noexcept; + /*((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)); */ + + vector& operator=(initializer_list __il) { + assign(__il.begin(), __il.end()); + return *this; + } + + template + typename enable_if::reference>::value, + void>::type + assign(_ForwardIterator __first, _ForwardIterator __last); + + void assign(size_type __n, const_reference __u); + + void assign(initializer_list __il) { + assign(__il.begin(), __il.end()); + } + + allocator_type get_allocator() const {} + + iterator begin() noexcept; + const_iterator begin() const noexcept; + iterator end() noexcept; + const_iterator end() const noexcept; + + reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } + + const_reverse_iterator rbegin() const noexcept { + return const_reverse_iterator(end()); + } + + reverse_iterator rend() noexcept { return reverse_iterator(begin()); } + + const_reverse_iterator rend() const noexcept { + return const_reverse_iterator(begin()); + } + + const_iterator cbegin() const noexcept { return begin(); } + + const_iterator cend() const noexcept { return end(); } + + const_reverse_iterator crbegin() const noexcept { return rbegin(); } + + const_reverse_iterator crend() const noexcept { return rend(); } + + size_type size() const noexcept { /* TODO */ + } + + size_type capacity() const noexcept { /* TODO */ + } + + bool empty() const noexcept { /* TODO */ + } + size_type max_size() const noexcept; + void reserve(size_type __n); + void shrink_to_fit() noexcept; + + reference operator[](size_type __n); + const_reference operator[](size_type __n) const; + reference at(size_type __n); + const_reference at(size_type __n) const; + + reference front() {} + const_reference front() const {} + reference back() {} + const_reference back() const {} + + value_type* data() noexcept {} + + const value_type* data() const noexcept {} + + void push_back(const_reference __x); + void push_back(value_type&& __x); + template + + void emplace_back(_Args&&... __args); + + void pop_back(); + + iterator insert(const_iterator __position, const_reference __x); + iterator insert(const_iterator __position, value_type&& __x); + template + iterator emplace(const_iterator __position, _Args&&... __args); + iterator insert(const_iterator __position, + size_type __n, + const_reference __x); + template + typename enable_if::reference>::value, + iterator>::type + insert(const_iterator __position, + _ForwardIterator __first, + _ForwardIterator __last); + + iterator insert(const_iterator __position, + initializer_list __il) { + return insert(__position, __il.begin(), __il.end()); + } + + iterator erase(const_iterator __position); + iterator erase(const_iterator __first, const_iterator __last); + + void clear() noexcept { /* TODO */ + } + + void resize(size_type __sz); + void resize(size_type __sz, const_reference __x); + + // NOTE c++17 adds noexcept + void swap(vector&); + + private: + iterator __make_iter(pointer __p) noexcept; + + const_iterator __make_iter(const_pointer __p) const noexcept; +}; + +template +typename vector<_Tp, _Allocator>::size_type vector<_Tp, _Allocator>::max_size() + const noexcept { + /*TODO*/ +} + +template +vector<_Tp, _Allocator>::vector(size_type __n) {} + +template +vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a) {} + +template +vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x) {} + +template +vector<_Tp, _Allocator>::vector(size_type __n, + const_reference __x, + const allocator_type& __a) {} + +template +template +vector<_Tp, _Allocator>::vector( + _ForwardIterator __first, + typename enable_if< + is_constructible< + value_type, + typename iterator_traits<_ForwardIterator>::reference>::value, + _ForwardIterator>::type __last) {} + +template +template +vector<_Tp, _Allocator>::vector( + _ForwardIterator __first, + _ForwardIterator __last, + const allocator_type& __a, + typename enable_if::reference>::value>::type*) { +} + +template +vector<_Tp, _Allocator>::vector(const vector& __x) {} + +template +vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a) {} + +template +inline vector<_Tp, _Allocator>::vector(vector&& __x) noexcept { + /* TODO - this->data = __x.data; __x.data = nullptr; + */ +} + +template +inline vector<_Tp, _Allocator>::vector(vector&& __x, + const allocator_type& __a) { + /* TODO - see above */ +} + +template +inline vector<_Tp, _Allocator>::vector(initializer_list __il) {} + +template +inline vector<_Tp, _Allocator>::vector(initializer_list __il, + const allocator_type& __a) {} + +template +inline vector<_Tp, _Allocator>& vector<_Tp, _Allocator>::operator=( + vector&& __x) noexcept /*((__noexcept_move_assign_container<_Allocator, + __alloc_traits>::value)) */ +{ + /* TODO - see above */ +} + +template +inline vector<_Tp, _Allocator>& vector<_Tp, _Allocator>::operator=( + const vector& __x) { + if (this != &__x) { + // assign(__x.__begin_, __x.__end_); + } + return *this; +} + +template +template +typename enable_if::reference>::value, + void>::type +vector<_Tp, _Allocator>::assign(_ForwardIterator __first, + _ForwardIterator __last) {} + +template +void vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u) {} + +template +inline typename vector<_Tp, _Allocator>::iterator +vector<_Tp, _Allocator>::__make_iter(pointer __p) noexcept { + return iterator(__p); +} + +template +inline typename vector<_Tp, _Allocator>::const_iterator +vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const noexcept { + return const_iterator(__p); +} + +template +inline typename vector<_Tp, _Allocator>::iterator +vector<_Tp, _Allocator>::begin() noexcept { + // return __make_iter(this->__begin_); +} + +template +inline typename vector<_Tp, _Allocator>::const_iterator +vector<_Tp, _Allocator>::begin() const noexcept { + // return __make_iter(this->__begin_); +} + +template +inline typename vector<_Tp, _Allocator>::iterator +vector<_Tp, _Allocator>::end() noexcept { + // return __make_iter(this->__end_); +} + +template +inline typename vector<_Tp, _Allocator>::const_iterator +vector<_Tp, _Allocator>::end() const noexcept { + // return __make_iter(this->__end_); +} + +template +inline typename vector<_Tp, _Allocator>::reference vector<_Tp, _Allocator>:: +operator[](size_type __n) {} + +template +inline typename vector<_Tp, _Allocator>::const_reference + vector<_Tp, _Allocator>::operator[](size_type __n) const {} + +template +typename vector<_Tp, _Allocator>::reference vector<_Tp, _Allocator>::at( + size_type __n) {} + +template +typename vector<_Tp, _Allocator>::const_reference vector<_Tp, _Allocator>::at( + size_type __n) const {} + +template +void vector<_Tp, _Allocator>::reserve(size_type __n) {} + +template +void vector<_Tp, _Allocator>::shrink_to_fit() noexcept {} + +template +inline void vector<_Tp, _Allocator>::push_back(const_reference __x) {} + +template +inline void vector<_Tp, _Allocator>::push_back(value_type&& __x) {} + +template +template +inline void vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) { + /* TODO - consider constructing the object + __alloc_traits::construct(this->__alloc(), + _VSTD::__to_raw_pointer(this->__end_), + _VSTD::forward<_Args>(__args)...); + */ +} + +template +inline void vector<_Tp, _Allocator>::pop_back() { + // dereference the object +} + +template +inline typename vector<_Tp, _Allocator>::iterator +vector<_Tp, _Allocator>::erase(const_iterator __position) { + // dereference the object + /* TODO return __r;*/ +} + +template +typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::erase( + const_iterator __first, const_iterator __last) { + // dereference the object + /* TODO return __r;*/ +} + +template +typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::insert( + const_iterator __position, const_reference __x) {} + +template +typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::insert( + const_iterator __position, value_type&& __x) {} + +template +template +typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::emplace( + const_iterator __position, _Args&&... __args) { + // TODO consider constructing the object +} + +template +typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::insert( + const_iterator __position, size_type __n, const_reference __x) {} + +template +template +typename enable_if::reference>::value, + typename vector<_Tp, _Allocator>::iterator>::type +vector<_Tp, _Allocator>::insert(const_iterator __position, + _ForwardIterator __first, + _ForwardIterator __last) { + // TODO return __make_iter(__p); +} + +template +void vector<_Tp, _Allocator>::resize(size_type __sz) {} + +template +void vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x) {} + +template +void vector<_Tp, _Allocator>::swap(vector& __x) {} + +template +struct hash> + : public unary_function, size_t> { + + size_t operator()(const vector& __vec) const noexcept {} +}; + +template +inline bool operator==(const vector<_Tp, _Allocator>& __x, + const vector<_Tp, _Allocator>& __y) { + const typename vector<_Tp, _Allocator>::size_type __sz = __x.size(); + return __sz == __y.size() && equal(__x.begin(), __x.end(), __y.begin()); +} + +template +inline bool operator!=(const vector<_Tp, _Allocator>& __x, + const vector<_Tp, _Allocator>& __y) { + return !(__x == __y); +} + +template +inline bool operator<(const vector<_Tp, _Allocator>& __x, + const vector<_Tp, _Allocator>& __y) { + return lexicographical_compare( + __x.begin(), __x.end(), __y.begin(), __y.end()); +} + +template +inline bool operator>(const vector<_Tp, _Allocator>& __x, + const vector<_Tp, _Allocator>& __y) { + return __y < __x; +} + +template +inline bool operator>=(const vector<_Tp, _Allocator>& __x, + const vector<_Tp, _Allocator>& __y) { + return !(__x < __y); +} + +template +inline bool operator<=(const vector<_Tp, _Allocator>& __x, + const vector<_Tp, _Allocator>& __y) { + return !(__y < __x); +} + +template +inline void swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y) { + __x.swap(__y); +} + +INFER_NAMESPACE_STD_END diff --git a/infer/models/cpp/include/vector b/infer/models/cpp/include/vector new file mode 100644 index 000000000..c19ebf900 --- /dev/null +++ b/infer/models/cpp/include/vector @@ -0,0 +1,10 @@ +#include + +#ifdef INFER_CPP11_ON +#include + +#else +// don't model std::vector pre-c++11 to +// simplify implementation +#include_next +#endif