# What is a "nested scope"?

**URL:** <https://verificationacademy.com/forums/t/what-is-a-nested-scope/27802>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [March 5, 2012, 6:24am UTC](https://verificationacademy.com/forums/t/what-is-a-nested-scope/27802 "2012-03-05T06:24:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liangjun\_Wang](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/65b543/32.png) [@Liangjun\_Wang](https://verificationacademy.com/forums/u/Liangjun_Wang)\
**Post date:** [March 5, 2012, 6:24am UTC](https://verificationacademy.com/forums/t/what-is-a-nested-scope/27802/1 "2012-03-05T06:24:02Z")

</div>

I am puzzled by the following paragraph, can anybody tell me what is a “nested scope”?  
“When an identifier is referenced within a scope  
— First, the nested scope is searched (see 23.9) (including nested module declarations), including any  
identifiers made available through package import declarations.”( IEEE Standard for SystemVerilog)

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 5, 2012, 7:02am UTC](https://verificationacademy.com/forums/t/what-is-a-nested-scope/27802/2 "2012-03-05T07:02:54Z")

</div>

In this context, a nested scope is any region of code inside a compilation unit that creates a new scope. A module declaration creates a scope nested inside a compilation unit. A task declaration inside that module declaration is a nested scope within the module.

Sometimes the term _lexical scope_ is used interchangeably with _nested scope_, but a nested scope excludes the scope that is the compilation unit. If you have the following code in a file:

```auto
int A;
task T;
  int B;
endtask
module M;
  int C;
  task T;
    int D;
    $display(A,,,C,,D);
  endtask
endmodule

```

The variable **A** is not declared in a nested scope. The first task **T** and the module **M** are not declared in a nested scope, but both create nested scopes. The variables **B** , **C** and **D** are all declared in nested scopes. The second task **T** creates a nested scope inside the scope **M**.

A reference is any mention of an identifier that is not a declaration. The $display statement references A and D. The compiler will search for A inside task T, then module M, and will find it the compilation unit. It will find C in module M and D in task T. It will do this search without even knowing where module M eventually gets instantiated.

---

<div class="post-metadata">

**Author:** ![Liangjun\_Wang](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/65b543/32.png) [@Liangjun\_Wang](https://verificationacademy.com/forums/u/Liangjun_Wang)\
**Post date:** [March 5, 2012, 8:34am UTC](https://verificationacademy.com/forums/t/what-is-a-nested-scope/27802/3 "2012-03-05T08:34:29Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/what-is-a-nested-scope/27802/2):_

Thank you Dave!

```
So, In your example, If I replace "module M" with "class M ", the result will be the same.

However, if "A" is not declared in the compilation unit, the compiler will search the instance hierarchy tree of module M(upwarding search)?
if reference "A" in "class M" and "A" is not declared in the compilation unit, the compiler will report compile error directly, it will not search instance hierarchy tree?

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 5, 2012, 5:21pm UTC](https://verificationacademy.com/forums/t/what-is-a-nested-scope/27802/4 "2012-03-05T17:21:29Z")

</div>

_In reply to [Wanglj](https://verificationacademy.com/forums/t/what-is-a-nested-scope/27802/3):_

Yes, you can replace **module** with **class** and get the same results.

However, searching the instance hierarchy tree does not happen for simple names like “A”. Verilog will only begin a search of the instance hierarchy for dotted names like “A.B” as well as for function/task references. If “A” was not declared, you should get a compiler error before elaboration.
