LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/sandesh/compiler/parse - t_scope.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 56 115 48.7 %
Date: 2026-11-09 02:10:49 Functions: 8 10 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Licensed to the Apache Software Foundation (ASF) under one
       3             :  * or more contributor license agreements. See the NOTICE file
       4             :  * distributed with this work for additional information
       5             :  * regarding copyright ownership. The ASF licenses this file
       6             :  * to you under the Apache License, Version 2.0 (the
       7             :  * "License"); you may not use this file except in compliance
       8             :  * with the License. You may obtain a copy of the License at
       9             :  *
      10             :  *   http://www.apache.org/licenses/LICENSE-2.0
      11             :  *
      12             :  * Unless required by applicable law or agreed to in writing,
      13             :  * software distributed under the License is distributed on an
      14             :  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      15             :  * KIND, either express or implied. See the License for the
      16             :  * specific language governing permissions and limitations
      17             :  * under the License.
      18             :  *
      19             :  * Copyright 2006-2017 The Apache Software Foundation.
      20             :  * https://github.com/apache/thrift
      21             :  *
      22             :  * Modifications Copyright (c) Juniper Networks, Inc.
      23             :  * Copyright Contributors to the OpenSDN project
      24             :  */
      25             : 
      26             : #ifndef T_SCOPE_H
      27             : #define T_SCOPE_H
      28             : 
      29             : #include <map>
      30             : #include <string>
      31             : #include <sstream>
      32             : 
      33             : #include "t_type.h"
      34             : #include "t_service.h"
      35             : #include "t_const.h"
      36             : #include "t_const_value.h"
      37             : #include "t_base_type.h"
      38             : #include "t_map.h"
      39             : #include "t_list.h"
      40             : #ifdef SANDESH
      41             : #include "t_sandesh.h"
      42             : #endif
      43             : 
      44             : /**
      45             :  * This represents a variable scope used for looking up predefined types and
      46             :  * services. Typically, a scope is associated with a t_program. Scopes are not
      47             :  * used to determine code generation, but rather to resolve identifiers at
      48             :  * parse time.
      49             :  *
      50             :  */
      51             : class t_scope {
      52             :  public:
      53         776 :   t_scope() {}
      54             : 
      55        7450 :   void add_type(std::string name, t_type* type) {
      56        7450 :     types_[name] = type;
      57        7450 :   }
      58             : 
      59        5619 :   t_type* get_type(std::string name) {
      60        5619 :     return types_[name];
      61             :   }
      62             : 
      63           0 :   void add_service(std::string name, t_service* service) {
      64           0 :     services_[name] = service;
      65           0 :   }
      66             : 
      67           0 :   t_service* get_service(std::string name) {
      68           0 :     return services_[name];
      69             :   }
      70             : 
      71             : #ifdef SANDESH
      72        8080 :   void add_sandesh(std::string name, t_sandesh* sandesh) {
      73        8080 :     sandeshs_[name] = sandesh;
      74        8080 :   }
      75             : 
      76        5804 :   t_sandesh* get_sandesh(std::string name) {
      77        5804 :     return sandeshs_[name];
      78             :   }
      79             : #endif
      80             : 
      81        8638 :   void add_constant(std::string name, t_const* constant) {
      82        8638 :     if (constants_.find(name) != constants_.end()) {
      83           0 :       throw "Enum " + name + " is already defined!";
      84             :     } else {
      85        8638 :       constants_[name] = constant;
      86             :     }
      87        8638 :   }
      88             : 
      89        4120 :   t_const* get_constant(std::string name) {
      90        4120 :     return constants_[name];
      91             :   }
      92             : 
      93             :   void print() {
      94             :     std::map<std::string, t_type*>::iterator iter;
      95             :     for (iter = types_.begin(); iter != types_.end(); ++iter) {
      96             :       printf("%s => %s\n",
      97             :              iter->first.c_str(),
      98             :              iter->second->get_name().c_str());
      99             :     }
     100             :   }
     101             : 
     102       37530 :   void resolve_const_value(t_const_value* const_val, t_type* ttype) {
     103       37530 :     if (ttype->is_map()) {
     104         535 :       const std::map<t_const_value*, t_const_value*>& map = const_val->get_map();
     105         535 :       std::map<t_const_value*, t_const_value*>::const_iterator v_iter;
     106        6207 :       for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) {
     107        5672 :         resolve_const_value(v_iter->first, ((t_map*)ttype)->get_key_type());
     108        5672 :         resolve_const_value(v_iter->second, ((t_map*)ttype)->get_val_type());
     109             :       }
     110       36995 :     } else if (ttype->is_list() || ttype->is_set()) {
     111         964 :       const std::vector<t_const_value*>& val = const_val->get_list();
     112         964 :       std::vector<t_const_value*>::const_iterator v_iter;
     113        7641 :       for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
     114        6677 :         resolve_const_value((*v_iter), ((t_list*)ttype)->get_elem_type());
     115             :       }
     116       36031 :     } else if (ttype->is_struct()) {
     117        4940 :       t_struct* tstruct = (t_struct*)ttype;
     118        4940 :       const std::map<t_const_value*, t_const_value*>& map = const_val->get_map();
     119        4940 :       std::map<t_const_value*, t_const_value*>::const_iterator v_iter;
     120       20800 :       for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) {
     121       15860 :         t_field* field = tstruct->get_field_by_name(v_iter->first->get_string());
     122       15860 :         if (field == NULL) {
     123           0 :           throw "No field named \"" + v_iter->first->get_string() + "\" was found in struct of type \"" + tstruct->get_name() + "\"";
     124             :         }
     125       15860 :         resolve_const_value(v_iter->second, field->get_type());
     126             :       }
     127       31091 :     } else if (const_val->get_type() == t_const_value::CV_IDENTIFIER) {
     128        9092 :       if (ttype->is_enum()) {
     129        4972 :         const_val->set_enum((t_enum*)ttype);
     130             :       } else {
     131        4120 :         t_const* constant = get_constant(const_val->get_identifier());
     132        4120 :         if (constant == NULL) {
     133           0 :           throw "No enum value or constant found named \"" + const_val->get_identifier() + "\"!";
     134             :         }
     135             : 
     136             :         // Resolve typedefs to the underlying type
     137        4120 :         t_type* const_type = constant->get_type()->get_true_type();
     138             : 
     139        4120 :         if (const_type->is_base_type()) {
     140        4120 :           switch (((t_base_type*)const_type)->get_base()) {
     141         159 :             case t_base_type::TYPE_I16:
     142             :             case t_base_type::TYPE_I32:
     143             :             case t_base_type::TYPE_I64:
     144             :             case t_base_type::TYPE_BOOL:
     145             :             case t_base_type::TYPE_BYTE:
     146         159 :               const_val->set_integer(constant->get_value()->get_integer());
     147         159 :               break;
     148        3631 :             case t_base_type::TYPE_STRING:
     149        3631 :               const_val->set_string(constant->get_value()->get_string());
     150        3631 :               break;
     151           0 :             case t_base_type::TYPE_DOUBLE:
     152           0 :               const_val->set_double(constant->get_value()->get_double());
     153           0 :               break;
     154           0 :             case t_base_type::TYPE_VOID:
     155           0 :               throw "Constants cannot be of type VOID";
     156             : #ifdef SANDESH
     157         330 :             case t_base_type::TYPE_U16:
     158             :             case t_base_type::TYPE_U32:
     159             :             case t_base_type::TYPE_U64:
     160         330 :               const_val->set_integer(constant->get_value()->get_integer());
     161         330 :               break;
     162           0 :             case t_base_type::TYPE_STATIC_CONST_STRING:
     163             :             case t_base_type::TYPE_XML:
     164           0 :               const_val->set_string(constant->get_value()->get_string());
     165           0 :               break;
     166           0 :             case t_base_type::TYPE_SANDESH_SYSTEM:
     167           0 :               break;
     168           0 :             case t_base_type::TYPE_SANDESH_REQUEST:
     169           0 :               break;
     170           0 :             case t_base_type::TYPE_SANDESH_RESPONSE:
     171           0 :               break;
     172           0 :             case t_base_type::TYPE_SANDESH_TRACE:
     173           0 :               break;
     174           0 :             case t_base_type::TYPE_SANDESH_TRACE_OBJECT:
     175           0 :               break;
     176           0 :             case t_base_type::TYPE_SANDESH_BUFFER:
     177           0 :               break;
     178           0 :             case t_base_type::TYPE_SANDESH_UVE:
     179           0 :               break;
     180           0 :             case t_base_type::TYPE_SANDESH_DYNAMIC_UVE:
     181           0 :               break;
     182           0 :             case t_base_type::TYPE_SANDESH_OBJECT:
     183           0 :               break;
     184           0 :             case t_base_type::TYPE_SANDESH_FLOW:
     185           0 :               break;
     186           0 :             case t_base_type::TYPE_SANDESH_SESSION:
     187           0 :               break;
     188             : #endif
     189             :           }
     190           0 :         } else if (const_type->is_map()) {
     191           0 :           const std::map<t_const_value*, t_const_value*>& map = constant->get_value()->get_map();
     192           0 :           std::map<t_const_value*, t_const_value*>::const_iterator v_iter;
     193             : 
     194           0 :           const_val->set_map();
     195           0 :           for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) {
     196           0 :             const_val->add_map(v_iter->first, v_iter->second);
     197             :           }
     198           0 :         } else if (const_type->is_list()) {
     199           0 :           const std::vector<t_const_value*>& val = constant->get_value()->get_list();
     200           0 :           std::vector<t_const_value*>::const_iterator v_iter;
     201             : 
     202           0 :           const_val->set_list();
     203           0 :           for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
     204           0 :             const_val->add_list(*v_iter);
     205             :           }
     206             :         }
     207             :       }
     208       21999 :     } else if (ttype->is_enum()) {
     209             :       // enum constant with non-identifier value. set the enum and find the
     210             :       // value's name.
     211           0 :       t_enum* tenum = (t_enum*)ttype;
     212           0 :       t_enum_value* enum_value = tenum->get_constant_by_value(const_val->get_integer());
     213           0 :       if (enum_value == NULL) {
     214           0 :         std::ostringstream valstm;
     215           0 :         valstm << const_val->get_integer();
     216           0 :         throw "Couldn't find a named value in enum " + tenum->get_name() + " for value " + valstm.str();
     217           0 :       }
     218           0 :       const_val->set_identifier(tenum->get_name() + "." + enum_value->get_name());
     219           0 :       const_val->set_enum(tenum);
     220             :     }
     221       37530 :   }
     222             : 
     223             :  private:
     224             : 
     225             :   // Map of names to types
     226             :   std::map<std::string, t_type*> types_;
     227             : 
     228             :   // Map of names to constants
     229             :   std::map<std::string, t_const*> constants_;
     230             : 
     231             :   // Map of names to services
     232             :   std::map<std::string, t_service*> services_;
     233             : 
     234             : #ifdef SANDESH
     235             :   // Map of names to sandeshs
     236             :   std::map<std::string, t_sandesh*> sandeshs_;
     237             : #endif
     238             : 
     239             : };
     240             : 
     241             : #endif

Generated by: LCOV version 1.14