1
0
Fork 0
arangodb/arangod/Aql/Scopes.h

324 lines
13 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// @brief Aql, scopes
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_AQL_SCOPES_H
#define ARANGODB_AQL_SCOPES_H 1
#include "Basics/Common.h"
#include "Aql/Variable.h"
namespace triagens {
namespace aql {
// -----------------------------------------------------------------------------
// --SECTION-- public types
// -----------------------------------------------------------------------------
enum ScopeType {
AQL_SCOPE_MAIN,
AQL_SCOPE_SUBQUERY,
AQL_SCOPE_FOR,
AQL_SCOPE_COLLECT
};
// -----------------------------------------------------------------------------
// --SECTION-- class Scope
// -----------------------------------------------------------------------------
class Scope {
// -----------------------------------------------------------------------------
// --SECTION-- constructors / destructors
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief create a scope
////////////////////////////////////////////////////////////////////////////////
Scope (ScopeType);
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the scope
////////////////////////////////////////////////////////////////////////////////
~Scope ();
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief return the name of a scope type
////////////////////////////////////////////////////////////////////////////////
std::string typeName () const;
////////////////////////////////////////////////////////////////////////////////
/// @brief return the name of a scope type
////////////////////////////////////////////////////////////////////////////////
static std::string typeName (ScopeType);
////////////////////////////////////////////////////////////////////////////////
/// @brief return the scope type
////////////////////////////////////////////////////////////////////////////////
inline ScopeType type () const {
return _type;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a variable to the scope
////////////////////////////////////////////////////////////////////////////////
void addVariable (Variable*);
////////////////////////////////////////////////////////////////////////////////
/// @brief checks if a variable exists in the scope
////////////////////////////////////////////////////////////////////////////////
bool existsVariable (char const*,
size_t) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief checks if a variable exists in the scope
////////////////////////////////////////////////////////////////////////////////
bool existsVariable (std::string const&) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief return a variable
////////////////////////////////////////////////////////////////////////////////
Variable const* getVariable (char const*,
size_t) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief return a variable
////////////////////////////////////////////////////////////////////////////////
Variable const* getVariable (std::string const&) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief return a variable, allowing usage of special pseudo vars such
/// as OLD and NEW
////////////////////////////////////////////////////////////////////////////////
Variable const* getVariable (char const*,
size_t,
bool) const;
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief scope type
////////////////////////////////////////////////////////////////////////////////
ScopeType const _type;
////////////////////////////////////////////////////////////////////////////////
/// @brief variables introduced by the scope
////////////////////////////////////////////////////////////////////////////////
std::unordered_map<std::string, Variable*> _variables;
};
// -----------------------------------------------------------------------------
// --SECTION-- class Scopes
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief scope management
////////////////////////////////////////////////////////////////////////////////
class Scopes {
// -----------------------------------------------------------------------------
// --SECTION-- constructors / destructors
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief create the scopes
////////////////////////////////////////////////////////////////////////////////
Scopes ();
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the scopes
////////////////////////////////////////////////////////////////////////////////
~Scopes ();
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief number of currently active scopes
////////////////////////////////////////////////////////////////////////////////
inline size_t numActive () const {
return _activeScopes.size();
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the type of the currently active scope
////////////////////////////////////////////////////////////////////////////////
ScopeType type () const {
TRI_ASSERT(numActive() > 0);
return _activeScopes.back()->type();
}
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the $CURRENT variable can be used at the caller's
/// current position
////////////////////////////////////////////////////////////////////////////////
inline bool canUseCurrentVariable () const {
return (! _currentVariables.empty());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief start a new scope
////////////////////////////////////////////////////////////////////////////////
void start (ScopeType);
////////////////////////////////////////////////////////////////////////////////
/// @brief end the current scope
////////////////////////////////////////////////////////////////////////////////
void endCurrent ();
////////////////////////////////////////////////////////////////////////////////
/// @brief end the current scope plus any FOR scopes it is nested in
////////////////////////////////////////////////////////////////////////////////
void endNested ();
////////////////////////////////////////////////////////////////////////////////
/// @brief adds a variable to the current scope
////////////////////////////////////////////////////////////////////////////////
void addVariable (Variable*);
////////////////////////////////////////////////////////////////////////////////
/// @brief checks whether a variable exists in any scope
////////////////////////////////////////////////////////////////////////////////
bool existsVariable (char const*,
size_t) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief return a variable by name - this respects the current scopes
////////////////////////////////////////////////////////////////////////////////
Variable const* getVariable (char const*,
size_t) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief return a variable by name - this respects the current scopes
////////////////////////////////////////////////////////////////////////////////
Variable const* getVariable (std::string const&) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief return a variable by name - this respects the current scopes
/// this also allows using special pseudo vars such as OLD and NEW
////////////////////////////////////////////////////////////////////////////////
Variable const* getVariable (char const*,
size_t,
bool) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief get the $CURRENT variable
////////////////////////////////////////////////////////////////////////////////
Variable const* getCurrentVariable () const;
////////////////////////////////////////////////////////////////////////////////
/// @brief stack a $CURRENT variable from the stack
////////////////////////////////////////////////////////////////////////////////
void stackCurrentVariable (Variable const*);
////////////////////////////////////////////////////////////////////////////////
/// @brief unregister the $CURRENT variable from the stack
////////////////////////////////////////////////////////////////////////////////
void unstackCurrentVariable ();
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief currently active scopes
////////////////////////////////////////////////////////////////////////////////
std::vector<Scope*> _activeScopes;
////////////////////////////////////////////////////////////////////////////////
/// @brief a stack with aliases for the $CURRENT variable
////////////////////////////////////////////////////////////////////////////////
std::vector<Variable const*> _currentVariables;
};
}
}
#endif
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End: